Help: cant get Ansible blockinfile to work

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.

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

But judging from the content the best option is probably the xml module:
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

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

@Visser, sure.

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

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

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

   - name: Policy Cleanup
     xml:
       path: /opt/install_content/test/wlm.config.xml
       xpath: /WLMConfigFile/Policies/Policy[@name="DataClickPolicy"]
       state: absent

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

Nice

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.

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

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

Dick

(attachments)