Last active
October 18, 2017 09:39
-
-
Save halberom/e4d22d1049b8b2d2cb8eb4a0f6f94afe to your computer and use it in GitHub Desktop.
ansible - example of pulling out all az:subnets pairs from facts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[{ | |
"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>" | |
} | |
] | |
}] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- 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