print highest number of volume group

Hello,

I

Is there a way to print only volume group name on server with the highest number? for eg if the server has vg00, vg01, vg02 I want to have print only highest number vg which is vg02. I am trying to use with ansible_lvm.vgs but couldnt able to. With shell command I am able to get the info but wondering if its possible with ansible_lvm facts.

  • debug:
    var: item
    with_items:

  • “{{ ansible_lvm.vgs }}”

  • shell: vgs --noheadings | grep -E “vg[0-9]” | sort | tail -n1 | awk ‘{print $1}’
    register: highest

  • debug:
    var: highest.stdout

Output:

PLAY [all] *****************************************************************************************************************************************************************

TASK [Gathering Facts] *****************************************************************************************************************************************************
ok: [rhel7.lab.com]

TASK [debug] ***************************************************************************************************************************************************************
ok: [rhel7.lab.com] => (item={‘vg09’: {‘free_g’: ‘5.00’, ‘size_g’: ‘5.00’, ‘num_lvs’: ‘0’, ‘num_pvs’: ‘1’}, ‘vg00’: {‘free_g’: ‘10.00’, ‘size_g’: ‘10.00’, ‘num_lvs’: ‘0’, ‘num_pvs’: ‘1’}}) =>
ansible_loop_var: item
item:
vg00:
free_g: ‘10.00’
num_lvs: ‘0’
num_pvs: ‘1’
size_g: ‘10.00’
vg09:
free_g: ‘5.00’
num_lvs: ‘0’
num_pvs: ‘1’
size_g: ‘5.00’

TASK [shell] ***************************************************************************************************************************************************************
changed: [rhel7.lab.com]

TASK [debug] ***************************************************************************************************************************************************************
ok: [rhel7.lab.com] =>
highest.stdout: vg09

PLAY RECAP *****************************************************************************************************************************************************************
rhel7.lab.com : ok=5 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Thank you.

Try:

"{{ ansible_lvm.lvs | select('match', '^vg[0-9]+') | list | sort | last }}"

Thank you Dick Visser. It’s working for me.

its working now. Below is the code

  • set_fact:
    highest_vg: “{{ ansible_lvm.vgs | select(‘match’, ‘^vg[0-9]+’) | list | sort | last }}”

  • debug:
    var: highest_vg

Thank you Dick Visser again for your help!