Convert given JSON string to XML
Version added: 2.0.2
- This plugin converts the JSON string to XML.
- Using the parameters below-
data|ansible.utils.to_xml
#### Simple examples with out any engine. plugin will use default value as xmltodict
- name: Define JSON data
ansible.builtin.set_fact:
data:
"interface-configurations":
"@xmlns": "http://cisco.com/ns/yang/Cisco-IOS-XR-ifmgr-cfg"
"interface-configuration":
- debug:
msg: "{{ data | ansible.utils.to_xml }}"
# TASK [Define JSON data ] *************************************************************************
# task path: /Users/amhatre/ansible-collections/playbooks/test_utils_json_to_xml.yaml:5
# ok: [localhost] => {
# "ansible_facts": {
# "data": {
# "interface-configurations": {
# "@xmlns": "http://cisco.com/ns/yang/Cisco-IOS-XR-ifmgr-cfg",
# "interface-configuration": null
# }
# }
# },
# "changed": false
# }
#
# TASK [debug] ***********************************************************************************************************
# task path: /Users/amhatre/ansible-collections/playbooks/test_utils_json_to_xml.yaml:13
# Loading collection ansible.utils from /Users/amhatre/ansible-collections/collections/ansible_collections/ansible/utils
# ok: [localhost] => {
# "msg": "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<interface-configurations xmlns=\"http://cisco.com/ns/yang/
# Cisco-IOS-XR-ifmgr-cfg\">\n\t<interface-configuration></interface-configuration>\n</interface-configurations>"
# }
#### example2 with engine=xmltodict
- name: Define JSON data
ansible.builtin.set_fact:
data:
"interface-configurations":
"@xmlns": "http://cisco.com/ns/yang/Cisco-IOS-XR-ifmgr-cfg"
"interface-configuration":
- debug:
msg: "{{ data | ansible.utils.to_xml('xmltodict') }}"
# TASK [Define JSON data ] *************************************************************************
# task path: /Users/amhatre/ansible-collections/playbooks/test_utils_json_to_xml.yaml:5
# ok: [localhost] => {
# "ansible_facts": {
# "data": {
# "interface-configurations": {
# "@xmlns": "http://cisco.com/ns/yang/Cisco-IOS-XR-ifmgr-cfg",
# "interface-configuration": null
# }
# }
# },
# "changed": false
# }
# TASK [debug] ***********************************************************************************************************
# task path: /Users/amhatre/ansible-collections/playbooks/test_utils_json_to_xml.yaml:13
# Loading collection ansible.utils from /Users/amhatre/ansible-collections/collections/ansible_collections/ansible/utils
# ok: [localhost] => {
# "msg": "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<interface-configurations xmlns=\"http://cisco.com/ns/yang/
# Cisco-IOS-XR-ifmgr-cfg\">\n\t<interface-configuration></interface-configuration>\n</interface-configurations>"
# }
#### example3 with indent='spaces' and indent_width=2
- name: Define JSON data
ansible.builtin.set_fact:
data:
"interface-configurations":
"@xmlns": "http://cisco.com/ns/yang/Cisco-IOS-XR-ifmgr-cfg"
"interface-configuration":
- debug:
msg: "{{ data | ansible.utils.to_xml(indent='spaces', indent_width=2) }}"
# TASK [Define JSON data ] *************************************************************************
# task path: /Users/amhatre/ansible-collections/playbooks/test_utils_json_to_xml.yaml:5
# ok: [localhost] => {
# "ansible_facts": {
# "data": {
# "interface-configurations": {
# "@xmlns": "http://cisco.com/ns/yang/Cisco-IOS-XR-ifmgr-cfg",
# "interface-configuration": null
# }
# }
# },
# "changed": false
# }
# TASK [debug] ***********************************************************************************************************
# task path: /Users/amhatre/ansible-collections/playbooks/test_utils_json_to_xml.yaml:13
# Loading collection ansible.utils from /Users/amhatre/ansible-collections/collections/ansible_collections/ansible/utils
# ok: [localhost] => {
# "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<interface-configurations xmlns=\"http://cisco.com/ns/yang/
# Cisco-IOS-XR-ifmgr-cfg\">\n <interface-configuration></interface-configuration>\n</interface-configurations>"
# }
- Ashwini Mhatre (@amhatre)
Hint
Configuration entries for each entry type have a low to high priority order. For example, a variable that is lower in the list will override a variable that is higher up.