Use variable with item in when condition in ansible task

214 views
Skip to first unread message

Pugazhendhi Ramakrishnan

unread,
Aug 4, 2022, 2:41:05 AM8/4/22
to Ansible Project

I'm using the below playbook to capture the vmware datacenter information, which is working fine without any issues:

---
- hosts: localhost
  vars_files: 1credentials.yml
  tasks:
    - name: Gather information about all datacenters
      community.vmware.vmware_datacenter_info:
        hostname: '{{ vcenter_hostname }}'
        username: '{{ vcenter_username }}'
        password: '{{ vcenter_password }}'
        validate_certs: no
      delegate_to: localhost
      register: datacenter

    - debug:
        msg: "{{ item.name }}"
      loop: "{{ datacenter.datacenter_info }}"
      when:
        - item.name is defined
        - item.name == datacenter

below is the output:

PLAY [localhost] *******************************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************
ok: [localhost]

TASK [Gather information about all datacenters] ************************************************************************************************************************
ok: [localhost]

TASK [debug] ***********************************************************************************************************************************************************
skipping: [localhost] => (item={'name': 'Datacenter-Test', 'moid': 'datacenter-1247', 'config_status': 'gray', 'overall_status': 'gray'})
ok: [localhost] => (item={'name': 'opendc-rookie', 'moid': 'datacenter-2', 'config_status': 'gray', 'overall_status': 'gray'}) => {
    "msg": "opendc-rookie"
}

PLAY RECAP *************************************************************************************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0


But when I try to use a variable for the datacenter value as below:

---
- hosts: localhost
  vars_files: 1credentials.yml
  vars:
    datacenter: opendc-rookie
  tasks:
    - name: Gather information about all datacenters
      community.vmware.vmware_datacenter_info:
        hostname: '{{ vcenter_hostname }}'
        username: '{{ vcenter_username }}'
        password: '{{ vcenter_password }}'
        validate_certs: no
      delegate_to: localhost
      register: datacenter

    - debug:
        msg: "{{ item.name }}"
      loop: "{{ datacenter.datacenter_info }}"
      when:
        - item.name is defined
        - item.name == "datacenter"

It is skipping the debug task. Kindly suggest how can I incorporate the variable value with the when condition having item. Below is the output skipping the variable

PLAY [localhost] *******************************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************
ok: [localhost]

TASK [Gather information about all datacenters] ************************************************************************************************************************
ok: [localhost]

TASK [debug] ***********************************************************************************************************************************************************
skipping: [localhost] => (item={'name': 'Datacenter-Test', 'moid': 'datacenter-1247', 'config_status': 'gray', 'overall_status': 'gray'})
skipping: [localhost] => (item={'name': 'opendc-rookie', 'moid': 'datacenter-2', 'config_status': 'gray', 'overall_status': 'gray'})
skipping: [localhost]

PLAY RECAP *************************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0

Dick Visser

unread,
Aug 4, 2022, 4:18:43 AM8/4/22
to ansible...@googlegroups.com
what are you trying to do exactly?
i.e. what do you expect the debug task to show?

without knowing the structure of the datacenter variable, it's not really possible to help

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/90c18c21-04f1-4313-932d-0eed6b27c54cn%40googlegroups.com.

Walter Rowe

unread,
Aug 4, 2022, 9:17:24 AM8/4/22
to Ansible Project
Your output tells you everything you need to know.

skipping: [localhost] => (item={'name': 'Datacenter-Test', 'moid': 'datacenter-1247', 'config_status': 'gray', 'overall_status': 'gray'})
skipping: [localhost] => (item={'name': 'opendc-rookie', 'moid': 'datacenter-2', 'config_status': 'gray', 'overall_status': 'gray'})
skipping: [localhost]

Your condition is

      when:
        - item.name is defined
        - item.name == "datacenter"

In all of the data processed by the look, the value of 'name' is never 'datacenter' so the task is skipped.
--
Walter Rowe, Chief
Infrastructure Services
Office of Information Systems Management
National Institute of Standards and Technology
United States Department of Commerce
Reply all
Reply to author
Forward
0 new messages