I have the below output that I'm trying to parse and filter:
- name: lvs
debug:
msg: "{{ ansible_lvm.lvs }}"
ok: [localhost] => {
"msg": {
"av_root_snapshot": {
"size_g": "4.00",
"vg": "vg_root"
},
"av_var_snapshot": {
"size_g": "5.00",
"vg": "vg_root"
},
"lv_root": {
"size_g": "20.00",
"vg": "vg_root"
},
"lv_var": {
"size_g": "15.00",
"vg": "vg_root"
}
}
}
I'm trying to create a list of out it so it returns the following:
av_root_snapshot vg_root
av_var_snapshot vg_root
I am able to get the values of 'vg' via:
- name: get snap
debug:
msg: "{{ ansible_lvm.lvs | json_query('*.vg') }}"
But what I can't seem to figure out is how to filter the root object names that end in *snapshot and parse the value of "vg". I'm trying to do this so I can also delete those snapshots.
How do I properly filter the above output and parse for the information I need?
Q: "Delete object names that end 'snapshot' and parse the value of vg."
A: Reject the keys matching the regex
lvs_no_snapshot: "{{ ansible_lvm.lvs|dict2items|
rejectattr('key', 'regex', '^.*_snapshot$')|
items2dict }}"
gives
lvs_no_snapshot:
lv_root:
size_g: '20.00'
vg: vg_root
lv_var:
size_g: '15.00'
vg: vg_root
Then, you can get the list of the attributes vg
vg: "{{ lvs_no_snapshot|json_query('*.vg') }}"