Search arrays inside ansible msg output

Viewed 38

Ansible-newbie here.

I am struggling with understanding how to check if a value exists in an array returned from a previous task. Im trying to search if a volume exists in any array of volumes returned from a search:

  - name: Get volume pools data
    community.libvirt.virt_pool:
      command: facts
    changed_when: false

  - debug: msg="{{ ansible_libvirt_pools }}.stdout_lines"

this returns output like this:

ok: [server] => {
    "msg": "{'default': {'status': 'running', 'size_total': '75125227520', 'size_used': '14622126080', 'size_available': '60503101440', 'autostart': 'yes', 'persistent': 'yes', 'state': 'active', 'path': '/var/lib/libvirt/images', 'type': 'dir', 'uuid': '5f6e9ba1-6a50-4313-9aa6-0981448aff0a', 'volume_count': 5, 'volumes': ['centos-8-stream.raw', 'ubuntu-20.04-server-cloudimg-amd64.img', 'ubuntu-server-20.04.raw', 'vyos-1.4-rolling-202201080317-amd64.iso', 'CentOS-Stream-GenericCloud-8-20201019.1.x86_64.qcow2']}, 'infra0_pool_0': {'status': 'running', 'size_total': '11998523817984', 'size_used': '294205259776', 'size_available': '11704318558208', 'autostart': 'yes', 'persistent': 'yes', 'state': 'active', 'path': '/dev/almalinux_images1', 'type': 'logical', 'uuid': '62882128-003d-472c-ac89-d0118cc992c6', 'volume_count': 6, 'volumes': ['admin0.admin_base_vol', 'test01.test_base_vol', 'test00.test_base_vol', 'root', 'vyos0', 'swap'], 'format': 'lvm2'}}.stdout_lines"
}

I need help understanding how to search a specific volumes array to see if a value already exists, and to ignore:

   - name: Create VM volume
        command: |
         virsh vol-create-as {{ pool_name }} {{ vm_volume }}  {{ vm_size }}
   when {{ vm_volume}} not in ansible_libvirt_pools.{{ pool_name }}.volumes

Would appreciate any help on how to do this correctly. Im getting warnings on Jinja templating for the 'when' statement, and I don't think its working as expected.

1 Answers
  ansible_libvirt_pools:
    default:
      autostart: 'yes'
      path: /var/lib/libvirt/images
      persistent: 'yes'
      size_available: '60503101440'
      size_total: '75125227520'
      size_used: '14622126080'
      state: active
      status: running
      type: dir
      uuid: 5f6e9ba1-6a50-4313-9aa6-0981448aff0a
      volume_count: 5
      volumes:
      - centos-8-stream.raw
      - ubuntu-20.04-server-cloudimg-amd64.img
      - ubuntu-server-20.04.raw
      - vyos-1.4-rolling-202201080317-amd64.iso
      - CentOS-Stream-GenericCloud-8-20201019.1.x86_64.qcow2
    infra0_pool_0:
      autostart: 'yes'
      format: lvm2
      path: /dev/almalinux_images1
      persistent: 'yes'
      size_available: '11704318558208'
      size_total: '11998523817984'
      size_used: '294205259776'
      state: active
      status: running
      type: logical
      uuid: 62882128-003d-472c-ac89-d0118cc992c6
      volume_count: 6
      volumes:
      - admin0.admin_base_vol
      - test01.test_base_vol
      - test00.test_base_vol
      - root
      - vyos0
      - swap

Q: "How to search a specific volumes array to see if a value already exists."

A: For example, given the list of volumes that should exist

  vm_volumes: [swap, export]

the task below

    - debug:
        msg: "Missing volumes in {{ item.key }}: {{ _missing }}"
      loop: "{{ ansible_libvirt_pools|dict2items }}"
      vars:
        _missing: "{{ vm_volumes|difference(item.value.volumes) }}"

gives (abridged)

  msg: 'Missing volumes in default: [''swap'', ''export'']'
  msg: 'Missing volumes in infra0_pool_0: [''export'']'

If you want to find existing volumes the task below

    - debug:
        msg: "Present volumes in {{ item.key }}: {{ _present }}"
      loop: "{{ ansible_libvirt_pools|dict2items }}"
      vars:
        _present: "{{ vm_volumes|intersect(item.value.volumes) }}"

gives (abridged)

  msg: 'Present volumes in default: []'
  msg: 'Present volumes in infra0_pool_0: [''swap'']'
Related