# Search for value in variable array

**URL:** <https://forum.ansible.com/t/search-for-value-in-variable-array/24683>\
**Category:** Ansible Project\
**Created:** [June 8, 2017, 2:29pm UTC](https://forum.ansible.com/t/search-for-value-in-variable-array/24683 "2017-06-08T14:29:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![iamroddo](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/iamroddo/32/321_2.png) [@iamroddo](https://forum.ansible.com/u/iamroddo)\
**Post date:** [June 8, 2017, 2:29pm UTC](https://forum.ansible.com/t/search-for-value-in-variable-array/24683/1 "2017-06-08T14:29:13Z")

</div>

I have created a couple of arrays using responses from commands via ntc\_ansible to a Cisco switch, one containing CDP neighbor information (with keys “local\_interface”, “neighbor” and “neighbor\_interface”) and the other from a listing of interfaces on the switch. I want to use the presence of a combination of the “neighbor” and “neighbor\_interface” values (from the CDP neighbor array) in the interface list array as a condition on the running of a task.

The playbook currently looks that below.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [June 8, 2017, 2:48pm UTC](https://forum.ansible.com/t/search-for-value-in-variable-array/24683/2 "2017-06-08T14:48:40Z")

</div>

Do as the warning say, remove {{ }}. So something like this might work

when: int\_stat.response | search(cdp\_nei.response.item.neighbor ~ "\_" ~ cdp\_nei.response.item.local\_interface)

---

<div class="post-metadata">

**Author:** ![iamroddo](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/iamroddo/32/321_2.png) [@iamroddo](https://forum.ansible.com/u/iamroddo)\
**Post date:** [June 8, 2017, 3:05pm UTC](https://forum.ansible.com/t/search-for-value-in-variable-array/24683/3 "2017-06-08T15:05:16Z")

</div>

Thanks for your respoinse Kai.

I changed the conditional statement to that below. It wasn’t what I really wanted but I figured if it worked I could build on it. I got a further error.

Statement:  
when: int\_stat.response | search(cdp\_nei.response.item.neighbor)

Error:

TASK [Create interface description statements where needed] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

fatal: [switch]: FAILED! =\> {“failed”: true, “msg”: “The conditional check ‘int\_stat.response | search(cdp\_nei.respon  
se.item.neighbor)’ failed. The error was: error while evaluating conditional (int\_stat.response | search(cdp\_nei.respons  
e.item.neighbor)): ‘list object’ has no attribute ‘item’\n\nThe error appears to have been in ‘/home/vagrant/ansible/Aut  
omation/cdp-description.yml’: line 26, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem  
.\n\nThe offending line appears to be:\n\n register: int\_stat\n - name: Create interface description statements wher  
e needed\n ^ here\n”}

I tried to surround cdp\_nei.response.item.neighbor with “” and got yet a different error.

Statement:  
when: int\_stat.response | search(“cdp\_nei.response.item.neighbor”)

Error:

TASK [Create interface description statements where needed] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

fatal: [switch]: FAILED! =\> {“failed”: true, “msg”: “The conditional check ‘int\_stat.response | search("cdp\_nei.resp  
onse.item.neighbor")’ failed. The error was: Unexpected templating type error occurred on ({% if int\_stat.response | se  
arch("cdp\_nei.response.item.neighbor") %} True {% else %} False {% endif %}): expected string or buffer\n\nThe error a  
ppears to have been in ‘/home/vagrant/ansible/Automation/cdp-description.yml’: line 26, column 5, but may\nbe elsewhere  
in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n register: int\_stat\n - n  
ame: Create interface description statements where needed\n ^ here\n”}

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [June 8, 2017, 3:17pm UTC](https://forum.ansible.com/t/search-for-value-in-variable-array/24683/4 "2017-06-08T15:17:28Z")

</div>

> Thanks for your respoinse Kai.
> 
> I changed the conditional statement to that below. It wasn't what I really  
> wanted but I figured if it worked I could build on it. I got a further  
> error.
> 
> Statement:  
> &nbsp;&nbsp;&nbsp;when: int\_stat.response | search(cdp\_nei.response.item.neighbor)
> 
> Error:  
> TASK [Create interface description statements where needed]  
> \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
> 
> fatal: [switch]: FAILED! =\> {"failed": true, "msg": "The conditional check  
> 'int\_stat.response | search(cdp\_nei.respon  
> se.item.neighbor)' failed. The error was: error while evaluating  
> conditional (int\_stat.response | search(cdp\_nei.respons  
> e.item.neighbor)): 'list object' has no attribute 'item'\n\nThe error  
> appears to have been in '/home/vagrant/ansible/Aut  
> omation/cdp-description.yml': line 26, column 5, but may\nbe elsewhere in  
> the file depending on the exact syntax problem  
> .\n\nThe offending line appears to be:\n\n register: int\_stat\n - name:  
> Create interface description statements wher  
> e needed\n ^ here\n"}

The message says cdp\_nei.response does not contain the attribute item.  
So what is the content of cdp\_nei.response?

- debug: var=cdp\_nei.response

Will give you the content.

> I tried to surround cdp\_nei.response.item.neighbor with "" and got yet a  
> different error.
> 
> Statement:  
> &nbsp;&nbsp;&nbsp;when: int\_stat.response | search("cdp\_nei.response.item.neighbor")

With "" it will be the literal string cdp\_nei.response.item.neighbor and not the content of the variable.

I also suspect that int\_stat.response and is not a string but a list. The search filter need a string.

---

<div class="post-metadata">

**Author:** ![iamroddo](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/iamroddo/32/321_2.png) [@iamroddo](https://forum.ansible.com/u/iamroddo)\
**Post date:** [June 9, 2017, 6:43am UTC](https://forum.ansible.com/t/search-for-value-in-variable-array/24683/5 "2017-06-09T06:43:33Z")

</div>

Thanks again for your reply. I think I have a faulty understanding with the way that loops and conditionals apply in Ansible.

cdp\_nei.response is a list of dictionaries (containing values for interface name, neighbor name and neighbor interface) and I’m using that as an input for a loop to build the configuration of the description of the interface of switch ports (a combination of neighbor name and neighbor interface). This works without the conditional.

I want to add a conditional that only runs the loop element when neighbor name is not present in int\_stat.response (also a list of dictionaries) which contains the configured state of the interfaces. Basically the idea is to create the configuration template to reconfigure the interface description where it is wrong but not otherwise.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [June 20, 2017, 6:46pm UTC](https://forum.ansible.com/t/search-for-value-in-variable-array/24683/6 "2017-06-20T18:46:47Z")

</div>

If I understand you correctly you are looking for a loop in a loop.  
Do do that in Ansible check out loop control  
[https://docs.ansible.com/ansible/playbooks\_loops.html#loop-control](https://docs.ansible.com/ansible/playbooks_loops.html#loop-control)
