<h5 dir="auto">Issue Type:</h5>
<p dir="auto">Bug Report</p>
<h5 dir="auto">Ansible Version:</h5>
<p dir="auto">ansible 1.7.1</p>
<h5 dir="auto">Environment:</h5>
<p dir="auto">N/A</p>
<h5 dir="auto">Summary:</h5>
<p dir="auto">There is a problem when invoking apt with variables.<br>
When the variables are inside an argument, it gives an error:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="- name: Install chromium browser
  apt: deb=/root/{{item}}.deb
  when: ansible_architecture in ['armv7l']
  with_items:
    - chromium-inspector
    - chromium-browser"><pre class="notranslate"><code class="notranslate">- name: Install chromium browser
  apt: deb=/root/{{item}}.deb
  when: ansible_architecture in ['armv7l']
  with_items:
    - chromium-inspector
    - chromium-browser
</code></pre></div>
<p dir="auto">Fails:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="TASK: [gui | Install chromium browser] ****************************************
fatal: [7c669d537a3e] =&gt; failed to parse: Traceback (most recent call last):
  File &quot;/root/.ansible/tmp/ansible-tmp-1409662082.8-144889871942361/apt&quot;, line 1907, in &lt;module&gt;
    main()
  File &quot;/root/.ansible/tmp/ansible-tmp-1409662082.8-144889871942361/apt&quot;, line 523, in main
    force=force_yes, dpkg_options=p['dpkg_options'])
  File &quot;/root/.ansible/tmp/ansible-tmp-1409662082.8-144889871942361/apt&quot;, line 299, in install_deb
    pkg = apt.debfile.DebPackage(deb_file)
  File &quot;/usr/lib/python2.7/dist-packages/apt/debfile.py&quot;, line 57, in __init__
    self.open(filename)
  File &quot;/usr/lib/python2.7/dist-packages/apt/debfile.py&quot;, line 66, in open
    self._debfile = apt_inst.DebFile(self.filename)
SystemError: E:Could not open file /root/chromium-inspector - open (2: No such file or directory), E:Unable to determine the file size - fstat (9: Bad file descriptor), E:Read error - read (9: Bad file descriptor)


FATAL: all hosts have already failed -- aborting"><pre class="notranslate"><code class="notranslate">TASK: [gui | Install chromium browser] ****************************************
fatal: [7c669d537a3e] =&gt; failed to parse: Traceback (most recent call last):
  File "/root/.ansible/tmp/ansible-tmp-1409662082.8-144889871942361/apt", line 1907, in &lt;module&gt;
    main()
  File "/root/.ansible/tmp/ansible-tmp-1409662082.8-144889871942361/apt", line 523, in main
    force=force_yes, dpkg_options=p['dpkg_options'])
  File "/root/.ansible/tmp/ansible-tmp-1409662082.8-144889871942361/apt", line 299, in install_deb
    pkg = apt.debfile.DebPackage(deb_file)
  File "/usr/lib/python2.7/dist-packages/apt/debfile.py", line 57, in __init__
    self.open(filename)
  File "/usr/lib/python2.7/dist-packages/apt/debfile.py", line 66, in open
    self._debfile = apt_inst.DebFile(self.filename)
SystemError: E:Could not open file /root/chromium-inspector - open (2: No such file or directory), E:Unable to determine the file size - fstat (9: Bad file descriptor), E:Read error - read (9: Bad file descriptor)


FATAL: all hosts have already failed -- aborting
</code></pre></div>
<p dir="auto">(note the missing .deb at the end of the filename)</p>
<p dir="auto">Whereas:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="- name: Install chromium browser
  apt: deb=/root/{{item}}
  when: ansible_architecture in ['armv7l']
  with_items:
    - chromium-inspector.deb
    - chromium-browser.deb"><pre class="notranslate"><code class="notranslate">- name: Install chromium browser
  apt: deb=/root/{{item}}
  when: ansible_architecture in ['armv7l']
  with_items:
    - chromium-inspector.deb
    - chromium-browser.deb
