FreeKB - Ansible replace module (edit a file)
Ansible - replace module (edit a file)

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.

  • The blockinfile module can be used to append, change, or remove a block of lines in a file. 
  • The lineinfile module can be used to append, change, or remove a line in a file. 
  • The replace module can be used to change or remove a line 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 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.

- name: replace Hello with Goodbye in foo.txt
  replace:
    path: /tmp/foo.txt
    regexp: Hello
    replace: Goodbye

 

foo.txt should now have the following.

Goodbye World
Earth Goodbye

 

The register parameter can be used to capture the ouptut of the replacment module.

- name: replace Hello with Goodbye in foo.txt
  replace:
    path: /tmp/foo.txt
    regexp: Hello
    replace: Goodbye
  register: out

 

The debug module can then been used to display the output.

- debug:
    var: out

 

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

ok: [server1.example.com] => {
    "out": {
        "changed": false, 
        "failed": false, 
        "msg": ""
    }
}

 

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

ok: [server1.example.com] => {
    "out": {
        "changed": true, 
        "failed": false, 
        "msg": "2 replacements made"
    }
}

 


Remove

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

Hello World
Goodbye World

 

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

- name: remove 'World' from foo.txt
  replace:
    path: /tmp/foo.txt
    regexp: World

 

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

Hello
Goodbye

 

Or with a regular expression like this, lines beginning with Hello will be replaced with an empty line.

- name: remove 'World' from foo.txt
  replace:
    path: /tmp/foo.txt
    regexp: ^Hello.*\n

 

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

Goodbye World

 

And here is how empty lines can be removed.

- name: remove 'World' from foo.txt
  replace:
    path: /tmp/foo.txt
    regexp: ^\n

 


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]

 

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




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




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |