Bootstrap FreeKB - Ansible - ChangeĀ or remove a line in a file using the replace module
Ansible - ChangeĀ or remove a line in a file using the replace 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
  • 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 (this article)

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 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": "Path /tmp/foo.txt does not exist !",
  "rc": 257
}

 

Let's say /tmp/foo.txt contains the following text.

Hello World
Earth Hello

 

In this example, Hello will be replaced with Goodbye in foo.txt, register is used to capture the output, and debug is used to display the output.

---
- hosts: localhost
  tasks:
  - name: replace Hello with Goodbye in foo.txt
    ansible.builtin.replace:
      path: /tmp/foo.txt
      regexp: Hello
      replace: Goodbye
      backup: true
    register: out

  - ansible.builtin.debug:
      var: out
...

 

If no replacements were made, something like this should be returned.

ok: [localhost] => {
    "out": {
        "changed": false, 
        "failed": false, 
        "msg": ""
    }
}

 

If one or more replacements were made, something like this should be returned.

ok: [localhost] => {
    "out": {
        "changed": true, 
        "failed": false, 
        "msg": "2 replacements made"
    }
}

 

foo.txt should now have the following.

Goodbye World
Earth Goodbye

 

And a backup of the original file should have been created, where the backup file is named <original file name>.xxxxx.yyyy-mm-dd@hh:mm:ss~.

 

Remove

Let's say /tmp/foo.txt contains the following.

Line 1
Line 2
Line 3

 

In this example, every match of "World" followed by anything in foo.txt will be removed.

---
- hosts: all
  tasks:
  - name: remove 'Line 2' from foo.txt
    ansible.builtin.replace:
      path: /tmp/foo.txt
      regexp: Line 2.*[\n|\r|\t|\s]
...

 

Now /tmp/foo.txt should look like this.

Line 1
Line 3

 

regular expression (regexp)

Regular expressions can be used. In this example, only lines beginning with Hello will be replaced with Goodbye.

---
- hosts: all
  tasks:
  - name: replace lines beginning with Hello with Goodbye in foo.txt
    ansible.builtin.replace:
      path: /tmp/foo.txt
      regexp: ^Hello
      replace: Goodbye
...

 

If the file was successfully updated, the play should indicate changed.

TASK [replace lines beginning with 'Hello' followed by anything with 'World' in foo.txt]
changed: [server1.example.com]

 

If the file was not updated, the play should indicate ok. This is the expected behavior if there are no lines to replace in the file.

TASK [replace lines beginning with 'Hello' followed by anything with 'World' in foo.txt]
ok: [server1.example.com]

 

Here is a bit of a more realistic example, where lines in /etc/security/limits.conf are updated.

---
- hosts: all
  tasks:
  - name: update lines in /etc/security/limits.conf
    ansible.builtin.replace:
      path: /etc/security/limits.conf
      regexp: "{{ item.regexp }}"
      replace: "{{ item.replace }}"
    with_items:
    - { "regexp": "\\*.*soft.*nofile.*", "replace": "*\tsoft\tnofile\t2147" }
    - { "regexp": "\\*.*soft.*nproc.*",  "replace": "*\tsoft\tnproc\t8192"   }
    - { "regexp": "\\*.*hard.*nofile.*", "replace": "*\thard\tnofile\t65536"  }
    - { "regexp": "\\*.*hard.*nproc.*",  "replace": "*\thard\tnproc\t16684"  }
...

 




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 650f10 in the box below so that we can be sure you are a human.