-
-
Save zukko78/ed7aeaee526d24ba40fc3a7d438f7e61 to your computer and use it in GitHub Desktop.
ansible - convoluted json_query foo - reducing a nested dict to just bits we can use
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
--- | |
- hosts: localhost | |
connection: local | |
gather_facts: False | |
tasks: | |
# - include_vars: jsonfile.json | |
# | |
# - debug: | |
# var: ansible_devices | |
# | |
# - debug: | |
# var: item.value.partitions | |
# when: "item.value.partitions | length > 0" | |
# with_dict: "{{ ansible_devices }}" | |
# | |
# - name: Check for partitions on disk devices | |
# parted: | |
# device: "/dev/{{ item.key }}" | |
# unit: MiB | |
# when: item.partitions | length > 0 | |
# with_dict: "{{ ansible_devices }}" | |
# register: driveFStype | |
- include_vars: 01_parted.json | |
- debug: | |
msg: "{{ results | json_query(query) }}" | |
vars: | |
query: "[].{disk: disk.dev, partitions: partitions[?fstype=='ntfs']}" | |
#query: "[].disk.dev" | |
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
"results": [ | |
{ | |
"_ansible_item_result": true, | |
"_ansible_no_log": false, | |
"_ansible_parsed": true, | |
"changed": false, | |
"disk": { | |
"dev": "/dev/sda2", | |
"logical_block": 512, | |
"model": "Unknown", | |
"physical_block": 512, | |
"size": 32466.0, | |
"table": "loop", | |
"unit": "mib" | |
}, | |
"invocation": { | |
"module_args": { | |
"align": "optimal", | |
"device": "/dev/sda2", | |
"flags": null, | |
"label": null, | |
"name": null, | |
"number": null, | |
"part_end": "100%", | |
"part_start": "0%", | |
"part_type": "primary", | |
"state": "info", | |
"unit": "MiB" | |
} | |
}, | |
"item": "sda2", | |
"partitions": [ | |
{ | |
"begin": 0.0, | |
"end": 32466.0, | |
"flags": [], | |
"fstype": "ntfs", | |
"num": 1, | |
"size": 32466.0, | |
"unit": "mib" | |
} | |
], | |
"script": "unit 'MiB' print" | |
}, | |
{ | |
"_ansible_item_result": true, | |
"_ansible_no_log": false, | |
"_ansible_parsed": true, | |
"changed": false, | |
"disk": { | |
"dev": "/dev/sda1", | |
"logical_block": 512, | |
"model": "Unknown", | |
"physical_block": 512, | |
"size": 300.0, | |
"table": "loop", | |
"unit": "mib" | |
}, | |
"invocation": { | |
"module_args": { | |
"align": "optimal", | |
"device": "/dev/sda1", | |
"flags": null, | |
"label": null, | |
"name": null, | |
"number": null, | |
"part_end": "100%", | |
"part_start": "0%", | |
"part_type": "primary", | |
"state": "info", | |
"unit": "MiB" | |
} | |
}, | |
"item": "sda1", | |
"partitions": [ | |
{ | |
"begin": 0.0, | |
"end": 300.0, | |
"flags": [], | |
"fstype": "ntfs", | |
"num": 1, | |
"size": 300.0, | |
"unit": "mib" | |
} | |
], | |
"script": "unit 'MiB' print" | |
} | |
] |
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
PLAY [localhost] ********************************************************************************************************************** | |
TASK [include_vars] ******************************************************************************************************************* | |
ok: [localhost] | |
TASK [debug] ************************************************************************************************************************** | |
ok: [localhost] => { | |
"msg": [ | |
{ | |
"disk": "/dev/sda2", | |
"partitions": [ | |
{ | |
"begin": 0.0, | |
"end": 32466.0, | |
"flags": [], | |
"fstype": "ntfs", | |
"num": 1, | |
"size": 32466.0, | |
"unit": "mib" | |
} | |
] | |
}, | |
{ | |
"disk": "/dev/sda1", | |
"partitions": [ | |
{ | |
"begin": 0.0, | |
"end": 300.0, | |
"flags": [], | |
"fstype": "ntfs", | |
"num": 1, | |
"size": 300.0, | |
"unit": "mib" | |
} | |
] | |
} | |
] | |
} | |
PLAY RECAP **************************************************************************************************************************** | |
localhost : ok=2 changed=0 unreachable=0 failed=0 |
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
--- | |
- hosts: localhost | |
connection: local | |
gather_facts: False | |
tasks: | |
- setup: | |
filter: ansible_devices | |
# - include_vars: jsonfile.json | |
# - name: Check for partitions on disk devices | |
# parted: | |
# device: "/dev/{{ item.key }}" | |
# unit: MiB | |
# when: item.partitions | length > 0 | |
# with_dict: "{{ ansible_devices }}" | |
# register: driveFStype | |
- include_vars: | |
path: 01_parted.json | |
name: driveFStype | |
- name: mount ntfs partitions | |
mount: | |
path: "/mnt/{{ item.item }}" | |
src: "{{ item.disk.dev }}" | |
fstype: ntfs | |
when: "item.partitions | length > 0 and item.partitions | selectattr('fstype', 'equalto', 'ntfs') | list | length > 0" | |
with_items: "{{ driveFStype.results }}" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment