Ansible 如何 select 列表属性中的嵌套列表

Ansible how to select a nested list in a attribut of list

您好,我想知道如何 select 一个列表嵌套在列表的属性中。我对我的 F5 BIG IP 进行了研究,它为我提供了一个虚拟服务器的属性列表:

    "/Common/vs_portailopal_wi_https_virtual_server": {
        "last_hop_pool": "",
        "name": "vs_portailopal_wi_https_virtual_server",
        "nat64_state": "STATE_DISABLED",
        "object_status": {
            "availability_status": "AVAILABILITY_STATUS_RED",
            "enabled_status": "ENABLED_STATUS_DISABLED",
            "status_description": "The children pool member(s) are down"
        },
        "profile": [
            {
                "profile_context": "PROFILE_CONTEXT_TYPE_CLIENT",
                "profile_name": "/Common/vs_portailopal_wi_clientssl_profile",
                "profile_type": "PROFILE_TYPE_CLIENT_SSL"
            },
            {
                "profile_context": "PROFILE_CONTEXT_TYPE_ALL",
                "profile_name": "/Common/vs_portailopal_wi_http_profile",
                "profile_type": "PROFILE_TYPE_HTTP"
            },
            {
                "profile_context": "PROFILE_CONTEXT_TYPE_ALL",
                "profile_name": "/Common/vs_portailopal_wi_http_profile-cache",
                "profile_type": "PROFILE_TYPE_WEBACCELERATION"
            },]
        },
    },

所以我想比较虚拟服务器的名称和每个配置文件的名称。我可以 select 虚拟服务器的名称,但我无法在配置文件列表中输入 select 名称,因为它是属性

中的嵌套列表

这是我在做的:

---
- name: Search
hosts: F5
gather_facts: no
connection: local


vars:
  username: '{{ cpt_username }}'
  password: '{{ cpt_password }}'

tasks: 


  - name: Get virtual-servers
    bigip_facts:
      include:
        - virtual_server
      server: '{{ inventory_hostname }}'
      user: '{{ username }}'
      password: '{{ password }}'
      validate_certs: no



  - name: filter on VIP_nommage when VIP_partition is OK
    lineinfile: 
      line: 
        - "{{ inventory_hostname }}  Virtual Server : {{ item.key }} => POOL: {{ item.value.profile.name }}" 
      dest: "xxxxx/file.csv"
      state: present
    with_dict: "{{ virtual_server }}"

我想在文件中存储每个虚拟服务器的所有配置文件名称,并在其他任务过滤器中存储它们的名称。

我不熟悉 bigip Ansible 模块,而且我绝对没有任何 F5 工具包可以用来测试 :) 在说别的之前,需要注意的是文档说 'bigip_facts' 是现在已弃用,您应该使用 'bigip_device_facts'。但是如果你想使用这个模块,你能做几件事吗:

1) 编辑您的原始问题以添加您在回复中发布的详细信息。最好继续使用附加信息编辑您的原始问题,而不是添加答案,因为这样更容易理解您的整个情况。

2) 创建一个包含以下内容的新剧本:

---
- hosts: F5
  gather_facts: no
  connection: local

  vars:
    username: '{{ cpt_username }}'
    password: '{{ cpt_password }}'

  tasks: 
    - name: Get virtual-servers
      bigip_facts:
        include:
          - virtual_server
        server: '{{ inventory_hostname }}'
        user: '{{ username }}'
        password: '{{ password }}'
        validate_certs: no
      register: bigip_facts_data

    - name: Display collected data
      debug:
        var: bigip_facts_data

这将向我们展示 Ansible 实际使用的数据。将输出粘贴到您的原始回复中,代替您最初粘贴的 JSON。它可能看起来像这样:

"/Common/vs_portailopal_wi_https_virtual_server":
  last_hop_pool: ''
  name: vs_portailopal_wi_https_virtual_server
  nat64_state: STATE_DISABLED
  object_status:
    availability_status: AVAILABILITY_STATUS_RED
    enabled_status: ENABLED_STATUS_DISABLED
    status_description: The children pool member(s) are down
  profile:
  - profile_context: PROFILE_CONTEXT_TYPE_CLIENT
    profile_name: "/Common/vs_portailopal_wi_clientssl_profile"
    profile_type: PROFILE_TYPE_CLIENT_SSL
  - profile_context: PROFILE_CONTEXT_TYPE_ALL
    profile_name: "/Common/vs_portailopal_wi_http_profile"
    profile_type: PROFILE_TYPE_HTTP
  - profile_context: PROFILE_CONTEXT_TYPE_ALL
    profile_name: "/Common/vs_portailopal_wi_http_profile-cache"
    profile_type: PROFILE_TYPE_WEBACCELERATION

如果是这样,那么这可能会产生您需要的输出(虽然我仍然不清楚我完全理解您的需求 - 希望我们更接近):

 - name: filter on VIP_nommage when VIP_partition is OK
    lineinfile: 
      line: 
        - "{{ inventory_hostname }}  Virtual Server : {{ item.0.name }} => POOL: {{ item.1.profile_name }}" 
      dest: "xxxxx/file.csv"
      state: present
    with_subelements:
      - "{{ bigip_fact_data }}"
      - profile

确实,'with_subelements' 与其他 'with_' 构造现在已被弃用,但如果您使用 'bigip_facts' 我猜您使用的是旧版本的 Ansible。