Skip to content

Instantly share code, notes, and snippets.

@halberom
Last active October 18, 2017 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halberom/e4d22d1049b8b2d2cb8eb4a0f6f94afe to your computer and use it in GitHub Desktop.
Save halberom/e4d22d1049b8b2d2cb8eb4a0f6f94afe to your computer and use it in GitHub Desktop.
ansible - example of pulling out all az:subnets pairs from facts
[{
"subnets": [
{
"availability_zone": "eu-west-1c",
"available_ip_address_count": 29,
"cidr_block": "<REDACTED>",
"default_for_az": "false",
"id": "subnet-<id>",
"map_public_ip_on_launch": "false",
"state": "available",
"tags": {
"Description": "subnet in AZc",
"Name": "<subname>",
"aws:cloudformation:logical-id": "subnetAZc",
"aws:cloudformation:stack-id": "<verylongcfnid>",
"aws:cloudformation:stack-name": "<weirdcfnstackname>"
},
"vpc_id": "vpc-<id>"
}
]
},
{
"subnets": [
{
"availability_zone": "eu-west-1d",
"available_ip_address_count": 29,
"cidr_block": "<REDACTED>",
"default_for_az": "false",
"id": "subnet-<id>",
"map_public_ip_on_launch": "false",
"state": "available",
"tags": {
"Description": "subnet in AZc",
"Name": "<subname>",
"aws:cloudformation:logical-id": "subnetAZc",
"aws:cloudformation:stack-id": "<verylongcfnid>",
"aws:cloudformation:stack-name": "<weirdcfnstackname>"
},
"vpc_id": "vpc-<id>"
}
]
}]
PLAY [localhost] ***************************************************************************************************************************************************************************************************************************************************************
TASK [set_fact] ****************************************************************************************************************************************************************************************************************************************************************
ok: [localhost]
TASK [set_fact] ****************************************************************************************************************************************************************************************************************************************************************
ok: [localhost]
TASK [set_fact] ****************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => (item=[u'eu-west-1c', u'subnet-<id>'])
ok: [localhost] => (item=[u'eu-west-1d', u'subnet-<id>'])
TASK [debug] *******************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"mydict": {
"eu-west-1c": "subnet-<id>",
"eu-west-1d": "subnet-<id>"
}
}
PLAY RECAP *********************************************************************************************************************************************************************************************************************************************************************
localhost : ok=4 changed=0 unreachable=0 failed=0
---
- hosts: localhost
gather_facts: False
tasks:
- set_fact:
myitems: "{{ lookup('file', 'foo.json')|from_json }}"
- set_fact:
myfilter: "{{ myitems|json_query('[].subnets[].[availability_zone, id]') }}"
- set_fact:
mydict: "{{ mydict|default({})|combine({item[0]: item[1]}) }}"
with_items:
# myfilter is a nested list structure, so we pass it as an item, instead of doing
# with_items: "{{ myfilter }}"
- "{{ myfilter }}"
- debug: var=mydict
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment