Ansible executes task regardless of conditional

106 views
Skip to first unread message

Trond Hindenes

unread,
Aug 1, 2016, 10:23:50 AM8/1/16
to Ansible Project
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:

---
-   name: Deploy Test VM
    hosts: localhost
    tags:
        - deploy
    roles:
        - { role: deploy_azurevm, vm_name: "{{ resource_group_prefix }}-trondtest", add_to_adhoc_group: "{{ resource_group_name }}" }

Since it works on the Windows "version" of "Add vm to inventory" everything should be fine, no?

I'm baffled. Any pointers appreciated!

_Trond

Trond Hindenes

unread,
Aug 1, 2016, 10:43:18 AM8/1/16
to Ansible Project
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:
---
-   name: Deploy Test VM
    hosts: localhost
    tags:
        - deploy
    roles:
        - { role: deploy_azurevm, vm_name: "{{ resource_group_prefix }}-trondtest", add_to_adhoc_group: "{{ resource_group_name }}" }

- name: Configure VM
  hosts: "{{ resource_group_name }}"
  roles:
    - rikstv_basicconfig_azurevm

I don't know what's wrong, whether it's the logging or the execution but I cannot make any sense of this.

Trond Hindenes

unread,
Aug 1, 2016, 11:04:37 AM8/1/16
to Ansible Project
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 }}"

J Hawkesworth

unread,
Aug 2, 2016, 1:20:55 AM8/2/16
to Ansible Project
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

Trond Hindenes

unread,
Aug 3, 2016, 7:07:37 AM8/3/16
to Ansible Project
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.

Trond Hindenes

unread,
Aug 3, 2016, 7:34:14 AM8/3/16
to Ansible Project
Reply all
Reply to author
Forward
0 new messages