# Adding a nested xml element using ansible xml module fails

**URL:** <https://forum.ansible.com/t/adding-a-nested-xml-element-using-ansible-xml-module-fails/27138>\
**Category:** Ansible Project\
**Tags:** ubuntu\
**Created:** [March 28, 2018, 5:26am UTC](https://forum.ansible.com/t/adding-a-nested-xml-element-using-ansible-xml-module-fails/27138 "2018-03-28T05:26:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Uvindra\_Dias\_Jayasin](https://avatars.discourse-cdn.com/v4/letter/u/a8b319/32.png) [@Uvindra\_Dias\_Jayasin](https://forum.ansible.com/u/Uvindra_Dias_Jayasin)\
**Post date:** [March 28, 2018, 5:26am UTC](https://forum.ansible.com/t/adding-a-nested-xml-element-using-ansible-xml-module-fails/27138/1 "2018-03-28T05:26:14Z")

</div>

Im using Ansible 2.5 on Ubuntu 17.10

Given the following xml file

Trying to add nested elements so that the file is changed to the following output fails.

Old Rasputin 2000

Here is the playbook I used

```auto
- name: Add several more beers with the year of manufacture nested
  xml:
    path: /foo/bar.xml
    xpath: /business/beers
    add_children:
    - beer: 
      - name: Old Rasputin
      - year: 2000

I see the following exception,

An exception occurred during task execution. To see the full traceback, use -vvv. The error was: TypeError: Argument must be bytes or unicode, got 'list'
fatal: [localhost]: FAILED! => {"changed": false, "module_stderr": "Traceback (most recent call last):\n File \"/tmp/ansible_sv9JSC/ansible_module_xml.py\", 
line 860, in <module>\n main()\n File \"/tmp/ansible_sv9JSC/ansible_module_xml.py\", line 840, in main\n    
add_target_children(module, doc, xpath, namespaces, add_children, input_type)\n File \"/tmp/ansible_sv9JSC/ansible_module_xml.py\", line 409, in add_target_children\n    
new_kids = children_to_nodes(module, children, in_type)\n File \"/tmp/ansible_sv9JSC/ansible_module_xml.py\", line 651, in children_to_nodes\n    
return [child_to_element(module, child, type) for child in children]\n File \"/tmp/ansible_sv9JSC/ansible_module_xml.py\", line 639, in child_to_element\n    
node.text = value\n File \"src/lxml/etree.pyx\", line 1030, in lxml.etree._Element.text. __set__ \n File \"src/lxml/apihelpers.pxi\", line 716, in lxml.etree._setNodeText\n  
File \"src/lxml/apihelpers.pxi\", line 704, in lxml.etree._createTextNode\n File \"src/lxml/apihelpers.pxi\", line 1442, in lxml.etree._utf8\nTypeError: Argument must be bytes or unicode, got 'list'\n", 
"module_stdout": "", "msg": "MODULE FAILURE", "rc": 1}

Is this supported or am I missing something here?

```

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [March 28, 2018, 1:06pm UTC](https://forum.ansible.com/t/adding-a-nested-xml-element-using-ansible-xml-module-fails/27138/2 "2018-03-28T13:06:39Z")

</div>

To my knowledge you can't create children's children in one task.  
You need to create beer first in one task and then name and year in a separate task.

---

<div class="post-metadata">

**Author:** ![Uvindra\_Dias\_Jayasin](https://avatars.discourse-cdn.com/v4/letter/u/a8b319/32.png) [@Uvindra\_Dias\_Jayasin](https://forum.ansible.com/u/Uvindra_Dias_Jayasin)\
**Post date:** [March 30, 2018, 4:55am UTC](https://forum.ansible.com/t/adding-a-nested-xml-element-using-ansible-xml-module-fails/27138/3 "2018-03-30T04:55:21Z")

</div>

Thanks Kai

---

<div class="post-metadata">

**Author:** ![Aliaa\_Yusuf](https://avatars.discourse-cdn.com/v4/letter/a/ba8739/32.png) [@Aliaa\_Yusuf](https://forum.ansible.com/u/Aliaa_Yusuf)\
**Post date:** [April 22, 2019, 3:46pm UTC](https://forum.ansible.com/t/adding-a-nested-xml-element-using-ansible-xml-module-fails/27138/4 "2019-04-22T15:46:32Z")

</div>

check this it worked with me  
[https://github.com/cmprescott/ansible-xml/issues/95](https://github.com/cmprescott/ansible-xml/issues/95)

بتاريخ الجمعة، 30 مارس، 2018 6:55:21 ص UTC+2، كتب Uvindra Dias Jayasinha:

---

<div class="post-metadata">

**Author:** ![Aliaa\_Yusuf](https://avatars.discourse-cdn.com/v4/letter/a/ba8739/32.png) [@Aliaa\_Yusuf](https://forum.ansible.com/u/Aliaa_Yusuf)\
**Post date:** [April 22, 2019, 3:47pm UTC](https://forum.ansible.com/t/adding-a-nested-xml-element-using-ansible-xml-module-fails/27138/5 "2019-04-22T15:47:52Z")

</div>

check this link it worked with me  
[https://github.com/cmprescott/ansible-xml/issues/95](https://github.com/cmprescott/ansible-xml/issues/95)

بتاريخ الأربعاء، 28 مارس، 2018 7:26:14 ص UTC+2، كتب Uvindra Dias Jayasinha:

---

<div class="post-metadata">

**Author:** ![Hasini\_P](https://avatars.discourse-cdn.com/v4/letter/h/b9bd4f/32.png) [@Hasini\_P](https://forum.ansible.com/u/Hasini_P)\
**Post date:** [September 16, 2021, 1:12pm UTC](https://forum.ansible.com/t/adding-a-nested-xml-element-using-ansible-xml-module-fails/27138/6 "2021-09-16T13:12:52Z")

</div>

Like add\_children, is there a solution for deletion of elements based on element value (elements with no attributes )

- name: Delete several beers with value  
xml:  
path: /foo/bar.xml  
xpath: /business/beers  
value: Old Rasputin  
state: absent

--------------------- Actually I am trying to delete an element in xml file with namespaces
