Ansible如何在列表的属性中选择嵌套列表
嗨,我想知道如何选择嵌套在列表中的列表。我对我的F5大IP做了一项研究,它给了我一个虚拟服务器的态度列表,如下所示:Ansible如何在列表的属性中选择嵌套列表,ansible,yaml,ansible-facts,Ansible,Yaml,Ansible Facts,嗨,我想知道如何选择嵌套在列表中的列表。我对我的F5大IP做了一项研究,它给了我一个虚拟服务器的态度列表,如下所示: "/Common/vs_portailopal_wi_https_virtual_server": { "last_hop_pool": "", "name": "vs_portailopal_wi_https_virtual_server", "nat64_state": "STATE_DISABLED",
"/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 }}"
我想在文件中存储每个虚拟服务器的所有配置文件名,并在其名称上的其他任务筛选器中。我不熟悉bigip Ansible模块,而且我肯定没有任何F5套件可供测试:)在说其他任何事情之前,一个重要的注意事项是,文档说“bigip\u事实”现在已被弃用,你应该使用“bigip\u设备\u事实”。但是如果你想使用这个模块,你能做几件事: 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
如果确实如此,那么这可能会产生您需要的输出(尽管我仍然不清楚,我完全理解您的需求-希望我们更接近):
实际上,“with_subelements”和其他“with_”结构现在都不推荐使用,但如果您使用的是“bigip_facts”,我猜您使用的是Ansible的旧版本。欢迎使用。也许医生能帮上忙。如果您需要其他版本,请将其添加到您的问题中。感谢您的回答:我已根据您的附加信息替换了我的答案。感谢您,这正是我想要做的
- 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