Skip to content

from_xml

Collection Note

This module is part of the ansible.utils collection. To install the collection, use:

ansible-galaxy collection install ansible.utils
Added in version 2.0.2.

Synopsis

  • This plugin converts the XML string to a native python dictionary.
  • Using the parameters below- C(data|ansible.utils.from_xml)

Parameters

Parameter Defaults / Choices Comments
data
str
required
The input XML string.
This option represents the XML value that is passed to the filter plugin in pipe format.
For example C(config_data|ansible.utils.from_xml), in this case C(config_data) represents this option.
engine
str
Default: xmltodict
Conversion library to use within the filter plugin.

Examples

#### Simple examples with out any engine. plugin will use default value as xmltodict


- name: convert given XML to native python dictionary
  ansible.builtin.set_fact:
    data: ' <netconf-state xmlns="urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring"><schemas><schema/></schemas></netconf-state> '
- debug:
    msg: '{{ data|ansible.utils.from_xml }}'

# TASK######
# TASK [convert given XML to native python dictionary] *****************************************************************************************************
# task path: /Users/amhatre/ansible-collections/playbooks/test_utils.yaml:5
# ok: [localhost] => {
#     "ansible_facts": {
#         "data": " <netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\"><schemas><schema/></schemas></netconf-state> "
#     },
#     "changed": false
# }
#
# TASK [debug] *************************************************************************************************************************
# task path: /Users/amhatre/ansible-collections/playbooks/test_utils.yaml:13
# Loading collection ansible.utils from /Users/amhatre/ansible-collections/collections/ansible_collections/ansible/utils
# ok: [localhost] => {
#     "msg": {
#         "netconf-state": {
#             "@xmlns": "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring",
#             "schemas": {
#                 "schema": null
#             }
#         }
#     }
# }

#### example2 with engine=xmltodict

- name: convert given XML to native python dictionary
  ansible.builtin.set_fact:
    data: ' <netconf-state xmlns="urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring"><schemas><schema/></schemas></netconf-state> '
- debug:
    msg: '{{ data|ansible.utils.from_xml(''xmltodict'') }}'

# TASK######
# TASK [convert given XML to native python dictionary] *****************************************************************************************************
# task path: /Users/amhatre/ansible-collections/playbooks/test_utils.yaml:5
# ok: [localhost] => {
#     "ansible_facts": {
#         "data": " <netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\"><schemas><schema/></schemas></netconf-state> "
#     },
#     "changed": false
# }
#
# TASK [debug] *************************************************************************************************************************
# task path: /Users/amhatre/ansible-collections/playbooks/test_utils.yaml:13
# Loading collection ansible.utils from /Users/amhatre/ansible-collections/collections/ansible_collections/ansible/utils
# ok: [localhost] => {
#     "msg": {
#         "netconf-state": {
#             "@xmlns": "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring",
#             "schemas": {
#                 "schema": null
#             }
#         }
#     }
# }

Authors

  • Ashwini Mhatre (@amhatre)