Skip to content

Instantly share code, notes, and snippets.

@mohclips
Last active October 24, 2021 21:56
Show Gist options
  • Save mohclips/f6caf9440d23dd18eb90feba15228c5b to your computer and use it in GitHub Desktop.
Save mohclips/f6caf9440d23dd18eb90feba15228c5b to your computer and use it in GitHub Desktop.
how to loop through a dict with lists and dicts that reference other vars dicts
---
#!/usr/bin/env ansible-playbook
# vim: noai:ts=2:sw=2:et
# how to loop through a dict with lists and dicts that reference other vars dicts
# note that the dict keys dont have '-' but '_' so that jinja2 doesnt try and parse them as subtraction.
- name: Test for Jon
connection: local
gather_facts: no
hosts: localhost
vars:
Networks:
External_uk_1a:
az: "uk-1a"
cidr: "192.168.1.0/24"
dns: [ 8.8.8.8, 8.8.4.4 ]
gateway_ip: "192.168.1.1"
router_name: "External-uk-1a"
ext_net: "inf_az1_ext-net02"
DMZ_uk_1a:
az: "uk-1a"
cidr: "10.1.1.0/24"
dns: [ 8.8.8.8, 8.8.4.4 ]
gateway_ip: "10.1.1.1"
router_name: "DMZ-uk-1a"
Protected_uk_1a:
az: "uk-1a"
cidr: "10.10.10.0/24"
dns: [ 8.8.8.8, 8.8.4.4 ]
gateway_ip: "10.10.10.1"
router_name: "Protected-uk-1a"
Firewalls:
- Fortigate_uk_1a:
ports:
- network: "{{ Networks.External_uk_1a }}"
security_group: "Any Any Permit"
- network: "{{ Networks.DMZ_uk_1a }}"
security_group: "Any Any Permit"
fixed_ip:
- "{{ Networks.DMZ_uk_1a.gateway_ip }}"
- network: "{{ Networks.Protected_uk_1a }}"
security_group: "Any Any Permit"
fixed_ip:
- "{{ Networks.Protected_uk_1a.gateway_ip }}"
tasks:
- debug: msg="{{ item.0.keys() }} {{ item.1.network }}"
#"
with_subelements:
- "{{ Firewalls }}"
- ports
# LOOK at the msg: line. If it was me i would add an additonal key/value pair to name the ports. I think you need this anyway when creating ports in OpenStack
# ansible-playbook jon1.yml
[WARNING]: provided hosts list is empty, only localhost is available
PLAY [Test for Jon] ************************************************************
TASK [debug] *******************************************************************
ok: [localhost] => (item=({u'Fortigate_uk_1a': None}, {u'security_group': u'Any Any Permit', u'network': {u'ext_net': u'inf_az1_ext-net02', u'gateway_ip': u'192.168.1.1', u'dns': [u'8.8.8.8', u'8.8.4.4'], u'cidr': u'192.168.1.0/24', u'az': u'uk-1a', u'router_name': u'External-uk-1a'}})) => {
"item": [
{
"Fortigate_uk_1a": null
},
{
"network": {
"az": "uk-1a",
"cidr": "192.168.1.0/24",
"dns": [
"8.8.8.8",
"8.8.4.4"
],
"ext_net": "inf_az1_ext-net02",
"gateway_ip": "192.168.1.1",
"router_name": "External-uk-1a"
},
"security_group": "Any Any Permit"
}
],
"msg": "[u'Fortigate_uk_1a'] {u'ext_net': u'inf_az1_ext-net02', u'gateway_ip': u'192.168.1.1', u'dns': [u'8.8.8.8', u'8.8.4.4'], u'cidr': u'192.168.1.0/24', u'az': u'uk-1a', u'router_name': u'External-uk-1a'}"
}
ok: [localhost] => (item=({u'Fortigate_uk_1a': None}, {u'security_group': u'Any Any Permit', u'fixed_ip': [u'10.1.1.1'], u'network': {u'gateway_ip': u'10.1.1.1', u'cidr': u'10.1.1.0/24', u'az': u'uk-1a', u'router_name': u'DMZ-uk-1a', u'dns': [u'8.8.8.8', u'8.8.4.4']}})) => {
"item": [
{
"Fortigate_uk_1a": null
},
{
"fixed_ip": [
"10.1.1.1"
],
"network": {
"az": "uk-1a",
"cidr": "10.1.1.0/24",
"dns": [
"8.8.8.8",
"8.8.4.4"
],
"gateway_ip": "10.1.1.1",
"router_name": "DMZ-uk-1a"
},
"security_group": "Any Any Permit"
}
],
"msg": "[u'Fortigate_uk_1a'] {u'gateway_ip': u'10.1.1.1', u'cidr': u'10.1.1.0/24', u'az': u'uk-1a', u'router_name': u'DMZ-uk-1a', u'dns': [u'8.8.8.8', u'8.8.4.4']}"
}
ok: [localhost] => (item=({u'Fortigate_uk_1a': None}, {u'security_group': u'Any Any Permit', u'fixed_ip': [u'10.10.10.1'], u'network': {u'gateway_ip': u'10.10.10.1', u'cidr': u'10.10.10.0/24', u'az': u'uk-1a', u'router_name': u'Protected-uk-1a', u'dns': [u'8.8.8.8', u'8.8.4.4']}})) => {
"item": [
{
"Fortigate_uk_1a": null
},
{
"fixed_ip": [
"10.10.10.1"
],
"network": {
"az": "uk-1a",
"cidr": "10.10.10.0/24",
"dns": [
"8.8.8.8",
"8.8.4.4"
],
"gateway_ip": "10.10.10.1",
"router_name": "Protected-uk-1a"
},
"security_group": "Any Any Permit"
}
],
"msg": "[u'Fortigate_uk_1a'] {u'gateway_ip': u'10.10.10.1', u'cidr': u'10.10.10.0/24', u'az': u'uk-1a', u'router_name': u'Protected-uk-1a', u'dns': [u'8.8.8.8', u'8.8.4.4']}"
}
PLAY RECAP *********************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment