<h5 dir="auto">ISSUE TYPE</h5>
<p dir="auto">Bug Report</p>
<h5 dir="auto">COMPONENT NAME</h5>
<p dir="auto">core</p>
<h5 dir="auto">ANSIBLE VERSION</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="ansible 1.9.4
ansible 2.0.1.0"><pre class="notranslate"><code class="notranslate">ansible 1.9.4
ansible 2.0.1.0
</code></pre></div>
<h5 dir="auto">CONFIGURATION</h5>
<p dir="auto">Default of yum install</p>
<h5 dir="auto">OS / ENVIRONMENT</h5>
<p dir="auto">CentOS Linux release 7.2.1511 (Core)<br>
SUMMARY and</p>
<h5 dir="auto">SUMMARY</h5>
<p dir="auto">Inconsistent behavior of when statement</p>
<h5 dir="auto">STEPS TO REPRODUCE</h5>
<p dir="auto">If I write an inventory file and a task file as follows, <code class="notranslate">do_something.sh</code> is executed only on web1.example.com.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="[webservers]
web1.example.com var1=true
web2.example.com var1=false"><pre class="notranslate"><code class="notranslate">[webservers]
web1.example.com var1=true
web2.example.com var1=false
</code></pre></div>
<div class="highlight highlight-source-yaml notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="- command: /usr/bin/do_something.sh
  when: var1"><pre class="notranslate">- <span class="pl-ent">command</span>: <span class="pl-s">/usr/bin/do_something.sh</span>
  <span class="pl-ent">when</span>: <span class="pl-s">var1</span></pre></div>
<p dir="auto">But if I write an inventory file as follows:</p>
<div class="highlight highlight-source-yaml notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="- command: /usr/bin/do_something.sh
  when: var1 is defined and var1"><pre class="notranslate">- <span class="pl-ent">command</span>: <span class="pl-s">/usr/bin/do_something.sh</span>
  <span class="pl-ent">when</span>: <span class="pl-s">var1 is defined and var1</span></pre></div>
<p dir="auto"><code class="notranslate">do_something.sh</code> is executed on both web1.example.com and web2.example.com.<br>
Of course, I know that all host variables defined in inventory files are treated as string variables. However, it does not explain this inconsistent behavior.<br>
Here is an example of task.yml: <a href="https://github.com/muziyoshiz/ansible-2.0-sample/blob/master/roles/webtier4/tasks/main.yml">ansible-2.0-sample/main.yml at master  muziyoshiz/ansible-2.0-sample</a></p>
<h5 dir="auto">EXPECTED RESULTS</h5>
<p dir="auto">If <code class="notranslate">web2.example.com var1=false</code> is in the inventory file,</p>
<ul dir="auto">
<li><code class="notranslate">when: var1</code> and <code class="notranslate">when: var1 is defined and var1</code> should be treated same as <code class="notranslate">when: false</code></li>
</ul>
<h5 dir="auto">ACTUAL RESULTS</h5>
<p dir="auto">If <code class="notranslate">web2.example.com var1=false</code> is in the inventory file,</p>
<ul dir="auto">
<li><code class="notranslate">when: var1</code> was treated same as <code class="notranslate">when: false</code></li>
<li><code class="notranslate">when: var1 is defined and var1</code> was treated same as <code class="notranslate">when: true</code></li>
</ul>
<h5 dir="auto">ISSUE TYPE</h5>
<ul dir="auto">
<li>Bug Report</li>
</ul>
<h5 dir="auto">COMPONENT NAME</h5>
<p dir="auto">when / when not conditional</p>
<h5 dir="auto">ANSIBLE VERSION</h5>
<p dir="auto">From devel branch:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="ansible 2.5.0 (devel 34206a0402) last updated 2018/01/08 13:39:51 (GMT -400)
  config file = None
  configured module search path = ['/home/myuser/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/myuser/git/ansible/lib/ansible
  executable location = /home/myuser/git/ansible/venv/bin/ansible
  python version = 3.6.2 (default, Oct  2 2017, 16:51:32) [GCC 7.2.1 20170915 (Red Hat 7.2.1-2)]"><pre class="notranslate"><code class="notranslate">ansible 2.5.0 (devel 34206a0402) last updated 2018/01/08 13:39:51 (GMT -400)
  config file = None
  configured module search path = ['/home/myuser/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/myuser/git/ansible/lib/ansible
  executable location = /home/myuser/git/ansible/venv/bin/ansible
  python version = 3.6.2 (default, Oct  2 2017, 16:51:32) [GCC 7.2.1 20170915 (Red Hat 7.2.1-2)]
</code></pre></div>
<p dir="auto">Also versions 2.4.1 and 2.4.2 from pip, and 2.4.1 via RPM on RHEL.</p>
<h5 dir="auto">CONFIGURATION</h5>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="$ ansible-config dump --only-changed
$ (no output)"><pre class="notranslate">$ ansible-config dump --only-changed
$ (no output)</pre></div>
<h5 dir="auto">OS / ENVIRONMENT</h5>
<p dir="auto">Fedora 26, RHEL 7</p>
<h5 dir="auto">SUMMARY</h5>
<p dir="auto">When a user supplies a variable that should be a boolean value as the string value 'false', we witness inconsistent behavior in the 'when' and 'when not' condition.  For example, if x="false", then both "when: not x" and "when: x" resolve to false.</p>
<h5 dir="auto">STEPS TO REPRODUCE</h5>
<p dir="auto">This was originally triggered by a value provided in an ini formatted inventory, (eg, x=false) which equates to a string, but the same behavior can be replicated with</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="---
- hosts: localhost
  connection: local
  vars:
    x: &quot;false&quot;
  tasks:
    - set_fact:
        other_var: &quot;x was true&quot;
      when: x
    - set_fact:
        other_var: &quot;x was false&quot;
      when: not x
    - debug: var=other_var"><pre class="notranslate"><code class="notranslate">---
- hosts: localhost
  connection: local
  vars:
    x: "false"
  tasks:
    - set_fact:
        other_var: "x was true"
      when: x
    - set_fact:
        other_var: "x was false"
      when: not x
    - debug: var=other_var
</code></pre></div>
<h5 dir="auto">EXPECTED RESULTS</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="TASK [debug] 
ok: [localhost] =&gt; {
    &quot;attempts&quot;: 1,
    &quot;other_var&quot;: &quot;x was false&quot;
}"><pre class="notranslate"><code class="notranslate">TASK [debug] 
ok: [localhost] =&gt; {
    "attempts": 1,
    "other_var": "x was false"
}
</code></pre></div>
<p dir="auto">It would be fair if this resolved to 'x was true' as well, because a defined string (other than one that shares a name with a boolean) will resolve to True normally.  However, I feel both 'x' and 'not x' cannot be false for the same value.</p>
<h5 dir="auto">ACTUAL RESULTS</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="TASK [set_fact] 
skipping: [localhost]

TASK [set_fact] 
skipping: [localhost]

TASK [debug] 
ok: [localhost] =&gt; {
    &quot;attempts&quot;: 1,
    &quot;other_var&quot;: &quot;VARIABLE IS NOT DEFINED!&quot;
}
"><pre class="notranslate"><code class="notranslate">TASK [set_fact] 
skipping: [localhost]

TASK [set_fact] 
skipping: [localhost]

TASK [debug] 
ok: [localhost] =&gt; {
    "attempts": 1,
    "other_var": "VARIABLE IS NOT DEFINED!"
}

</code></pre></div>