Created
January 8, 2020 15:34
-
-
Save mohclips/c0253ad53d27b11dad535ada37a8e9ba to your computer and use it in GitHub Desktop.
Ansible json test
This file contains hidden or 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
--- | |
# vim: set ft=ansible ts=2 sw=2 et: | |
- hosts: localhost | |
gather_facts: no | |
connection: local | |
become: no | |
vars: | |
my_json: | | |
{ | |
"ami_out": { | |
"architecture": "x86_64", | |
"block_device_mapping": { | |
"/dev/sda1": { | |
"delete_on_termination": true, | |
"encrypted": false, | |
"size": 8, | |
"snapshot_id": "snap-0e24e63b741d17279", | |
"volume_type": "gp2" | |
}, | |
"/dev/sdb": { | |
"virtual_name": "ephemeral0" | |
}, | |
"/dev/sdc": { | |
"virtual_name": "ephemeral1" | |
} | |
} | |
} | |
} | |
tasks: | |
- name: Get block_device_mapping | |
set_fact: | |
blk_dev_map: "{{ (my_json | from_json).ami_out.block_device_mapping }}" | |
- debug: | |
msg: | |
- "Block Device Mapping" | |
- "{{ blk_dev_map }}" | |
- "First item of unordered dict" | |
- "{{ blk_dev_map | first }}" | |
- "Select only those objects that contain snpshot_id - by making it an ordered list first" | |
- "{{ blk_dev_map.values() | selectattr('snapshot_id', 'defined') | list }}" | |
- "First" | |
- "{{ blk_dev_map.values() | selectattr('snapshot_id', 'defined') | first }}" | |
- "Snapshot id" | |
- "{{ (blk_dev_map.values() | selectattr('snapshot_id', 'defined') | first)['snapshot_id'] }}" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment