<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 2.2.0 (devel 5f12731797) last updated 2016/08/03 183842 (GMT +200)
lib/ansible/modules/core (detached HEAD db66758125) last updated 2016/08/03 183900 (GMT +200)
lib/ansible/modules/extras (detached HEAD 57c142b6ed) last updated 2016/08/03 184009 (GMT +200)
config file = /home/jpic/.ansible.cfg
configured module search path = [/home/jpic/ansible/library /usr/share/ansible]"><pre class="notranslate"><code class="notranslate">ansible 2.2.0 (devel 5f12731797) last updated 2016/08/03 183842 (GMT +200)
lib/ansible/modules/core (detached HEAD db66758125) last updated 2016/08/03 183900 (GMT +200)
lib/ansible/modules/extras (detached HEAD 57c142b6ed) last updated 2016/08/03 184009 (GMT +200)
config file = /home/jpic/.ansible.cfg
configured module search path = [/home/jpic/ansible/library /usr/share/ansible]
</code></pre></div>
<h5 dir="auto">CONFIGURATION</h5>
<h5 dir="auto">OS / ENVIRONMENT</h5>
<h5 dir="auto">SUMMARY</h5>
<p dir="auto">An exception is raised when:</p>
<ul dir="auto">
<li>adding a host with a given group name,</li>
<li>having a task with delegate_to: the_group_name</li>
<li>having a play for that group.<br>
Pretty interresting indeed.</li>
</ul>
<h5 dir="auto">STEPS TO REPRODUCE</h5>
<div class="highlight highlight-source-yaml notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="- hosts: localhost
  tasks:
  - add_host: name=testboot.lxc group=testboot # replace testboot.lxc by anything on which setup wil work
  - raw: uname
    delegate_to: testboot # obviously this is wrong, because we have no such host
    when: false  # however, this task is skipped, so not failing
- hosts: testboot # this triggers the exception, if you remove the skipped task above if works though
  tasks:
  - shell: uname -a"><pre class="notranslate">- <span class="pl-ent">hosts</span>: <span class="pl-s">localhost</span>
  <span class="pl-ent">tasks</span>:
  - <span class="pl-ent">add_host</span>: <span class="pl-s">name=testboot.lxc group=testboot </span><span class="pl-c"><span class="pl-c">#</span> replace testboot.lxc by anything on which setup wil work</span>
  - <span class="pl-ent">raw</span>: <span class="pl-s">uname</span>
    <span class="pl-ent">delegate_to</span>: <span class="pl-s">testboot </span><span class="pl-c"><span class="pl-c">#</span> obviously this is wrong, because we have no such host</span>
    <span class="pl-ent">when</span>: <span class="pl-s">false  </span><span class="pl-c"><span class="pl-c">#</span> however, this task is skipped, so not failing</span>
- <span class="pl-ent">hosts</span>: <span class="pl-s">testboot </span><span class="pl-c"><span class="pl-c">#</span> this triggers the exception, if you remove the skipped task above if works though</span>
  <span class="pl-ent">tasks</span>:
  - <span class="pl-ent">shell</span>: <span class="pl-s">uname -a</span></pre></div>
<h5 dir="auto">EXPECTED RESULTS</h5>
<h5 dir="auto">ACTUAL RESULTS</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="PLAYBOOK: test.yml *************************************************************
2 plays in test.yml
PLAY [localhost] ***************************************************************
TASK [setup] *******************************************************************
ok: [localhost]
TASK [add_host] ****************************************************************
task path: /home/jpic/scratch/test.yml:6
creating host via 'add_host': hostname=testboot.lxc
changed: [localhost] =&gt; {
    &quot;add_host&quot;: {
        &quot;groups&quot;: [
            &quot;testboot&quot;
        ], 
        &quot;host_name&quot;: &quot;testboot.lxc&quot;, 
        &quot;host_vars&quot;: {
            &quot;group&quot;: &quot;testboot&quot;
        }
    }, 
    &quot;changed&quot;: true, 
    &quot;invocation&quot;: {
        &quot;module_args&quot;: {
            &quot;group&quot;: &quot;testboot&quot;, 
            &quot;name&quot;: &quot;testboot.lxc&quot;
        }, 
        &quot;module_name&quot;: &quot;add_host&quot;
    }
}
TASK [raw] *********************************************************************
task path: /home/jpic/scratch/test.yml:7
skipping: [localhost] =&gt; {
    &quot;changed&quot;: false, 
    &quot;skip_reason&quot;: &quot;Conditional check failed&quot;, 
    &quot;skipped&quot;: true
}
ERROR! Unexpected Exception: 'NoneType' object has no attribute 'name'
the full traceback was:
Traceback (most recent call last):
  File &quot;/home/jpic/env/src/ansible/bin/ansible-playbook&quot;, line 97, in &lt;module&gt;
    exit_code = cli.run()
  File &quot;/home/jpic/env/src/ansible/lib/ansible/cli/playbook.py&quot;, line 154, in run
    results = pbex.run()
  File &quot;/home/jpic/env/src/ansible/lib/ansible/executor/playbook_executor.py&quot;, line 144, in run
    self._inventory.restrict_to_hosts(batch)
  File &quot;/home/jpic/env/src/ansible/lib/ansible/inventory/__init__.py&quot;, line 640, in restrict_to_hosts
    self._restriction = [ h.name for h in restriction ]
