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

The replace module can be used to replace markup in a file on the managed node (e.g. the target system). Or, the following can be used to invoke this module on the control node (that's your Ansible server). 

The ansible-doc replace command can be used to show the Ansible documention on the replace module.

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

Hello World
Earth Hello

 

In this example, Hello will be replaced with Goodby 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

 


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