What is Ansible XML?

What is Ansible XML?
Techiio-author
Written by Nilima PaulFebruary 7, 2022
7 min read
Ansible
2 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 2 VIEWS SHARE
Techiio-author
Nilima Paul

Technology Security Analyst

In this blog, We will discuss Ansible XML.

About Ansible XML

Ansible XML module, which is a part of the General ansible collection module and it supports the various parameters (described in the syntax section) to read the content of the XML file, add or remove the child elements or values, print the specific child attributes or the nodes, change the attribute values, etc.

How does XML work in Ansible?

To manage the XML records, we want the ansible XML module, and it doesn't accompany the ansible default establishment, however, we want to introduce it. It is accessible as a piece of the Ansible people group.

To introduce the XML module, we can utilize the beneath order. This will introduce the XML for both the Unix and the Windows working frameworks.

ansible-cosmic system assortment introduces community. general

For the Windows working framework, you can utilize the win_xml module, and for the non-windows target, you can utilize the community.general.xml module

Whenever you work with the XML record, you want to furnish the xPath to manage the XML credits and the qualities. You can advance additional with regards to the xPath documentations from the sites beneath.

Examples

Let us discuss examples of Ansible XML.

Example #1: Ansible XML module to count the number of attribute nodes

In this playbook, we will count the total number of author nodes.

---
- name: Ansible playbook to count the XML nodes.
hosts: localhost
tasks:
- name: Count the books nodes.
xml:
path: /tmp/books.xml
xpath: /catalog/book/author
count: yes
register: hits
- ansible.builtin.debug:
var: hits.count

Output:

blogpost

Example #2: Remove the specific node with the attribute value

This playbook will remove all the nodes with the book id attribute bk101.

- name: task to remove the book id
xml:
path: /tmp/books.xml
xpath: /catalog/book[@id='bk101'] state: absent

Output:

blogpost

Example #3: Playbook to remove attributes

This playbook will remove all the matching attributes. In this case, it will remove the ID attribute.

- name: task to remove the all the id attributes.
xml:
path: /tmp/books.xml
xpath: /catalog/book/@id
state: absent

Output:

blogpost

Once you check the XML output, all the ids will be removed.

blogpost

Example #4: Adding the new element with the value

This playbook will add the new element “Newbook” with the value “fiction”.

- name: task to add the element to the xml.
xml:
path: /tmp/books.xml
xpath: /catalog/book[@id='bk101']/newbook
value: 'Fiction'

Output:

blogpost

Example #5: Playbook to change the attribute value

This playbook will modify the content of the genre attribute from Computer to ComputerTech.

- name: task to add the element to the xml.
xml:
path: /tmp/books.xml
xpath: /catalog/book[@id='bk101']/genre
value: 'ComputerTech'

Output:

blogpost

XML file output:

blogpost

Example #6: Ansible playbook to add multiple child elements

This playbook will add the multiple child elements to the specified attributes of the book node.

- name: task to add multiple child elements
xml:
path: /tmp/books.xml
xpath: /catalog/book[@id='bk101'] add_children:
- shop: modern
- owner: jack
- tag: '1001'

Output:

blogpost

XML file output:

blogpost

If you need to insert before the specific element, then use the insertbefore parameter. For example,

- name: task to add multiple child elements
xml:
path: /tmp/books.xml
xpath: /catalog/book[@id='bk101']/genre[text() = "Computer"] insertbefore: yes
add_children:
- shop: modern
- owner: jack
- tag: '1001'

XML Output:

blogpost

And for the insert after some attribute, you need to specify insertafter parameter.

- name: task to add multiple child elements
xml:
path: /tmp/books.xml
xpath: /catalog/book[@id='bk101']/genre
insertafter: yes
add_children:
- shop: modern
- owner: jack
- tag: '1001'

Conclusion

XML document is utilized by the different sites, programming, setups, and so forth and ansible uses the different boundaries to peruse the substance, duplicate the XML record, control the XML record according to the necessity which assists when you with arranging any site or programming utilizing XML then you can utilize the builtin module to work with the XML record.

Ansible
Ansible XML
ansib facts
2 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 2 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-author
Nilima Paul
Technology Security Analyst
Techiio-followerTechiio-followerTechiio-follower
201 Blog Posts
0 Discussion Threads
Trending Technologies
15
Software40
DevOps47
Frontend Development24
Backend Development20
Server Administration17
Linux Administration26
Data Center24
Sentry24
Terraform23
Ansible83
Docker70
Penetration Testing16
Kubernetes21
NGINX20
JenkinsX17
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance