在 ansible 消息输出中搜索数组

Search arrays inside ansible msg output

这里是 Ansible 新手。

我正在努力理解如何检查从上一个任务返回的数组中是否存在值。 我正在尝试搜索从搜索返回的任何卷数组中是否存在卷:

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

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

这个returns输出是这样的:

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"
}

我需要帮助了解如何搜索特定卷数组以查看值是否已存在,以及如何忽略:

   - 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

对于如何正确执行此操作的任何帮助,我们将不胜感激。 我收到有关 'when' 语句的 Jinja 模板的警告,我认为它没有按预期工作。

  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

问:"如何搜索特定卷数组以查看值是否已存在。"

A:例如,给定应该存在的卷列表

  vm_volumes: [swap, export]

下面的任务

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

给出(删节)

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

如果您想查找现有卷,请执行以下任务

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

给出(删节)

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