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.
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.
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.
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”}
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.
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.
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.