I believe the*loop:* values are evaluated first, before any iterations are performed, in which case any undefined variables in any of the*loop:* expressions will throw the error you see.

This is probably a desirable behavior in general even though in this case you got caught by surprise.

Here's a work-around that uses a sentinel value — and blatantly abuses "*omit*" for which I'm sure to be rightly scorned. :)

*---****- name: "Loop Iteration List"****hosts: localhost****vars:****nfs_prod_target_id: "Khan"****nfs_prod_source_id: 
"Siddque"****tasks:****- name: "Connect to Isilon Device"****ansible.builtin.debug:****msg: "{{ item }}"****when: item != 
omit****loop:****- "{{ nfs_prod_source_id | d(omit) }}"****- "{{ nfs_prod_target_id | d(omit) }}"****- "{{ nfs_dev_source_id | d(omit) 
}}"****- "{{ nfs_dev_target_id | d(omit) }}"*

Cheers,
--
Todd

On 6/23/23 3:09 PM, javed khan Siddque wrote:
The below task is not working as expected.
i am expecting it will print message for at least 2 times which is defined and skip rest 2.


TASK [Connect to Isilon Device] ****************************************************************************************************
skipping: [localhost]



---
- name: "Loop Iteration List"
hosts: localhost
vars:
nfs_prod_target_id: "Khan"
nfs_prod_source_id: "Siddque"
tasks:
- name: "Connect to Isilon Device"
ansible.builtin.debug:
msg: "{{item}}"
when: item isdefined
loop:
- "{{nfs_prod_source_id}}"
- "{{nfs_prod_target_id}}"
- "{{nfs_dev_source_id}}"
- "{{nfs_dev_target_id}}"
--

--
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/2366dc40-c7cb-234d-9fdb-f5cc110141f3%40gmail.com.

Reply via email to