Last active
June 15, 2017 16:20
-
-
Save halberom/af695bc02fbe19a9e4d2d6667300bb92 to your computer and use it in GitHub Desktop.
ansible - example of getting snapshot_ids from ami_find results for tagging
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 | |
connection: local | |
# don't need these with the power of jmespath | |
#vars: | |
# snapshot_ids: [] | |
tasks: | |
# I'm not actually running ami_find, i just happen to have a json file handy | |
- set_fact: | |
ami_find_results: "{{ lookup('file', '01_foo.json') | from_json }}" | |
- debug: | |
var: ami_find_results | |
# little bit of json query + with_items | |
#- set_fact: | |
# snapshot_ids: "{{ snapshot_ids|union(item|json_query('block_device_mapping.*.snapshot_id')|list) }}" | |
# with_items: "{{ ami_find_results }}" | |
# or just use the full power of json_query, which we need to sum at the end to flatten | |
# might be able to remove that too if jmespath is tweaked | |
# what this is actually doing (see http://jmespath.org/tutorial.html and other docs for full info) | |
# [] - the top level of ami_find_results is a list | |
# .block_device_mapping - pretty obvious, access the block_device_mapping key per list item | |
# .* - wildcard | |
# .snapshot_id - find the snapshot_id | |
# sum(start=[]), jinja way of flattening lists. needed because we get a list of snapshot_ids per item in the results | |
- set_fact: | |
snapshot_ids: "{{ ami_find_results|json_query('[].block_device_mapping.*.snapshot_id')|sum(start=[]) }}" | |
- debug: | |
var: snapshot_ids |
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
[ | |
{ | |
"ami_id": "abc123", | |
"architecture": "x86_64", | |
"block_device_mapping": { | |
"/dev/sda1": { | |
"delete_on_termination": true, | |
"encrypted": true, | |
"size": 40, | |
"snapshot_id": "snap-abc123", | |
"volume_type": "gp2" | |
}, | |
"/dev/sdb": { | |
"delete_on_termination": true, | |
"encrypted": true, | |
"size": 50, | |
"snapshot_id": "snap-efg123", | |
"volume_type": "gp2" | |
}, | |
"/dev/sdc": { | |
"delete_on_termination": true, | |
"encrypted": true, | |
"size": 50, | |
"snapshot_id": "snap-hij123", | |
"volume_type": "gp2" | |
} | |
}, | |
"description": "redacted", | |
"hypervisor": "xen", | |
"is_public": false, | |
"location": "redacted", | |
"name": "redactedl", | |
"owner_id": "redacted", | |
"platform": null, | |
"root_device_name": "/dev/sda1", | |
"root_device_type": "ebs", | |
"state": "available", | |
"tags": { | |
"Name": "redacted" | |
}, | |
"virtualization_type": "hvm" | |
}, | |
{ | |
"ami_id": "efg123", | |
"architecture": "x86_64", | |
"block_device_mapping": { | |
"/dev/sda1": { | |
"delete_on_termination": true, | |
"encrypted": true, | |
"size": 40, | |
"snapshot_id": "blahblah", | |
"volume_type": "gp2" | |
}, | |
"/dev/sdb": { | |
"delete_on_termination": true, | |
"encrypted": true, | |
"size": 50, | |
"snapshot_id": "blahblah123", | |
"volume_type": "gp2" | |
}, | |
"/dev/sdc": { | |
"delete_on_termination": true, | |
"encrypted": true, | |
"size": 50, | |
"snapshot_id": "blahblah456", | |
"volume_type": "gp2" | |
}, | |
"/dev/sdd": { | |
"delete_on_termination": true, | |
"encrypted": true, | |
"size": 8, | |
"snapshot_id": "blahblah789", | |
"volume_type": "gp2" | |
} | |
}, | |
"description": "redacted", | |
"hypervisor": "xen", | |
"is_public": false, | |
"location": "redacted", | |
"name": "redacted", | |
"owner_id": "redacted", | |
"platform": null, | |
"root_device_name": "/dev/sda1", | |
"root_device_type": "ebs", | |
"state": "available", | |
"tags": { | |
"Name": "redacted" | |
}, | |
"virtualization_type": "hvm" | |
} | |
] |
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
... | |
TASK [debug] ******************************************************************************************************************************************************************************************************************************************************************* | |
ok: [localhost] => { | |
"snapshot_ids": [ | |
"snap-efg123", | |
"snap-abc123", | |
"snap-hij123", | |
"blahblah789", | |
"blahblah123", | |
"blahblah", | |
"blahblah456" | |
] | |
} | |
PLAY RECAP ********************************************************************************************************************************************************************************************************************************************************************* | |
localhost : ok=4 changed=0 unreachable=0 failed=0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment