Search code examples
bashreplaceansibleyamlinline

Ansible - only replace string if not what I am expecting


So let's say I have a bash script and inside that bash script I have these parameters

Sleep=45
Awake=52
Hello=69

What I want to do is in my ansible playbook have a task that looks at this file and if is see's that sleep is not equal to 45 or awake is not equal to 52 or hello is not equal to 69 it corrects it.

I know you can use the replace inline module but not sure if using that would be the best way of doing things


Solution

  • Try using the Ansible lineinfile module. You can attest that a certain line should be there, and if not, any line matching a regex (say Hello=.*) will be replaced with the correct value (here Hello=69). No change will be made if the line is already the correct value.

    - lineinfile:
        path: /tmp/example
        regexp: '^Sleep='
        line: 'Sleep=45'