On 08. juni 2017 16:29, Rod Oliver wrote:
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.
---
- hosts: production
gather_facts: no
connection: local
vars:
provider:
host: "{{ inventory_hostname }}"
username: "{{ enc_username }}"
password: "{{ enc_password }}"
transport: cli
platform: cisco_nxos
tasks:
- include_vars: authentication.yml
- name: Run SHOW CDP NEI and store output
ntc_show_command:
command: "show cdp neig"
template_dir: "./ntc-templates/templates"
provider: "{{ provider }}"
register: cdp_nei
- name: Run SHOW INT STATUS and store output
ntc_show_command:
command: "show int status"
template_dir: "./ntc-templates/templates"
provider: "{{ provider }}"
register: int_stat
- name: Create interface description statements where needed
blockinfile:
dest: ./Output/cdp-output-processed
insertafter: EOF
marker: "!"
block: |
interface {{ item.local_interface }}
description ->{{ item.neighbor | regex_replace("\(.*") }}_{{
item.neighbor_interface }}
with_items: "{{ cdp_nei.response }}"
when: int_stat.response | search("{{ cdp_nei.response.item.neighbor
}}"_"{{ cdp_nei.response.item.local_interface }}")
The last task fails as below
TASK [Create interface description statements where needed]
************************************************************
[WARNING]: when statements should not include jinja2 templating delimiters
such as {{ }} or {% %}. Found:
int_stat.response | search("{{ cdp_nei.response.item.neighbor }}"_"{{
cdp_nei.response.item.local_interface }}")
fatal: [switch]: FAILED! => {"failed": true, "msg": "The conditional check
'int_stat.response | search(\"{{ cdp_nei.r
esponse.item.neighbor }}\"_\"{{ cdp_nei.response.item.local_interface
}}\")' failed. The error was: error while evaluati
ng conditional (int_stat.response | search(\"{{
cdp_nei.response.item.neighbor }}\"_\"{{ cdp_nei.response.item.local_int
erface }}\")): 'list object' has no attribute 'item'\n\nThe error appears
to have been in '/home/vagrant/ansible/Automat
ion/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 where ne
eded\n ^ here\n"}
Any tips on how to proceed?
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)
--
Kai Stian Olstad
--
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 post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/e42491b2-a784-ea06-34e3-de225366cd66%40olstad.com.
For more options, visit https://groups.google.com/d/optout.