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

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/90c18c21-04f1-4313-932d-0eed6b27c54cn%40googlegroups.com.

Reply via email to