Ansible如何在列表属性中选择嵌套列表

时间:2018-12-14 10:46:15

标签: ansible yaml ansible-facts

您好,我想知道如何选择嵌套在列表中的列表。我已经对我的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"
            },]
        },
    },

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

这是我在做的

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

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

1 个答案:

答案 0 :(得分:0)

我对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: "/home/ADMB0002194/F5/file.csv"
      state: present
    with_subelements:
      - "{{ bigip_fact_data }}"
      - profile

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