Blockinfile - how to delete text

1,230 views
Skip to first unread message

Anfield

unread,
Aug 31, 2017, 12:24:15 PM8/31/17
to Ansible Project
I have this text file and I was trying to delete some text that I had just added. Not sure how I would go about deleting

1) The entire text tree and the wrappers #BEGIN ANSIBLE MANAGED BLOCK

2) Just deleting the text tree

Can anyone explain? Thanks

The delete functionality is not functioning as expected
(ansible 2.3.0)

textfile - 
giraffe
This is my text to add
Noooooooooooooo
penguin
robot

#BEGIN ANSIBLE MANAGED BLOCK
tree
#BEGIN ANSIBLE MANAGED BLOCK

Tried the following playbook and it just deleted the last wrapper

Playbook
---
- hosts: local
  tasks:
    - name: Use blockinfile
      blockinfile:
        path: /home/ansible/blockexample.txt
        marker: "#BEGIN ANSIBLE MANAGED BLOCK"
        block :
           tree
        state: absent

Outcome - 

giraffe
This is my text to add
Noooooooooooooo
penguin
robot

#BEGIN ANSIBLE MANAGED BLOCK
tree




Kai Stian Olstad

unread,
Aug 31, 2017, 2:07:02 PM8/31/17
to ansible...@googlegroups.com
On torsdag 31. august 2017 18.24.14 CEST Anfield wrote:
> textfile -
>
> giraffe
> This is my text to add
> Noooooooooooooo
> penguin
> robot
>
> #BEGIN ANSIBLE MANAGED BLOCK
> tree
> #BEGIN ANSIBLE MANAGED BLOCK
>
>
> Tried the following playbook and it just deleted the last wrapper
>
> Playbook
>
> ---
> - hosts: local
> tasks:
> - name: Use blockinfile
> blockinfile:
> path: /home/ansible/blockexample.txt
> marker: "#BEGIN ANSIBLE MANAGED BLOCK"
> block :
> tree
> state: absent

The problem is that the start marker and end marker is the same and
blockinfile doesn't now where to start and stop.

If you take a look at the blockinfile documentation under marker you will see
that it contain the template sign {mark} that will be changed for START or END

That way the start and end marker differ.

Just run your test without marker: and you'll see how it work since marker has
a default value that only need to be changed if your are using more than one
blockinfile on a file.


--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages