19

I'm new to ansible, however I was able to successfully use the blockinfile function. However I can't figure out how to prevent markers from being inserted. I read the documentation but it didn't appear to describe it.

This is what shows up in every file that blockinfile writes to.

# BEGIN ANSIBLE MANAGED BLOCK
# END ANSIBLE MANAGED BLOCK
udondan
  • 2,061
  • 15
  • 18
Nik
  • 197
  • 1
  • 1
  • 4

3 Answers3

16

The markers are actually used to identify the block.

That means if you remove the markers, blockinfile will no longer be able to identify the block, which will result in the module to add the block to the file every time you run the task.

So the markers before and after the written block are a requirement by the module and cannot be removed.

If this is a one-time playbook that will never be executed again you could run the lineinfile module with the state: absent option afterwards.

udondan
  • 2,061
  • 15
  • 18
  • 1
    Sorry about the incomplete statement above, someone knocked at the door and I had to run. I tried these ideas yesterday. marker: "" - Adds a line to the top and the bottom of the file. False: - Just adds the word false at the bottom and top of the file. Any other ideas? – Nik Feb 13 '16 at 19:36
  • 2
    I updated my answer. The markers actually are required by gthe module itself and even if you could, you probably do not want to remove them. See updated answer for details. – udondan Feb 14 '16 at 02:22
2

i did something like this...

- name: Insert someline in somefile.sh
    blockinfile:
      path: /usr/share/somefile.sh
      block: -sometext
      insertafter: '-XX:originaltext'
      marker: ""
      backup: yes


 - name: Remove blank lines blockinfile put in
    lineinfile :
     path: /usr/share/somefile.sh
     state: absent
     regexp: '^$'
  • marker "" will insert blank lines into the file
  • lineinfile will remove them (along with all other blank lines) so beware !!
user489843
  • 21
  • 1
-2

Fair enough, I ended up using the following to create multiple lines.

https://stackoverflow.com/questions/24334115/ansible-lineinfile-for-several-lines

Thanks @udondan

Nik
  • 197
  • 1
  • 1
  • 4