<p dir="auto">From <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/ekini/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/ekini">@ekini</a> on 2014-09-30T01:23:55Z</p>
<h5 dir="auto">Issue Type:</h5>
<p dir="auto">Feature Idea</p>
<h5 dir="auto">Component Name:</h5>
<p dir="auto">lineinfile module</p>
<h5 dir="auto">Ansible Version:</h5>
<p dir="auto">All versions.</p>
<h5 dir="auto">Environment:</h5>
<p dir="auto">Ubuntu 14.04, and all other.</p>
<h5 dir="auto">Summary:</h5>
<p dir="auto">The module "lineinfile" should have an option to remove duplicate lines matched by regexp.<br>
Currently it changes only the last line.</p>
<h5 dir="auto">Steps To Reproduce:</h5>
<p dir="auto">Let's say that we have the following lines in sshd_config (don't ask me how I got this):</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="PasswordAuthentication yes
# some stuff
PasswordAuthentication yes"><pre class="notranslate"><code class="notranslate">PasswordAuthentication yes
# some stuff
PasswordAuthentication yes
</code></pre></div>
<p dir="auto">We run ansible as:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="$ansible -c local -m lineinfile -a 'dest=./sshd_config  regexp=&quot;^[#\s]*PasswordAuthentication.*&quot; line=&quot;PasswordAuthentication no&quot;' localhost"><pre class="notranslate"><code class="notranslate">$ansible -c local -m lineinfile -a 'dest=./sshd_config  regexp="^[#\s]*PasswordAuthentication.*" line="PasswordAuthentication no"' localhost
</code></pre></div>
<h5 dir="auto">Expected Results:</h5>
<p dir="auto">With an option like "remove_dups=yes" we would have:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="# some stuff
PasswordAuthentication no"><pre class="notranslate"><code class="notranslate"># some stuff
PasswordAuthentication no
</code></pre></div>
<h5 dir="auto">Actual Results:</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="PasswordAuthentication yes
# some stuff
PasswordAuthentication no"><pre class="notranslate"><code class="notranslate">PasswordAuthentication yes
# some stuff
PasswordAuthentication no
</code></pre></div>
<p dir="auto">Copied from original issue: <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="44378120" data-permission-text="Title is private" data-url="https://github.com/ansible/ansible-modules-core/issues/52" data-hovercard-type="issue" data-hovercard-url="/ansible/ansible-modules-core/issues/52/hovercard" href="https://github.com/ansible/ansible-modules-core/issues/52">ansible/ansible-modules-core#52</a></p>
<h5 dir="auto">ISSUE TYPE</h5>
<ul dir="auto">
<li>Bug Report</li>
</ul>
<h5 dir="auto">COMPONENT NAME</h5>