</code></pre></div>
<p dir="auto">(note moving the .deb filenames inside the array rather than after the variable on the deb argument)</p>
<p dir="auto">Succeeds.<br>
There should NOT be any difference between the runs.<br>
When trying to find out if it is caused by whitespace I did:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="- name: Install chromium browser
  shell: echo deb=/root/{{item}}.deb
  when: ansible_architecture in ['armv7l']
  with_items:
    - chromium-inspector
    - chromium-browser"><pre class="notranslate"><code class="notranslate">- name: Install chromium browser
  shell: echo deb=/root/{{item}}.deb
  when: ansible_architecture in ['armv7l']
  with_items:
    - chromium-inspector
    - chromium-browser
</code></pre></div>
<p dir="auto">Which resulted in:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="changed: [7c669d537a3e] =&gt; (item=chromium-inspector) =&gt; {&quot;changed&quot;: true, &quot;cmd&quot;: &quot;echo deb=/root/chromium-inspector.deb&quot;, &quot;delta&quot;: &quot;0:00:00.035228&quot;, &quot;end&quot;: &quot;2014-09-02 12:48:54.123722&quot;, &quot;item&quot;: &quot;chromium-inspector&quot;, &quot;rc&quot;: 0, &quot;start&quot;: &quot;2014-09-02 12:48:54.088494&quot;, &quot;stderr&quot;: &quot;&quot;, &quot;stdout&quot;: &quot;deb=/root/chromium-inspector.deb&quot;}
changed: [7c669d537a3e] =&gt; (item=chromium-browser) =&gt; {&quot;changed&quot;: true, &quot;cmd&quot;: &quot;echo deb=/root/chromium-browser.deb&quot;, &quot;delta&quot;: &quot;0:00:00.017942&quot;, &quot;end&quot;: &quot;2014-09-02 12:48:56.467385&quot;, &quot;item&quot;: &quot;chromium-browser&quot;, &quot;rc&quot;: 0, &quot;start&quot;: &quot;2014-09-02 12:48:56.449443&quot;, &quot;stderr&quot;: &quot;&quot;, &quot;stdout&quot;: &quot;deb=/root/chromium-browser.deb&quot;}"><pre class="notranslate"><code class="notranslate">changed: [7c669d537a3e] =&gt; (item=chromium-inspector) =&gt; {"changed": true, "cmd": "echo deb=/root/chromium-inspector.deb", "delta": "0:00:00.035228", "end": "2014-09-02 12:48:54.123722", "item": "chromium-inspector", "rc": 0, "start": "2014-09-02 12:48:54.088494", "stderr": "", "stdout": "deb=/root/chromium-inspector.deb"}
changed: [7c669d537a3e] =&gt; (item=chromium-browser) =&gt; {"changed": true, "cmd": "echo deb=/root/chromium-browser.deb", "delta": "0:00:00.017942", "end": "2014-09-02 12:48:56.467385", "item": "chromium-browser", "rc": 0, "start": "2014-09-02 12:48:56.449443", "stderr": "", "stdout": "deb=/root/chromium-browser.deb"}
</code></pre></div>
<h5 dir="auto">Steps To Reproduce:</h5>
<p dir="auto">See above.</p>
<h5 dir="auto">Expected Results:</h5>
<p dir="auto">No difference between the runs</p>
<h5 dir="auto">Actual Results:</h5>
<p dir="auto">Fails when .deb is in the argument line, Succeeds when inside the array.</p>
<p dir="auto">From <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/jsnshrmn/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/jsnshrmn">@jsnshrmn</a> on 2016-06-17T15:03:20Z</p>
<h5 dir="auto">ISSUE TYPE</h5>
<ul dir="auto">
<li>Bug Report</li>
</ul>
<h5 dir="auto">COMPONENT NAME</h5>
<p dir="auto">lineinfile</p>
<h5 dir="auto">ANSIBLE VERSION</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="ansible 2.1.0.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides"><pre class="notranslate"><code class="notranslate">ansible 2.1.0.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
</code></pre></div>
<h5 dir="auto">CONFIGURATION</h5>
<p dir="auto">Bone stock</p>
<h5 dir="auto">OS / ENVIRONMENT</h5>
<p dir="auto">CentOS 7</p>
<h5 dir="auto">SUMMARY</h5>
<p dir="auto">When lineinfile  with regexp finds a match, it substitutes properly.  When it doesn't find a match (say, on subsequent runs), it just dumps the specified line at the bottom of the file as if you hadn't specified a regexp.  Needless to say, this breaks idempotence.</p>
<h5 dir="auto">STEPS TO REPRODUCE</h5>
<ol dir="auto">
<li>Take a look at a file.</li>
<li>Run lineinfile with regexp that matches a line</li>
<li>See that your line was in fact replaced.</li>
<li>Run lineinfile again.</li>
<li>See that the specified replacement line is now duplicated at the bottom of the file.</li>
</ol>

