I have a file with the following block of text in it. I have text before and after the text block
other_user:
hash: JKJ;LKJA;LDKJF;LKJA;LKJIUR;JFKLJDQPIRQKJ;LKFJPOQJ
#password is: some_pw0
logstash:
hash: $fj;kdjjfajf;ajKFJ;dfj;dkfja;dfjFJ:LFJj;kj;lfkajs
#password is: some_pw
other_user1:
hash: JJKLJDRKJOPIQMVOIUROIJFAUROJJFIUQWKERJJFKQURJAKDJ
#password is: some_pw1
I'm trying to remove the block for the logstash user using this code, but it does NOT remove it.
- name: Delete existing logstash user
blockinfile:
dest: /path_to_file/foo.yml
state: absent
block: |
logstash:
hash: $fj;kdjjfajf;ajKFJ;dfj;dkfja;dfjFJ:LFJj;kj;lfkajs
#password is: some_pw
I expect the result to be:
other_user:
hash: JKJ;LKJA;LDKJF;LKJA;LKJIUR;JFKLJDQPIRQKJ;LKFJPOQJ
#password is: some_pw0
other_user1:
hash: JJKLJDRKJOPIQMVOIUROIJFAUROJJFIUQWKERJJFKQURJAKDJ
#password is: some_pw1
What am I missing?
Ansible blockinfile module, which is a part of the ansible-base and comes with the default installation of the ansible, which is responsible to insert, update or remove a block of lines (multi-line text) from the file on the remote nodes, and these blocks are surrounded by the marker like begin and end which can be a ...
Ansible blockinfile module is used to insert/update/remove a block of lines. The block will be surrounded by a marker, like begin and end, to make the task idempotent.
blockinfile module – Insert/update/remove a text block surrounded by marker lines.
There's a thing about blockinfile
. Pay close attention at the description:
This module will insert/update/remove a block of multi-line text surrounded by customizable marker lines.
The default value for markers: # {mark} ANSIBLE MANAGED BLOCK
where mark is BEGIN
/END
.
So if there are no markers in the file, module will treat it as no block found.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With