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, the register parameter can be used to capture the ouptut of the replacment module, and the debug module is used to display the output.

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

  - 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
    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.

- name: replace lines beginning with Hello with Goodbye in foo.txt
  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]

 

 




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