The following task always triggers a notify
The first time it runs ansible applies the change which is expected, and the line is changed. If I run it again, ansible considers it as "changed", even though the regex cannot possibly match, since the line has become "bind-address = 0.0.0.0"
why ?
- name: Ensure MySQL will listen on all ip interfaces (bind to 0.0.0.0)
lineinfile: dest=/etc/mysql/my.cnf
regexp='bind-address\s*=\s*127\.0\.0\.1\s*'
line='bind-address = 0.0.0.0'
state=present
insertafter=EOF
notify: restart mysql
In Ansible, a handler refers to a particular task that executes when triggered by the notify module. Handlers perform an action defined in the task when a change occurs in the remote host.
The lineinfile is one of the most powerful modules in the Ansible toolbox. Ansible lineinfile module is used to insert a line, modify, remove, and replace an existing line.
Refer to the backrefs
option of the lineinfile
module. Specifically, "if the regexp doesn't match anywhere in the file, the file will be left unchanged." A working play would look like this:
- name: Ensure MySQL will listen on all ip interfaces (bind to 0.0.0.0)
lineinfile: dest=/etc/mysql/my.cnf
regexp='bind-address\s*=\s*127\.0\.0\.1\s*'
line='bind-address = 0.0.0.0'
state=present
backrefs=yes
notify: restart mysql
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