<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="- name: Add ops scripts to sudo secure_path
  lineinfile:
    dest: /etc/sudoers
    regexp: &gt;
      ^Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin$
    line: 'Defaults    secure_path = /opt/d7/bin:/sbin:/bin:/usr/sbin:/usr/bin'
    validate: visudo -cf %s"><pre class="notranslate"><code class="notranslate">- name: Add ops scripts to sudo secure_path
  lineinfile:
    dest: /etc/sudoers
    regexp: &gt;
      ^Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin$
    line: 'Defaults    secure_path = /opt/d7/bin:/sbin:/bin:/usr/sbin:/usr/bin'
    validate: visudo -cf %s
</code></pre></div>
<h5 dir="auto">EXPECTED RESULTS</h5>
<p dir="auto">Idempotent line replacement after a second run</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="# Refuse to run if unable to disable echo on the tty.
Defaults   !visiblepw

#
# Preserving HOME has security implications since many programs
# use it when searching for configuration files. Note that HOME
# is already set when the the env_reset option is enabled, so
# this option is only effective for configurations where either
# env_reset is disabled or HOME is present in the env_keep list.
#
Defaults    always_set_home

Defaults    env_reset
Defaults    env_keep =  &quot;COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR LS_COLORS&quot;
Defaults    env_keep += &quot;MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE&quot;
Defaults    env_keep += &quot;LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES&quot;
Defaults    env_keep += &quot;LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE&quot;
Defaults    env_keep += &quot;LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY&quot;

#
# Adding HOME to env_keep may enable a user to run unrestricted
# commands via sudo.
#
# Defaults   env_keep += &quot;HOME&quot;

Defaults    secure_path = /opt/d7/bin:/sbin:/bin:/usr/sbin:/usr/bin

## Next comes the main part: which users can run what software on
## which machines (the sudoers file can be shared between multiple
## systems).
## Syntax:
##
##      user    MACHINE=COMMANDS
##
## The COMMANDS section may have other options added to it.
##
## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL

## Allows people in group wheel to run all commands without a password
%wheel        ALL=(ALL)       NOPASSWD: ALL"><pre class="notranslate"><code class="notranslate"># Refuse to run if unable to disable echo on the tty.
Defaults   !visiblepw

#
# Preserving HOME has security implications since many programs
# use it when searching for configuration files. Note that HOME
# is already set when the the env_reset option is enabled, so
# this option is only effective for configurations where either
# env_reset is disabled or HOME is present in the env_keep list.
#
Defaults    always_set_home

Defaults    env_reset
Defaults    env_keep =  "COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR LS_COLORS"
Defaults    env_keep += "MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE"
Defaults    env_keep += "LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES"
Defaults    env_keep += "LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE"
Defaults    env_keep += "LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY"

