# Loop over vars not working

**URL:** <https://forum.ansible.com/t/loop-over-vars-not-working/34810>\
**Category:** Ansible Project\
**Created:** [March 19, 2021, 1:24pm UTC](https://forum.ansible.com/t/loop-over-vars-not-working/34810 "2021-03-19T13:24:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sharon\_Day](https://avatars.discourse-cdn.com/v4/letter/s/94ad74/32.png) [@Sharon\_Day](https://forum.ansible.com/u/Sharon_Day)\
**Post date:** [March 19, 2021, 1:24pm UTC](https://forum.ansible.com/t/loop-over-vars-not-working/34810/1 "2021-03-19T13:24:36Z")

</div>

I have a playbook that is doing two lookup for the next available Infoblox network subnet in two different networks. I set the facts and I’m trying to loop over the results of the two lookups. I print the results of the lookup so I can see I’m getting back what I expected. I think the issue is I’m trying to create the network from the lookup results with a single task. If I break it out into two different task one for each network returned from the lookup it fails. Its somehow related to the loop statement. I’ve changed the IPs so not to show our acutal IP space.

Here’s my playbook:

vars:  
verizon: 1.1.1.0/20  
at\_t: 1.1.2.0/20

- name: Return the next available Verizon IP subnet for network  
set\_fact:  
verizon\_pe\_ce: “{{ lookup(‘nios\_next\_network’, verizon, cidr=30, provider=cli\_infoblox ) }}”  
delegate\_to: localhost

- name: Print the subnet  
debug:  
var: verizon\_pe\_ce

- name: Return the next available at\_t IP subnet for network  
set\_fact:  
att\_pe\_ce: “{{ lookup(‘nios\_next\_network’, at\_t, cidr=30, provider=cli\_infoblox ) }}”  
delegate\_to: localhost

- name: Print the subnet  
debug:  
var: att\_pe\_ce

- name: configure a network ipv4  
nios\_network:  
network: “{{ item }}”  
comment: WAN ansible assignment  
extattrs:  
Network\_Name: ansible\_test\_assignment  
Network\_Environment: CLIENT\_REMOTE  
Client\_Site\_#: “{{ client\_num }}-{{ client\_name }} - ({{ city }}, {{ state }})”  
CL\_RTR\_INT: [“RTR A”, “WAN Network \_30”]  
VRF\_NAME: CL  
Network\_Container: SNG  
state: present  
provider: “{{ cli\_infoblox }}”  
loop:

- verizon\_pe\_ce

- att\_pe\_ce  
delegate\_to: localhost

This is the output from the lookups:

TASK [Print the subnet] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /automation/ansible/playbooks/infoblox/next\_available\_network.yml:28  
ok: [10.x.x.x] =\> {  
“verizon\_pe\_ce”: [  
“1.1.1.36/30”  
]  
}

TASK [Print the subnet] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /automation/ansible/playbooks/infoblox/next\_available\_network.yml:36  
ok: [10.x.x.x] =\> {  
“att\_pe\_ce”: [  
“1.1.2.36/30”  
]  
}

The error is the same for both the verizon\_pe\_ce and the att\_pe\_ce. Here’s the output related to the att\_pe\_ce.

TypeError: ‘NoneType’ object is not iterable  
failed: [10.x.x.x] (item=att\_pe\_ce) =\> {  
“ansible\_loop\_var”: “item”,  
“changed”: false,  
“item”: “att\_pe\_ce”,  
“module\_stderr”: “Traceback (most recent call last):\n File "/home/e001gx/.ansible/tmp/ansible-tmp-1616159168.3519313-122151-53449377816842/AnsiballZ\_nios\_network.py", line 102, in \n \_ansiballz\_main()\n File "/home/e001gx/.ansible/tmp/ansible-tmp-1616159168.3519313-122151-53449377816842/AnsiballZ\_nios\_network.py", line 94, in \_ansiballz\_main\n invoke\_module(zipped\_mod, temp\_path, ANSIBALLZ\_PARAMS)\n File "/home/e0013192gx/.ansible/tmp/ansible-tmp-1616159168.3519313-122151-53449377816842/AnsiballZ\_nios\_network.py", line 40, in invoke\_module\n runpy.run\_module(mod\_name=‘ansible\_collections.infoblox.nios\_modules.plugins.modules.nios\_network’, init\_globals=None, run\_name=‘ **main** ’, alter\_sys=True)\n File "/opt/rh/rh-python36/root/usr/lib64/python3.6/runpy.py", line 205, in run\_module\n return \_run\_module\_code(code, init\_globals, run\_name, mod\_spec)\n File "/opt/rh/rh-python36/root/usr/lib64/python3.6/runpy.py", line 96, in \_run\_module\_code\n mod\_name, mod\_spec, pkg\_name, script\_name)\n File "/opt/rh/rh-python36/root/usr/lib64/python3.6/runpy.py", line 85, in \_run\_code\n exec(code, run\_globals)\n File "/tmp/ansible\_nios\_network\_payload\_5eez7968/ansible\_nios\_network\_payload.zip/ansible\_collections/infoblox/nios\_modules/plugins/modules/nios\_network.py", line 302, in \n File "/tmp/ansible\_nios\_network\_payload\_5eez7968/ansible\_nios\_network\_payload.zip/ansible\_collections/infoblox/nios\_modules/plugins/modules/nios\_network.py", line 290, in main\nTypeError: ‘NoneType’ object is not iterable\n”,  
“module\_stdout”: “”,  
“msg”: “MODULE FAILURE\nSee stdout/stderr for the exact error”,  
“rc”: 1  
}

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [March 19, 2021, 2:37pm UTC](https://forum.ansible.com/t/loop-over-vars-not-working/34810/2 "2021-03-19T14:37:15Z")

</div>

You are looping over 2 strings, loop does not template by default:

&nbsp;&nbsp;&nbsp;&nbsp;loop: '{{ [verizon\_pe\_ce, att\_pe\_ce] }}'

or  
&nbsp;&nbsp;&nbsp;&nbsp;loop:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- ' {{verizon\_pe\_ce }}'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- '{{ att\_pe\_ce }}'

or keep loop as you have it but then use

network: "{{ q('vars', item)[0] }}"

---

<div class="post-metadata">

**Author:** ![Sharon\_Day](https://avatars.discourse-cdn.com/v4/letter/s/94ad74/32.png) [@Sharon\_Day](https://forum.ansible.com/u/Sharon_Day)\
**Post date:** [March 19, 2021, 5:09pm UTC](https://forum.ansible.com/t/loop-over-vars-not-working/34810/3 "2021-03-19T17:09:16Z")

</div>

I tried,

loop: ‘{{ [verizon\_pe\_ce, att\_pe\_ce] }}’

And

loop:

- ’ {{verizon\_pe\_ce }}’
- ‘{{ att\_pe\_ce }}’

Both fail with the same error message. I’m not sure what you meant by your third example.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [March 19, 2021, 5:19pm UTC](https://forum.ansible.com/t/loop-over-vars-not-working/34810/4 "2021-03-19T17:19:13Z")

</div>

Those were correcting your syntax, but that aside, the module has a  
bug (always the case if you see MODULE\_FAILURE).

---

<div class="post-metadata">

**Author:** ![Gaetan\_QUENTIN](https://avatars.discourse-cdn.com/v4/letter/g/e9bcb4/32.png) [@Gaetan\_QUENTIN](https://forum.ansible.com/u/Gaetan_QUENTIN)\
**Post date:** [June 22, 2021, 9:08pm UTC](https://forum.ansible.com/t/loop-over-vars-not-working/34810/5 "2021-06-22T21:08:50Z")

</div>

Hi,

My goal is to iterate items on a json list response, and find the good item. The good item is the one on which all test tasks succeed. Tests are done in the included\_task yaml file.

But my playbook do not work, i am unable to break the outer calling loop task if i found the good item from within the called included task list.

The outer task :

- name: set item\_ok  
set\_fact:  
item\_ok: false

- name: call included task for each items  
include\_tasks: “tests\_on\_item.yml”  
loop: “{{ item\_list }}”  
loop\_control:  
loop\_var: outer\_item  
when: item\_ok is false

and inner task , tests\_on\_item.yml:

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [June 23, 2021, 5:59am UTC](https://forum.ansible.com/t/loop-over-vars-not-working/34810/6 "2021-06-23T05:59:09Z")

</div>

It's not possible to break a loop. In some cases you can use  
registered value to skip the rest of the list. But, there might be a  
solution to your use-case: "Find the first item in a list that meets  
a list of conditions". For example, given the list

&nbsp;&nbsp;&nbsp;&nbsp;\_list:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- {a1: A, a2: 1, a3: first}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- {a1: A, a2: 2, a3: second}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- {a1: B, a2: 3, a3: third}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- {a1: A, a2: 2, a3: last}

find the first item that meets the conditions

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- a1 == 'A'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- a2 == 2

The task

&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x: "{{ \_list|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;selectattr('a1', 'eq', 'A')|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;selectattr('a2', 'eq', 2)|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;first }}"

gives

&nbsp;&nbsp;x:  
&nbsp;&nbsp;&nbsp;&nbsp;a1: A  
&nbsp;&nbsp;&nbsp;&nbsp;a2: 2  
&nbsp;&nbsp;&nbsp;&nbsp;a3: second

If you need the index of the item

&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i: "{{ \_list.index(x) }}"

gives

&nbsp;&nbsp;i: '1'

The next option is \*json\_query\*. The task below gives the same result

&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x: "{{ \_list|json\_query(query)|first }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;query: "[?a1 == 'A']|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[?a2 == `2`]"

A custom filter might be the next option to improve the efficiency of  
searching large lists.