AttributeError: 'NoneType' object has no attribute 'name'"><pre class="notranslate"><code class="notranslate">PLAYBOOK: test.yml *************************************************************
2 plays in test.yml
PLAY [localhost] ***************************************************************
TASK [setup] *******************************************************************
ok: [localhost]
TASK [add_host] ****************************************************************
task path: /home/jpic/scratch/test.yml:6
creating host via 'add_host': hostname=testboot.lxc
changed: [localhost] =&gt; {
    "add_host": {
        "groups": [
            "testboot"
        ], 
        "host_name": "testboot.lxc", 
        "host_vars": {
            "group": "testboot"
        }
    }, 
    "changed": true, 
    "invocation": {
        "module_args": {
            "group": "testboot", 
            "name": "testboot.lxc"
        }, 
        "module_name": "add_host"
    }
}
TASK [raw] *********************************************************************
task path: /home/jpic/scratch/test.yml:7
skipping: [localhost] =&gt; {
    "changed": false, 
    "skip_reason": "Conditional check failed", 
    "skipped": true
}
ERROR! Unexpected Exception: 'NoneType' object has no attribute 'name'
the full traceback was:
Traceback (most recent call last):
  File "/home/jpic/env/src/ansible/bin/ansible-playbook", line 97, in &lt;module&gt;
    exit_code = cli.run()
  File "/home/jpic/env/src/ansible/lib/ansible/cli/playbook.py", line 154, in run
    results = pbex.run()
  File "/home/jpic/env/src/ansible/lib/ansible/executor/playbook_executor.py", line 144, in run
    self._inventory.restrict_to_hosts(batch)
  File "/home/jpic/env/src/ansible/lib/ansible/inventory/__init__.py", line 640, in restrict_to_hosts
    self._restriction = [ h.name for h in restriction ]
AttributeError: 'NoneType' object has no attribute 'name'
</code></pre></div>
<p dir="auto">However, if I use another faulty delegate_to that's <strong>not</strong> the name of the group in the next play then it doesn't crash:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="---
- hosts: localhost
  tasks:
  - add_host: name=testboot.lxc group=testboot
  - raw: uname
    delegate_to: testbar # not testboot
    when: false
- hosts: testboot
  tasks:
  - shell: uname -a"><pre class="notranslate"><code class="notranslate">---
- hosts: localhost
  tasks:
  - add_host: name=testboot.lxc group=testboot
  - raw: uname
    delegate_to: testbar # not testboot
    when: false
- hosts: testboot
  tasks:
  - shell: uname -a
</code></pre></div>
<p dir="auto">Result:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="PLAY [localhost] ***************************************************************
TASK [setup] *******************************************************************
ok: [localhost]
TASK [add_host] ****************************************************************
changed: [localhost]
TASK [raw] *********************************************************************
skipping: [localhost]
PLAY [testboot] ****************************************************************
TASK [setup] *******************************************************************
ok: [testboot.lxc]
TASK [command] *****************************************************************
changed: [testboot.lxc]
PLAY RECAP *********************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0   
testboot.lxc               : ok=2    changed=1    unreachable=0    failed=0   "><pre class="notranslate"><code class="notranslate">PLAY [localhost] ***************************************************************
TASK [setup] *******************************************************************
ok: [localhost]
TASK [add_host] ****************************************************************
changed: [localhost]
TASK [raw] *********************************************************************
skipping: [localhost]
PLAY [testboot] ****************************************************************
TASK [setup] *******************************************************************
ok: [testboot.lxc]
TASK [command] *****************************************************************
changed: [testboot.lxc]
PLAY RECAP *********************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0   
testboot.lxc               : ok=2    changed=1    unreachable=0    failed=0   
</code></pre></div>
<h3 dir="auto">EXPECTED RESULT</h3>
<p dir="auto">So, I find it a bit weird that a skipped task can cause a subsequent play to fail (imagine the time it took to isolate this !). I'd be willing to contribute a fix, but I'm not sure what it should be:</p>
<ul dir="auto">
<li>fail a task if delegate_to is incorrect or is a valid group name, even if it is skipped,</li>
<li>ensure that delegate_to doesn't affect the global state if it's used with a value that's a valid group name but not a valid host name.<br>
I suspect that other users might have been affected, but I don't know if anybody made it to isolate the issue, because it took quite some time I recon.</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">delegate_to</p>
<h5 dir="auto">ANSIBLE VERSION</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="ansible 2.0.1.0
  config file = /etc/ansible.cfg
  configured module search path = /usr/share/ansible"><pre class="notranslate"><code class="notranslate">ansible 2.0.1.0
  config file = /etc/ansible.cfg
  configured module search path = /usr/share/ansible
