Chris F
Chris F

Reputation: 16685

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?

Upvotes: 5

Views: 4190

Answers (2)

Chloé Boucher
Chloé Boucher

Reputation: 1

Here is a concrete example for completing what is said in the previous answer:

- name: "Example on how to remove a block in a file"    
  blockinfile:
  path: "example/test.sh"
  state: absent
  marker_begin: 'line to remove 1'
  marker_end: 'line to remove 4'
  marker: "{mark}"
  block: |
    line to remove 2
    line to remove 3

Upvotes: 0

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68269

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.

Upvotes: 7

Related Questions