On ansible [core 2.13.3] my var.yml file is like this :
vlans:
Servers:
vlan_id: 10
ip: 192.168.22.20
mask: 255.255.255.0
mode:
tagged: 1/1,1/2
untagged: 1/3
Clients:
vlan_id: 20
mode:
tagged: 1/1,1/3
Printers:
vlan_id: 30
The objective is to launch one task for each elements in vlans (Servers, Clients, Printers...) and for each mode (tagged, untagged, forbid...).
I successful iterate on vlans but not in vlans and mode. I can't find the good way to success nested loop. Try everything like subelement, with_subelements, loop_nested without success.
- name: Set member of vlan
arubaoss_vlan:
command: config_vlan_port
config: "create"
vlan_id: "{{ item.value.vlan_id }}"
port_id: "{{ item.value.port | default('') }}"
port_mode: "{{ item.value.mode | default('POM_UNTAGGED') | regex_replace('^tagged$', 'POM_TAGGED_STATIC') | regex_replace('^untagged$', 'POM_UNTAGGED') | regex_replace('^forbid$', 'POM_FORBIDDEN') }}"
api_version: "{{ ansible_api_version }}"
use_ssl: "{{ ansible_use_ssl }}"
port: "{{ ansible_port }}"
host: "{{ ansible_host }}"
username: "{{ ansible_user }}"
password: "{{ ansible_password }}"
loop: "{{ lookup('dict', vlans) }}"
when: "'port' in dict(item)"
tags:
- vlans
- vlans_mode
How I can make nested loop in Ansible ?
- name: Debug2
debug:
msg: "Name: {{ item.key }} - mode: {{ item.value.mode }}"
loop: "{{ lookup('dict', vlans) }}"
when: "'mode' in dict(item.value)"
tags:
- debug
Debug 2 return :
ok: [sw39stack01.group.corp] => (item={'key': 'Servers', 'value':
{'vlan_id': 10, 'ip': '192.168.22.20', 'mask': '255.255.255.0',
'mode': {'tagged': '1/1,1/2', 'untagged': '1/3'}}}) => {
"msg": "Name: Servers - mode: {'tagged': '1/1,1/2', 'untagged': '1/3'}" } ok: [sw39stack01.group.corp] => (item={'key': 'Clients',
'value': {'vlan_id': 20, 'mode': {'tagged': '1/1,1/3'}}}) => {
"msg": "Name: Clients - mode: {'tagged': '1/1,1/3'}" } skipping: [sw39stack01.group.corp] => (item={'key': 'Printers', 'value':
{'vlan_id': 30}}) => {
"ansible_loop_var": "item",
"item": {
"key": "Printers",
"value": {
"vlan_id": 30
}
} }
Best regards,