'Get Volume group attributes to be used in condition using Ansible

I would like to be able to get list of volume groups available and then use the attributes of the volume group to conditionally create the filesystem on volume which meets the Criteria

Criteria 1: Volume must not have name root in it 2: Volume must be at-least 2 GB

I have this logic

- name: list volume groups
  debug: msg="echo volume groups is  {{ item }}" 
  with_items: "{{ ansible_lvm.vgs }}

But am struggling to get that attribute of the vgs.

Here is snippet of ansible_lvm

"ansible_lvm": {
            "lvs": {
                "lvud01": {
                    "size_g": "0.50",
                    "vg": "vgud01"
                },
                "root": {
                    "size_g": "79.01",
                    "vg": "precise32"
                },
                "swap_1": {
                    "size_g": "0.75",
                    "vg": "precise32"
                }
            },
            "vgs": {
                "precise32": {
                    "free_g": "0",
                    "num_lvs": "2",
                    "num_pvs": "1",
                    "size_g": "79.76"
                },
                "vgud01": {
                    "free_g": "0.50",
                    "num_lvs": "1",
                    "num_pvs": "1",
                    "size_g": "1.00"
                }
            }


Solution 1:[1]

Just for everyones benefit and thanks @fishi

- name: list volume groups
      debug: msg="echo volume groups is  {{ item.key }} has {{ item.value.free_g }}" 
      with_dict: "{{ ansible_lvm.vgs }}"
      when: not item.key  == "root"

Solution 2:[2]

For those like me taken here by Google and wondering where this ansible_lvm dict comes from :

  • it is part of the facts gathered by the setup module
  • it is collected only when executed with super-user privileges

In other words, to get ansible_lvm :

  • with an ad-hoc command (the ?magic? lies in the -b) :

    ansible myManagedNode -m setup -kK -b

  • within a playbook :

    - hosts: myManagedNode
      become: yes
      tasks:
      - debug:
          var: ansible_lvm
    

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user3073335
Solution 2 Httqm