<h5 dir="auto">ISSUE TYPE</h5>
<ul dir="auto">
<li>Bug Report</li>
</ul>
<h5 dir="auto">COMPONENT NAME</h5>
<p dir="auto">Variable handling</p>
<h5 dir="auto">ANSIBLE VERSION</h5>

<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="ansible 2.3.1.0"><pre class="notranslate"><code class="notranslate">ansible 2.3.1.0
</code></pre></div>
<h5 dir="auto">CONFIGURATION</h5>
<p dir="auto">N/A</p>
<h5 dir="auto">OS / ENVIRONMENT</h5>
<p dir="auto">N/A</p>
<h5 dir="auto">SUMMARY</h5>
<p dir="auto">Ansible now insists on evaluating variables in  <code class="notranslate">group_vars</code> even if overridden in  <code class="notranslate">hosts</code></p>
<h5 dir="auto">STEPS TO REPRODUCE</h5>
<p dir="auto">In <code class="notranslate">group_vars</code> i have a file <code class="notranslate">all</code> (for all my environments), in which I have `testing_host: "{{ groups.loadbalancer_production[0] }}"</p>
<p dir="auto">In my <code class="notranslate">hosts</code> directory, the <code class="notranslate">stage</code> hostfiles contains <code class="notranslate">testing_host=127.0.0.1</code><br>
Before upgrading to 2.3, this was working fine.</p>
<h5 dir="auto">EXPECTED RESULTS</h5>
<p dir="auto"><code class="notranslate">testing_host</code> correctly contains 127.0.0.1</p>
<h5 dir="auto">ACTUAL RESULTS</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="'dict object' has no attribute 'loadbalancer_production'"><pre class="notranslate"><code class="notranslate">'dict object' has no attribute 'loadbalancer_production'
</code></pre></div>
<p dir="auto">From <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/ghost/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/ghost">@ghost</a> on 2016-01-29T10:37:14Z</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">N/A</p>
<h5 dir="auto">SUMMARY</h5>
<p dir="auto">It is very common that you need to edit a file (with e.g. <code class="notranslate">lineinfile</code>, <code class="notranslate">replace</code>), but only if the file exists. To do this right now you need to combine <code class="notranslate">stats</code> module and <code class="notranslate">register: var</code> with the module which will do the actual edit:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="- stats: path=/etc/foo.conf
  register: foo

- lineinfile: dest=/etc/foo.conf regexp='find' line='replace'
  when: register.stat.exists"><pre class="notranslate"><code class="notranslate">- stats: path=/etc/foo.conf
  register: foo

- lineinfile: dest=/etc/foo.conf regexp='find' line='replace'
  when: register.stat.exists
</code></pre></div>
<p dir="auto">or you could use a dirty hack which isn't documented by checking the return code:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="- lineinfile: dest=/etc/foo.conf regexp='find' line='replace'
  register: foo
  failed_when: foo.rc not in [0, 257]"><pre class="notranslate"><code class="notranslate">- lineinfile: dest=/etc/foo.conf regexp='find' line='replace'
  register: foo
  failed_when: foo.rc not in [0, 257]
</code></pre></div>
<p dir="auto">It would be much cleaner and simpler to just add an additional parameter called <code class="notranslate">ignore_missing</code>, which will be <code class="notranslate">no</code> by default, so you could just use plain and simple:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="- lineinfile: dest=/etc/foo.conf regexp='find' line='replace' ignore_missing=yes"><pre class="notranslate"><code class="notranslate">- lineinfile: dest=/etc/foo.conf regexp='find' line='replace' ignore_missing=yes
</code></pre></div>
<p dir="auto">If you like this idea, I would be more than happy to add it to these modules (and probably some others).</p>
<p dir="auto">Copied from original issue: <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="129728094" data-permission-text="Title is private" data-url="https://github.com/ansible/ansible-modules-core/issues/2914" data-hovercard-type="issue" data-hovercard-url="/ansible/ansible-modules-core/issues/2914/hovercard" href="https://github.com/ansible/ansible-modules-core/issues/2914">ansible/ansible-modules-core#2914</a></p>