Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this ansible lineinfile task always trigger a notify?

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
like image 623
Max L. Avatar asked Feb 15 '14 07:02

Max L.


People also ask

How does notify work Ansible?

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.

What is Lineinfile module in Ansible?

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.


1 Answers

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
like image 128
Ben Whaley Avatar answered Nov 07 '22 19:11

Ben Whaley