with_items
with_dict
第二个loop需要使用方括号[]来调用diction key, 不能使用{} 在大括号中,否则会报错。
"msg": "AnsibleError: template error while templating string: expected name or number.
{% for interf in ansible_net_interfaces.keys()|sort %}
{% if "bridged" in ansible_net_interfaces[interf].type and ansible_net_interfaces[interf].lineprotocol == "up" %}
interface {{interf}}
description ** Change from ansible **
no switchport
{% else %}
interface {{interf}}
description ** No change **
{% endif %}
{% endfor %}
vars:
vlans: {
"100": {"description": "floor_1", "ip":"192.168.10.1"}
"200": {"description": "floor_2", "ip":"192.168.20.1"},
"300": {"description": "floor_3", "ip":"192.168.30.1"}
}
{% for i, k in vlans.items() %}
vlan {{i}}
name {{k.description}}
{% endfor%}
hosts: all
gather_facts: no
connection: local
tasks:
- name: #Capture Show Commands
ios_command:
commands: sh int description | i Vl
register: description_output
- name: #Populate the interface list
set_fact:
SVI="{{ description_output.stdout[0] | regex_findall('Vl\\d{1,3}\\b') }}"
- name: #Check Individul SVI for ip helper address
register: SVI_output
ios_command:
commands: sh run interface {{ item }} | inc ip
with_items: "{{ SVI }}"
# - copy: content="{{ SVI_output|to_nice_json }}" dest="../outputs/SVI_Results.out"
- name: #Record output
lineinfile:
create: yes
path: ../outputs/SVI_Results.out
line:
- 'SWITCH==> {{inventory_hostname}} HAS Vlan interface==> {{item.item | to_nice_json }} WITH==> {{item.stdout_lines[0][0] | to_nice_json }}'
with_items: "{{ SVI_output.results }}"
# - name: debug
# debug: var={{ item.stdout_lines[0] }}
# with_items: "{{ SVI_output.results }}"