Ansible lineinfile duplicates line

You need to add backrefs=yes if you don’t want to change your regular expression.

- name: test lineinfile
  lineinfile: backup=yes state=present dest=/etc/foo.txt
              regexp='^#bar' backrefs=yes
              line="bar"

This changes the behavior of lineinfile from:

 find
 if found
   replace line found
 else
   add line

to:

 find
 if found
   replace line found

In other words, this makes operation idempotent.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)