Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 963
Chris F Avatar asked Sep 27 '16 13:09

Chris F


People also ask

What is blockinfile in Ansible?

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

What is Blockinfile?

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.

Which module will insert update remove multi line text in a file?

blockinfile module – Insert/update/remove a text block surrounded by marker lines.


1 Answers

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.

like image 111
Konstantin Suvorov Avatar answered Sep 22 '22 16:09

Konstantin Suvorov