Skip to content

Instantly share code, notes, and snippets.

@mohclips
Created January 8, 2020 15:34
Show Gist options
  • Save mohclips/c0253ad53d27b11dad535ada37a8e9ba to your computer and use it in GitHub Desktop.
Save mohclips/c0253ad53d27b11dad535ada37a8e9ba to your computer and use it in GitHub Desktop.
Ansible json test
---
# 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