# Ansible executes task regardless of conditional

**URL:** <https://forum.ansible.com/t/ansible-executes-task-regardless-of-conditional/22181>\
**Category:** Ansible Project\
**Tags:** azure, windows\
**Created:** [August 1, 2016, 2:23pm UTC](https://forum.ansible.com/t/ansible-executes-task-regardless-of-conditional/22181 "2016-08-01T14:23:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trond\_Hindenes](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@Trond\_Hindenes](https://forum.ansible.com/u/Trond_Hindenes)\
**Post date:** [August 1, 2016, 2:23pm UTC](https://forum.ansible.com/t/ansible-executes-task-regardless-of-conditional/22181/1 "2016-08-01T14:23:50Z")

</div>

Hi all,  
I have this weird thing inside a role, hopefully someone can straighten me out:

My role is a custom made one, which performs Azure vm deployments, supporting both Linux and Windows. The last steps of the role are as follows:

- name: debug  
debug:  
msg: “os\_type: {{ os\_type }} add\_to\_inventory: {{ add\_to\_inventory }}”

- name: Add vm to inventory (Windows)  
when: os\_type == “Windows”  
add\_host:  
ansible\_host: “{{ vm\_mgmt\_ip }}”  
name: “{{ vm\_name }}”  
groups: “{{ add\_to\_adhoc\_group }}”  
ansible\_user: “{{ admin\_username }}”  
ansible\_password: “{{ admin\_password }}”  
ansible\_port: 5986  
ansible\_connection: winrm  
ansible\_winrm\_server\_cert\_validation: ignore

- name: Add vm to inventory (Linux)  
when: os\_type == “Linux” and add\_to\_adhoc\_group is defined  
add\_host:  
ansible\_host: “{{ vm\_mgmt\_ip }}”  
name: “{{ vm\_name }}”  
groups: “{{ add\_to\_adhoc\_group }}”  
ansible\_user: “{{ admin\_username }}”  
ansible\_ssh\_pass: “{{ admin\_password }}”  
ansible\_become: yes

My problem is that regardless of the OS, Ansible will execute the “linux” task, here is the output from a run deploying a Windows VM:

TASK [deploy\_azurevm : debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”: “os\_type: Windows add\_to\_inventory: internal\_ip”  
}

TASK [deploy\_azurevm : Add vm to inventory (Windows)] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
changed: [localhost]

TASK [deploy\_azurevm : Add vm to inventory (Linux)] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ERROR! ‘resource\_group\_name’ is undefined

This used to work, but as I’m constantly updating both the role and the Ansible version I’m unable to track down exactly what changed. Has anyone seen something similar?  
Also, the fact that “resource\_group\_name” is undefined also seems strange to me, the role task shouldn’t care about that variable at all. The only reference to it is how I pass in vars to the role:

---

<div class="post-metadata">

**Author:** ![Trond\_Hindenes](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@Trond\_Hindenes](https://forum.ansible.com/u/Trond_Hindenes)\
**Post date:** [August 1, 2016, 2:43pm UTC](https://forum.ansible.com/t/ansible-executes-task-regardless-of-conditional/22181/2 "2016-08-01T14:43:18Z")

</div>

I’m just getting more confused by this. I updated my Ansible version to run from devel, this gave me some more info which I totally cannot make sense of:  
Here’s the output running from devel:

TASK [deploy\_azurevm : Add vm to inventory (Linux)] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ERROR! the field ‘hosts’ has an invalid value, which appears to include a variable that is undefined. The error was: ‘resource\_group\_name’ is undefined

The error appears to have been in ‘/opt/ansible/playbooks/config.azure/resourcegroupdeployments/TrondTest/site.yml’: line 9, column 3, but may  
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

- name: Configure VM  
^ here

The weird thing here, is that the error references the _next_ play in my playbook (that is, after the role has executed). Here’s the whole playbook:

---

<div class="post-metadata">

**Author:** ![Trond\_Hindenes](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@Trond\_Hindenes](https://forum.ansible.com/u/Trond_Hindenes)\
**Post date:** [August 1, 2016, 3:04pm UTC](https://forum.ansible.com/t/ansible-executes-task-regardless-of-conditional/22181/3 "2016-08-01T15:04:37Z")

</div>

As far as I can see, weird things start happening when using a variable as a target for hosts.  
If I do: hosts: just\_created things look good, but if I capture that value in a variable instead, things fall to pieces:

hosts: “{{ my\_group }}”

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [August 2, 2016, 5:20am UTC](https://forum.ansible.com/t/ansible-executes-task-regardless-of-conditional/22181/4 "2016-08-02T05:20:55Z")

</div>

This isn’t a pattern I use but I just wonder if you need to ensure that the list of hosts you are creating is held as a list

set\_fact:  
my\_group: “{{ just\_created | list }}”

hosts: “{{ my\_group }}”  
or possibly even

---

<div class="post-metadata">

**Author:** ![Trond\_Hindenes](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@Trond\_Hindenes](https://forum.ansible.com/u/Trond_Hindenes)\
**Post date:** [August 3, 2016, 11:07am UTC](https://forum.ansible.com/t/ansible-executes-task-regardless-of-conditional/22181/5 "2016-08-03T11:07:36Z")

</div>

Thanks Jon, I’ll look into this. The group I’m trying to add to is already present in the inventory, not sure if that means anything. I’ll do a bit of digging.

---

<div class="post-metadata">

**Author:** ![Trond\_Hindenes](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@Trond\_Hindenes](https://forum.ansible.com/u/Trond_Hindenes)\
**Post date:** [August 3, 2016, 11:34am UTC](https://forum.ansible.com/t/ansible-executes-task-regardless-of-conditional/22181/6 "2016-08-03T11:34:14Z")

</div>

Filed the bug here:  
[https://github.com/ansible/ansible/issues/16931](https://github.com/ansible/ansible/issues/16931)
