Bootstrap FreeKB - Ansible - Append, change, or remove a block of lines in a file using the blockinfile module
Ansible - Append, change, or remove a block of lines in a file using the blockinfile module

Updated:   |  Ansible articles

If you are not familiar with modules, check out Ansible - Getting Started with Modules.

There are a few different modules that can be used to modify a file.

  • blockinfile can be used to append, change, or remove a block of lines in a file (this article)
  • lineinfile can be used to append, change, or remove one or more lines in a file
  • replace can be used to change or remove one or more lines in a file.

AVOID TROUBLE

If the target file does not exist, invoking this playbook will return an error like the sample below.

For this reason, you will almost always want to use the create: true parameter, so that the file is created if it does not exist. Or use the stat module to determine if the file exists, and then used the when parameter to only execute the task when the file exists.

fatal: [server1.example.com]: FAILED! => {
  "ansible_facts": {
    "discovered_interpreter_python": "/usr/bin/python"
  },
  "changed": false,
  "msg": "Destination /tmp/foo.txt does not exist !",
  "rc": 257
}

 

At the bare minimum, the following is all that is needed to invoke this module. By default, the block is appended as a new line at the end of the file (EOF).

---
- hosts: all
  tasks:
  - name: append block 'Hello World' to /tmp/foo.txt
    ansible.builtin.blockinfile:
      path: /tmp/foo.txt
      block: |
        Hello
        World
...

 

When the target file does not exist, invoking this playbook will return an error. For this reason, you probably want to first use the stat module to determine if the target file exists, and then use the file module to create the target file, or use the when parameter to skip the lineinfile module if the target file does not exist. Or, you could use the ignore_errors parameter.

TASK [append 'World' to /tmp/foo.txt] 
fatal: [server1.example.com]: FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "msg": "Destination /tmp/foo.txt does not exist !", "rc": 257}

 

If the target file exists, and the file was successfully updated, the folllowing should be displayed.

TASK [append block 'Hello World' to /tmp/foo.txt]
changed: [server1.example.com]

 

foo.txt should now contain the following.

# BEGIN ANSIBLE MANAGED BLOCK
Hello
World
# END ANSIBLE MANAGED BLOCK

 

Notice that # BEGIN ANSIBLE MANAGED BLOCK and # END ANSIBLE MANAGED BLOCK were appended to foo.txt. These are the default marker lines. The following will produce the same exact result as above.

---
- hosts: all
  tasks:
  - name: append 'Hello World' to /tmp/foo.txt
    ansible.builtin.blockinfile:
      path: /tmp/foo.txt
      marker: "# {mark} ANSIBLE MANAGED BLOCK"
      block: |
        Hello
        World
...

 

You can create your own marker

---
- hosts: all
  tasks:
  - name: append 'Hello World' to /tmp/foo.txt
    ansible.builtin.blockinfile:
      path: /tmp/foo.txt
      marker_begin: START
      marker_end: END
      marker: "# {mark} MY MARKER"
      block: |
        Hello
        World
...

 

When the block parameter is empty or not included, the marker lines and everything inside the marker lines will be removed. Notice the marker parameter is not used here. When the default marker lines are being used, the marker parameter is not needed to remove everything inside the marker lines.

---
- hosts: all
  tasks:
  - name: remove marker lines from /tmp/foo.txt
    ansible.builtin.blockinfile:
      path: /tmp/foo.txt
...

 

Or, when the state parameter is absent, everything inside the marker lines will be removed.

---
- hosts: all
  tasks:
  - name: remove marker lines from /tmp/foo.txt
    ansible.builtin.blockinfile:
      path: /tmp/foo.txt
      state: absent
...

 

If you have custom marker lines, then the marker parameter will need to be included.

---
- hosts: all
  tasks:
  - name: remove marker lines from /tmp/foo.txt
    ansible.builtin.blockinfile:
      path: /tmp/foo.txt
      marker: "# {mark} MY MARKER"
...

 

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 619bce in the box below so that we can be sure you are a human.