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

Let's say your managed nodes (e.g. target systems) contain /tmp/foo.txt, and foo.txt contains the following markup.

Hello

 

The replace module can be used to replace "Hello". In this example, every line beginning with Hello in /tmp/foo.txt will be updated to contain just the text World.

- name: edit foo.txt
  replace:
    path: /tmp/foo.txt
    regexp: "^Hello.*"
    replace: "World"

 

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

TASK [edit 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 [edit foo.txt]
ok: [server1.example.com]

 

The delegate_to module can be used to modify a file on the control node (that's your Ansible server), like this. In this example, the /tmp/foo.txt file on the control node will be modified.

- name: edit foo.txt
  replace:
    path: /tmp/foo.txt
    regexp: "^Hello.*"
    replace: "World"
  delegate_to: localhost

 



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 938e1 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   |