title: Ansible lineinfile duplicates line
body: <p>I have a simple file at /etc/foo.txt. The file contains the following:</p>
  
  <pre><code>#bar
  </code></pre>
  
  <p>I have the following ansible playbook task to uncomment the line above:</p>
  
  <pre><code>- name: test lineinfile
    lineinfile: backup=yes state=present dest=/etc/foo.txt
                regexp='^#bar'
                line='bar'
  </code></pre>
  
  <p>When I first run ansible-playbook, the line gets uncommented and the /etc/foo.txt now contains the following:</p>
  
  <pre><code>bar
  </code></pre>
  
  <p>However, if I run ansible-playbook again, I get the following:</p>
  
  <pre><code>bar
  bar
  </code></pre>
  
  <p>If I run it yet again, then the /etc/foo.txt file will look like this:</p>
  
  <pre><code>bar
  bar
  bar
  </code></pre>
  
  <p>How to avoid this duplications of lines? I just want to uncomment the '#bar' and be done with it.</p>