42

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 above:

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

When I first run ansible-playbook, the line gets uncommented and the /etc/foo.txt now contains the following:

bar

However, if I run ansible-playbook again, I get the following:

bar
bar

If I run it yet again, then the /etc/foo.txt file will look like this:

bar
bar
bar

How to avoid this duplications of lines? I just want to uncomment the '#bar' and be done with it.

zupo
  • 1,556
  • 1
  • 13
  • 17
  • Use [replace](https://docs.ansible.com/ansible/latest/modules/replace_module.html#replace-replace-all-instances-of-a-particular-string-in-a-file-using-a-back-referenced-regular-expression) to "`just uncomment the '#bar' and be done with it`". – Vladimir Botka Jun 09 '19 at 17:03

4 Answers4

73

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.

anatoly techtonik
  • 19,847
  • 9
  • 124
  • 140
59

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.

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
joemaller
  • 19,579
  • 7
  • 67
  • 84
  • 1
    But that always changes the file. My understanding was that the line only gets inserted (file modified) when the regexp matched. – jdevora Nov 15 '16 at 01:04
  • As long as the file comes out the same every time, the task is idempotent. That's all that matters. – joemaller Nov 15 '16 at 23:03
  • 1
    I found out the hard way that if you use `insertafter` in combination with this, you end up with duplicate lines again. `insertafter` breaks this formula. – Dale C. Anderson Feb 08 '18 at 23:44
3

See https://github.com/ansible/ansible/issues/4531.

The solution is to not replace the commented out line, but to add an additional line, while keeping the original there.

zupo
  • 1,556
  • 1
  • 13
  • 17
0

For those of you who came here after looking at their file full of duplicate lines: a simple way to remove the duplicates and get back to a "good state" is, to use "state=absent" with the orginal regex.

For Example:

- name: clean KexAlgorithms
  lineinfile: dest=/etc/ssh/sshd_config
              regexp="^KexAlgorithms"
              state=absent

And then just re-adding them correctly.

Martin
  • 1