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.

1
  • Use replace to "just uncomment the '#bar' and be done with it". Jun 9, 2019 at 17:03

4 Answers 4

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.

0
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.

3
  • 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, 2016 at 1:04
  • As long as the file comes out the same every time, the task is idempotent. That's all that matters.
    – joemaller
    Nov 15, 2016 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. Feb 8, 2018 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.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.