ansibleansible-2.x

Why does blockinfile with state=absent not work for these lines?


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?


Solution

  • 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.