1

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?

2
  • 1
    I'm a little confused about what you're trying to do. For example, how are you getting from av_root_snapshot to av_root_snap1? Commented May 3, 2023 at 2:41
  • sorry, typo. s/snap1/snapshot Commented May 3, 2023 at 6:42

1 Answer 1

2

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') }}"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.