I have a simple file at /etc/foo.txt. The file contains the following:
#bar
I have the following ansible playbook task to uncomment the line ab
The problem is the task's regex only matches the commented out line, #bar
. To be idempotent, the lineinfile task needs to match both the commented and uncommented state of the line. This way it will uncomment #bar
but will pass bar
unchanged.
This task should do what you want:
- name: test lineinfile
lineinfile:
backup=yes
state=present
dest=/etc/foo.txt
regexp='^#?bar'
line='bar'
Note the only change was adding a "?" to the regex.