问题
I have a file filedets.yml which has many IP addresses and the file details listed under each IP. Below example shows just 3 entries however, expect this file to have many more entries of IP and file details. cat filedets.yml:
---
10.9.9.111:
/tmp/test.jar:
hash: e6df90d38fa86f0e289f73d79cd2cfd2a29954eb
/tmp/best.jar:
hash: e6df90d38fa86f0e289f73d79cd2cfd2a29954eb
10.8.8.44:
/tmp/conf/extra/httpd-ssl.conf:
hash: 1746f03d57491b27158b0d3a48fca8b5fa85c0c2
/tmp/conf/httpd.conf:
hash: 1746f03d57491b27158b0d3a48fca8b5fa85c0c2
10.7.7.37:
/tmp/conf/extra/httpd-ssl.conf:
hash: 1746f03d57491b27158b0d3a48fca8b5fa85c0c2
/tmp/conf/extra/web.xml:
hash: 1746f03d57491b28753j39983308igfgja8b5fa85
/tmp/conf/httpd.conf:
hash: 1746f03d57491b27158b0d3a48fca8b5fa85c0c2
and so on .....
Given an IP address say 10.8.8.44
I wish to remove the IP and all its file details from filedets.yml. Thus the below entry should be removed from the file.
10.8.8.44:
/tmp/conf/extra/httpd-ssl.conf:
hash: 1746f03d57491b27158b0d3a48fca8b5fa85c0c2
/tmp/conf/httpd.conf:
hash: 1746f03d57491b27158b0d3a48fca8b5fa85c0c2
The below regex helps me search for the text that needs to be deleted.
grep -Pzo '[^#](^10.8.8.44::)(.|\n)*^(?!( |\n))' /app/filedets.yml
I tried to use state: absent
attribute of lineinfile
and blockinfile
modules with the above regex in my ansible-playbook inorder to remove the desired entry from filedets.yml
Here is my playbook:
- name: "Remove entry from file."
lineinfile:
path: "/app/filedets.yml"
regexp: "[^#](^10.8.8.44:)(.|\n)*^(?!( |\n))"
state: absent
Unfortunately this does not work and the entries do not get removed from the filedets.yml.
Can you please suggest how can I get this to work ?
来源:https://stackoverflow.com/questions/59948370/remove-contents-of-a-include-vars-file-using-ansible