I am attempting to comment out below line using Ansible. I am running into an issue where a comment keeps getting added every time the playbook is run.
How can I change my regexp to ignore the line if already commented out?
- replace:
path: /etc/rsyslog.conf
regexp: '(.*@hostname.*)'
replace: '#\1'
notify:
- Restart rsyslog
You will need a negative look-ahead for this, to exclude line starting with a sharp (#
).
So, your regex should be ^(?!#)(.*@hostname.*)
and your task would end up being:
- replace:
path: /etc/rsyslog.conf
regexp: '^(?!#)(.*@hostname.*)'
replace: '#\1'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With