# Help: cant get Ansible blockinfile to work

**URL:** <https://forum.ansible.com/t/help-cant-get-ansible-blockinfile-to-work/31561>\
**Category:** Ansible Project\
**Created:** [October 29, 2019, 1:11pm UTC](https://forum.ansible.com/t/help-cant-get-ansible-blockinfile-to-work/31561 "2019-10-29T13:11:23Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael3](https://avatars.discourse-cdn.com/v4/letter/m/ecccb3/32.png) [@Michael3](https://forum.ansible.com/u/Michael3)\
**Post date:** [October 29, 2019, 1:11pm UTC](https://forum.ansible.com/t/help-cant-get-ansible-blockinfile-to-work/31561/1 "2019-10-29T13:11:23Z")

</div>

Hello,

I am having a challenge removing a block of lines in a configuration file after an uninstall using Ansible module blockinfile. I have tried multiple variations(of indentation) after block: | but they’re all a miss. In the configuration file, the first line is preceded by 2 tabs(8spaces) and this appears to be what I’m not getting right in my code

- name: Policy Cleanup

blockinfile:  
path: /opt/install\_content/test/wlm.config.xml  
state: absent  
block: |

Pls help.

---

<div class="post-metadata">

**Author:** ![The\_Vandyy\_Vines](https://avatars.discourse-cdn.com/v4/letter/t/7993a0/32.png) [@The\_Vandyy\_Vines](https://forum.ansible.com/u/The_Vandyy_Vines)\
**Post date:** [October 29, 2019, 1:14pm UTC](https://forum.ansible.com/t/help-cant-get-ansible-blockinfile-to-work/31561/2 "2019-10-29T13:14:07Z")

</div>

I think you need to add , regex for better filtering the lines to be removed.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [October 29, 2019, 1:17pm UTC](https://forum.ansible.com/t/help-cant-get-ansible-blockinfile-to-work/31561/3 "2019-10-29T13:17:28Z")

</div>

But judging from the content the best option is probably the xml module:  
[https://docs.ansible.com/ansible/latest/modules/xml\_module.html](https://docs.ansible.com/ansible/latest/modules/xml_module.html)

Once you've set that up properly, it's way more robust and will do  
exactly what you're looking for.  
It does depend on the entire xml file though - if you can post that?

Dick

---

<div class="post-metadata">

**Author:** ![Michael3](https://avatars.discourse-cdn.com/v4/letter/m/ecccb3/32.png) [@Michael3](https://forum.ansible.com/u/Michael3)\
**Post date:** [October 29, 2019, 1:23pm UTC](https://forum.ansible.com/t/help-cant-get-ansible-blockinfile-to-work/31561/4 "2019-10-29T13:23:21Z")

</div>

Right, i thought so too, and so one of the variations i tried, with and without single and double quotes, was:

- name: Policy Cleanup  
blockinfile:  
path: /opt/install\_content/test/wlm.config.xml  
state: absent  
block: |  
\s\*\n\s\*\n\s\*\<Distribution name=“DataClick” value=“100” /\>\n\s\*\</Allocation\>\n\s\*\</Policy\>

This did not work. Except I’m not specifying it correctly

---

<div class="post-metadata">

**Author:** ![Michael3](https://avatars.discourse-cdn.com/v4/letter/m/ecccb3/32.png) [@Michael3](https://forum.ansible.com/u/Michael3)\
**Post date:** [October 29, 2019, 1:39pm UTC](https://forum.ansible.com/t/help-cant-get-ansible-blockinfile-to-work/31561/5 "2019-10-29T13:39:02Z")

</div>

@Visser, sure.

\<?xml version="1.0" encoding="UTF-8" ?\>

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [October 29, 2019, 2:32pm UTC](https://forum.ansible.com/t/help-cant-get-ansible-blockinfile-to-work/31561/6 "2019-10-29T14:32:27Z")

</div>

Thanks - but it looks like your XML file has accidentally been truncated?  
I'd expect a closing WLMConfigFile tag...

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [October 29, 2019, 2:48pm UTC](https://forum.ansible.com/t/help-cant-get-ansible-blockinfile-to-work/31561/7 "2019-10-29T14:48:07Z")

</div>

OK assuming your file is valid XML and is not missing the above  
mentioned end tag, try this:

&nbsp;&nbsp;&nbsp;- name: Policy Cleanup  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xml:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: /opt/install\_content/test/wlm.config.xml  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xpath: /WLMConfigFile/Policies/Policy[@name="DataClickPolicy"]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: absent

Note that you'd have to "pip install lxml" or equivalent for the xml  
module to work.

---

<div class="post-metadata">

**Author:** ![The\_Vandyy\_Vines](https://avatars.discourse-cdn.com/v4/letter/t/7993a0/32.png) [@The\_Vandyy\_Vines](https://forum.ansible.com/u/The_Vandyy_Vines)\
**Post date:** [October 29, 2019, 4:55pm UTC](https://forum.ansible.com/t/help-cant-get-ansible-blockinfile-to-work/31561/8 "2019-10-29T16:55:31Z")

</div>

Nice

---

<div class="post-metadata">

**Author:** ![Michael3](https://avatars.discourse-cdn.com/v4/letter/m/ecccb3/32.png) [@Michael3](https://forum.ansible.com/u/Michael3)\
**Post date:** [October 29, 2019, 6:43pm UTC](https://forum.ansible.com/t/help-cant-get-ansible-blockinfile-to-work/31561/9 "2019-10-29T18:43:58Z")

</div>

Visser, you are right, i truncated the file but i am including the complete file below this reply.

Using the solution you provided got me somewhere but i noticed that some changes in the file with respect to change from double to single quotes and character spacing. Here is a screenshot of the file after running the playbook compared to the original file on the right. I highlighted the few changes.

I am not experienced with xml and not sure what effect this would have.

 ![img.JPG](https://us1.discourse-cdn.com/ansible/original/2X/5/58e72301af5acfcd7067d0bd9f54b2ef9719bee1.jpeg)

\<?xml version='1.0' encoding='UTF-8'?\>

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [October 29, 2019, 9:29pm UTC](https://forum.ansible.com/t/help-cant-get-ansible-blockinfile-to-work/31561/10 "2019-10-29T21:29:12Z")

</div>

Those changes are to ensure compatibility and consistency, and are harmless.

Dick

> **(attachments)**
>
> ![img.JPG](https://us1.discourse-cdn.com/ansible/original/2X/5/58e72301af5acfcd7067d0bd9f54b2ef9719bee1.jpeg)