<p dir="auto">ansible-vault</p>
<h5 dir="auto">ANSIBLE VERSION</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="ansible 2.4.0.0
  config file = /home/lw/code/work/ansible/ansible.cfg
  configured module search path = [u'/home/lw/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /home/lw/code/work/ansible/venv-27-ans-24/local/lib/python2.7/site-packages/ansible
  executable location = /home/lw/code/work/ansible/venv-27-ans-24/bin/ansible
  python version = 2.7.12 (default, Nov 19 2016, 06:48:10) [GCC 5.4.0 20160609]

AND

ansible 2.5.0
  config file = /home/lw/code/work/ansible/ansible.cfg
  configured module search path = [u'/home/lw/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /home/lw/code/work/ansible/venv-27-ans/local/lib/python2.7/site-packages/ansible
  executable location = /home/lw/code/work/ansible/venv-27-ans/bin/ansible
  python version = 2.7.12 (default, Nov 19 2016, 06:48:10) [GCC 5.4.0 20160609]
"><pre class="notranslate"><code class="notranslate">ansible 2.4.0.0
  config file = /home/lw/code/work/ansible/ansible.cfg
  configured module search path = [u'/home/lw/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /home/lw/code/work/ansible/venv-27-ans-24/local/lib/python2.7/site-packages/ansible
  executable location = /home/lw/code/work/ansible/venv-27-ans-24/bin/ansible
  python version = 2.7.12 (default, Nov 19 2016, 06:48:10) [GCC 5.4.0 20160609]

AND

ansible 2.5.0
  config file = /home/lw/code/work/ansible/ansible.cfg
  configured module search path = [u'/home/lw/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /home/lw/code/work/ansible/venv-27-ans/local/lib/python2.7/site-packages/ansible
  executable location = /home/lw/code/work/ansible/venv-27-ans/bin/ansible
  python version = 2.7.12 (default, Nov 19 2016, 06:48:10) [GCC 5.4.0 20160609]

</code></pre></div>
<h5 dir="auto">CONFIGURATION</h5>
<p dir="auto">Configuration unchanged, I have run the same manual check across three versions, 2.3, 2.4 and 2.5 with the same config file. It is the default configuration.</p>
<h5 dir="auto">OS / ENVIRONMENT</h5>
<p dir="auto">Linux Ubuntu 16.04 Dell XPS 13<br>
I first installed just the devel version (2.5), after finding this bug, I apt-get installed 2.3 and tested it. After this, to duplicate virtualenv situation, I then installed stable-2.4 via pip in a new venv, then finally installed 2.3 in another separate env.</p>
<h5 dir="auto">SUMMARY</h5>
<p dir="auto">Files can be encrypted with:<br>
<code class="notranslate">ansible-vault encrypt foobar.yml</code></p>
<p dir="auto">But when you use:<br>
<code class="notranslate">ansible-vault edit foorbar.yml</code></p>
<p dir="auto">On 2.4 and 2.5, you receive a prompt to set a new password again, even though the file is already encrypted, you can then drag your version back to 2.3, and it wiill return to "normal/expected behaviour".</p>
<p dir="auto">EDIT: Also, an additional point (this is actually probably important), is that when you are reprompted to create a "new" password for an already encrypted file, if you input the password originally used for encryption on both of the "set a new password prompts", you will be able to access the file, if you try to set a new password however, that will fail.</p>
<h5 dir="auto">STEPS TO REPRODUCE</h5>
<p dir="auto">Install from devel, locate yourself in the role directory and create a file you wish to ansible-vault, use <code class="notranslate">ansible-vault encrypt foobar.yml</code> on the file, then try and run <code class="notranslate">ansible-vault edit foobar.yml</code>, this should occur on 2.4 and 2.5, I first ran the encrypt via 2.5, and I can successfully edit it using 2.3, but not any version higher than 2.3</p>

<h5 dir="auto">EXPECTED RESULTS</h5>

<p dir="auto">I expected to be able to get a predictable result from ansible-vault, I expect to be able to encrypt, decrypt, edit and view like I can in 2.3</p>
<h5 dir="auto">ACTUAL RESULTS</h5>

<p dir="auto">What</p>

<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="(venv-27-ans) lw@RobertOppenheimer:files$ echo &quot;this is a test&quot; &gt; foobar.yml
(venv-27-ans) lw@RobertOppenheimer:files$ cat foobar.yml 
this is a test
(venv-27-ans) lw@RobertOppenheimer:files$ ansible-vault encrypt foobar.yml 
New Vault password: _thisisatest_ 
Confirm New Vault password: _thisisatest_
Encryption successful
(venv-27-ans) lw@RobertOppenheimer:files$ cat foobar.yml 
$ANSIBLE_VAULT;1.1;AES256
37623637346261373237633131376234356433353765633366346561363935666634663035313736
3961383762346666626434366635343461613064383066340a643237636566353533623234633231
36626166376463363365396131623030326564666331353962626433653962663731386336646135
3232393131316630660a376437633162613437326633626536656131653461636266346463353366
6330
(venv-27-ans) lw@RobertOppenheimer:files$ ansible-vault edit foobar.yml 
New Vault password: 
"><pre class="notranslate"><code class="notranslate">(venv-27-ans) lw@RobertOppenheimer:files$ echo "this is a test" &gt; foobar.yml
(venv-27-ans) lw@RobertOppenheimer:files$ cat foobar.yml 
this is a test
(venv-27-ans) lw@RobertOppenheimer:files$ ansible-vault encrypt foobar.yml 
New Vault password: _thisisatest_ 
Confirm New Vault password: _thisisatest_
Encryption successful
(venv-27-ans) lw@RobertOppenheimer:files$ cat foobar.yml 
$ANSIBLE_VAULT;1.1;AES256
37623637346261373237633131376234356433353765633366346561363935666634663035313736
3961383762346666626434366635343461613064383066340a643237636566353533623234633231
36626166376463363365396131623030326564666331353962626433653962663731386336646135
3232393131316630660a376437633162613437326633626536656131653461636266346463353366
6330
(venv-27-ans) lw@RobertOppenheimer:files$ ansible-vault edit foobar.yml 
New Vault password: 

</code></pre></div>
<p dir="auto">EDIT: Also, an additional point (this is actually probably important), is that when you are reprompted to create a "new" password for an already encrypted file, if you input the password originally used for encryption on both of the "set a new password prompts", you will be able to access the file, if you try to set a new password however, that will fail.</p>