</code></pre></div>
<h5 dir="auto">CONFIGURATION</h5>
<h5 dir="auto">OS / ENVIRONMENT</h5>
<p dir="auto">N/A</p>
<h5 dir="auto">SUMMARY</h5>
<p dir="auto">delegate_to: ignore when:<br>
More specifically delegate_to is evaluated before when could even skip the task.</p>
<h5 dir="auto">STEPS TO REPRODUCE</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="- hosts: localhost
  tasks:
    - debug: msg=&quot;blah&quot;
      delegate_to: false
      when: false"><pre class="notranslate"><code class="notranslate">- hosts: localhost
  tasks:
    - debug: msg="blah"
      delegate_to: false
      when: false
</code></pre></div>
<p dir="auto">(In my case  delegate_to was a variable [the IP of a "master" node], I had hard time figuring out what was wrong)</p>
<h5 dir="auto">EXPECTED RESULTS</h5>
<p dir="auto">task skipped</p>
<h5 dir="auto">ACTUAL RESULTS</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Traceback (most recent call last):
  File &quot;/usr/bin/ansible-playbook&quot;, line 86, in &lt;module&gt;
    sys.exit(cli.run())
  File &quot;/usr/lib/python2.7/dist-packages/ansible/cli/playbook.py&quot;, line 150, in run
    results = pbex.run()
  File &quot;/usr/lib/python2.7/dist-packages/ansible/executor/playbook_executor.py&quot;, line 140, in run
    result = self._tqm.run(play=play)
  File &quot;/usr/lib/python2.7/dist-packages/ansible/executor/task_queue_manager.py&quot;, line 238, in run
    play_return = strategy.run(iterator, play_context)
  File &quot;/usr/lib/python2.7/dist-packages/ansible/plugins/strategy/linear.py&quot;, line 229, in run
    task_vars = self._variable_manager.get_vars(loader=self._loader, play=iterator._play, host=host, task=task)
  File &quot;/usr/lib/python2.7/dist-packages/ansible/vars/__init__.py&quot;, line 343, in get_vars
    all_vars['ansible_delegated_vars'] = self._get_delegated_vars(loader, play, task, all_vars)
  File &quot;/usr/lib/python2.7/dist-packages/ansible/vars/__init__.py&quot;, line 483, in _get_delegated_vars
    include_hostvars=False,
  File &quot;/usr/lib/python2.7/dist-packages/ansible/vars/__init__.py&quot;, line 247, in get_vars
    all_vars = combine_vars(all_vars, host.get_vars())
  File &quot;/usr/lib/python2.7/dist-packages/ansible/inventory/host.py&quot;, line 128, in get_vars
    results['inventory_hostname_short'] = self.name.split('.')[0]
AttributeError: 'bool' object has no attribute 'split'"><pre class="notranslate"><code class="notranslate">Traceback (most recent call last):
  File "/usr/bin/ansible-playbook", line 86, in &lt;module&gt;
    sys.exit(cli.run())
  File "/usr/lib/python2.7/dist-packages/ansible/cli/playbook.py", line 150, in run
    results = pbex.run()
  File "/usr/lib/python2.7/dist-packages/ansible/executor/playbook_executor.py", line 140, in run
    result = self._tqm.run(play=play)
  File "/usr/lib/python2.7/dist-packages/ansible/executor/task_queue_manager.py", line 238, in run
    play_return = strategy.run(iterator, play_context)
  File "/usr/lib/python2.7/dist-packages/ansible/plugins/strategy/linear.py", line 229, in run
    task_vars = self._variable_manager.get_vars(loader=self._loader, play=iterator._play, host=host, task=task)
  File "/usr/lib/python2.7/dist-packages/ansible/vars/__init__.py", line 343, in get_vars
    all_vars['ansible_delegated_vars'] = self._get_delegated_vars(loader, play, task, all_vars)
  File "/usr/lib/python2.7/dist-packages/ansible/vars/__init__.py", line 483, in _get_delegated_vars
    include_hostvars=False,
  File "/usr/lib/python2.7/dist-packages/ansible/vars/__init__.py", line 247, in get_vars
    all_vars = combine_vars(all_vars, host.get_vars())
  File "/usr/lib/python2.7/dist-packages/ansible/inventory/host.py", line 128, in get_vars
    results['inventory_hostname_short'] = self.name.split('.')[0]
AttributeError: 'bool' object has no attribute 'split'
</code></pre></div>