How to declare a list of lists of lists

I need to set up a list of volume groups. for each volume group there will be an attribute name and an attribute state. under each volume group is a list of one or more volumes. For each volume, there are attributes name, size, file system type, and mount point. Under each volume is a list of one or more disks. for each disk there are attributes bdevice and cdevice to indicate /dev/sda or /dev/sda1.

Something like this:

disks:

  • vgname: vg1
    state: present
  • lvname: lv1
    mntp: /u01
    lvsize: 100G
    fstype: xfs
    disk:
  • {device: /dev/sda, pvname: /dev/sda1}

but this doesn’t work. Lastly, I would like a technique to know how many elements of disks there are because if there is only one disk I do not want to use lvm.

- For each volume group there will be an attribute name and an attribute
  state
- Under each volume group is a list of one or more volumes.
- For each volume, there are attributes name,
  size, file system type, and mount point
- Under each volume is a list of one or more disks.
- for each disk there are attributes bdevice and cdevice to
  indicate /dev/sda or /dev/sda1.

Something like this:

disks:
  - vgname: vg1
    state: present

      volumes:

      - lvname: lv1
        mntp: /u01
        lvsize: 100G
        fstype: xfs
        disk:
          - device: /dev/sda
            pvname: /dev/sda1

The list of volumes must be an attribute too.

Is there a way to walk volumes and disk lists? I can only find examples of walking disks and volumes with the subelements construct. Sorry there is both a disk and disks list construct.

Yes. It is. Use "include_tasks" in the loop with subelements volumes. In
the included tasks loop "item.1.disk".

> Is there a way to walk *volumes* and *disk* lists?

Try this loop

    - debug:
        msg: "{{ outer_item.0.vgname }} {{ outer_item.1.lvname }}"
      loop: "{{ lookup('subelements', disks, 'volumes') }}"
      loop_control:
        loop_var: outer_item
        label: "{{ outer_item.0.vgname }}"

If it works then create a file to be included in the loop. For example

  > cat test.yml
  - debug:
      msg:
        group [{{ outer_item.0.vgname }}]
        volume [{{ outer_item.1.lvname }}]
        device [{{ item.device }}]
        pvname [{{ item.pvname }}]
    loop: "{{ outer_item.1.disk }}"

and include it in the loop. For example

    - include_tasks: test.yml
      loop: "{{ lookup('subelements', disks, 'volumes') }}"
      loop_control:
        loop_var: outer_item
        label: "{{ outer_item.0.vgname }}"

The result should be a list of messages with all combinations.