#
# Adding HOME to env_keep may enable a user to run unrestricted
# commands via sudo.
#
# Defaults   env_keep += "HOME"

Defaults    secure_path = /opt/d7/bin:/sbin:/bin:/usr/sbin:/usr/bin

## Next comes the main part: which users can run what software on
## which machines (the sudoers file can be shared between multiple
## systems).
## Syntax:
##
##      user    MACHINE=COMMANDS
##
## The COMMANDS section may have other options added to it.
##
## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL

## Allows people in group wheel to run all commands without a password
%wheel        ALL=(ALL)       NOPASSWD: ALL
</code></pre></div>
<h5 dir="auto">ACTUAL RESULTS</h5>
<p dir="auto">Potentially show-stopping garbage at the bottom of a file</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="# Refuse to run if unable to disable echo on the tty.
Defaults   !visiblepw

#
# Preserving HOME has security implications since many programs
# use it when searching for configuration files. Note that HOME
# is already set when the the env_reset option is enabled, so
# this option is only effective for configurations where either
# env_reset is disabled or HOME is present in the env_keep list.
#
Defaults    always_set_home

Defaults    env_reset
Defaults    env_keep =  &quot;COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR LS_COLORS&quot;
Defaults    env_keep += &quot;MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE&quot;
Defaults    env_keep += &quot;LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES&quot;
Defaults    env_keep += &quot;LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE&quot;
Defaults    env_keep += &quot;LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY&quot;

#
# Adding HOME to env_keep may enable a user to run unrestricted
# commands via sudo.
#
# Defaults   env_keep += &quot;HOME&quot;

Defaults    secure_path = /opt/d7/bin:/sbin:/bin:/usr/sbin:/usr/bin

## Next comes the main part: which users can run what software on
## which machines (the sudoers file can be shared between multiple
## systems).
## Syntax:
##
##      user    MACHINE=COMMANDS
##
## The COMMANDS section may have other options added to it.
##
## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL

## Allows people in group wheel to run all commands without a password
%wheel        ALL=(ALL)       NOPASSWD: ALL
Defaults    secure_path = /opt/d7/bin:/sbin:/bin:/usr/sbin:/usr/bin"><pre class="notranslate"><code class="notranslate"># Refuse to run if unable to disable echo on the tty.
Defaults   !visiblepw

#
# Preserving HOME has security implications since many programs
# use it when searching for configuration files. Note that HOME
# is already set when the the env_reset option is enabled, so
# this option is only effective for configurations where either
# env_reset is disabled or HOME is present in the env_keep list.
#
Defaults    always_set_home

Defaults    env_reset
Defaults    env_keep =  "COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR LS_COLORS"
Defaults    env_keep += "MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE"
Defaults    env_keep += "LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES"
Defaults    env_keep += "LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE"
Defaults    env_keep += "LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY"

#
# Adding HOME to env_keep may enable a user to run unrestricted
# commands via sudo.
#
# Defaults   env_keep += "HOME"

Defaults    secure_path = /opt/d7/bin:/sbin:/bin:/usr/sbin:/usr/bin

## Next comes the main part: which users can run what software on
## which machines (the sudoers file can be shared between multiple
## systems).
## Syntax:
##
##      user    MACHINE=COMMANDS
##
## The COMMANDS section may have other options added to it.
##
## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL

## Allows people in group wheel to run all commands without a password
%wheel        ALL=(ALL)       NOPASSWD: ALL
Defaults    secure_path = /opt/d7/bin:/sbin:/bin:/usr/sbin:/usr/bin
</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="160908951" data-permission-text="Title is private" data-url="https://github.com/ansible/ansible-modules-core/issues/3975" data-hovercard-type="issue" data-hovercard-url="/ansible/ansible-modules-core/issues/3975/hovercard" href="https://github.com/ansible/ansible-modules-core/issues/3975">ansible/ansible-modules-core#3975</a></p>