在Ansible中使用json_query查询字典列表中包含的字典列表



我在使用json_query搜索下面的数据结构时遇到了麻烦,我想从中创建两个单独的列表。

List 1应该包含每个字典中包含subPath键且其值包含特定值的条目的name键。

List 2应该包含每个字典中成员列表不包含subPath键且其名称包含特定值的条目的name键。

到目前为止,我能找到的最接近的是那些成员字典具有subPath键的字典:

- name: debug
debug:
msg: "{{ device_facts.gtm_a_pools | json_query(query) }}"
vars:
query: "[].members[?!subPath][].name"

或没有子路径键:

- name: debug
debug:
msg: "{{ device_facts.gtm_a_pools | json_query(query) }}"
vars:
query: "[].members[?subPath][].name"

但是,当我想要的是基于上述两个条件(一个列表有子路径,另一个列表没有子路径)的每个gtm_a_pools字典中的名称键时,这会从成员字典中返回名称键。

使用如下数据结构:

  • List1 = ['a2-test_pool']
  • LIst 2 = [' ace_pool ']
"gtm_a_pools": [
{
"alternate_mode": "global-availability",
"dynamic_ratio": "no",
"enabled": "yes",
"fallback_mode": "return-to-dns",
"full_path": "/Common/a2-test_pool",
"load_balancing_mode": "global-availability",
"manual_resume": "no",
"max_answers_returned": 1,
"members": [
{
"disabled": "no",
"enabled": "yes",
"limitMaxBps": 0,
"limitMaxBpsStatus": "disabled",
"limitMaxConnections": 0,
"limitMaxConnectionsStatus": "disabled",
"limitMaxPps": 0,
"limitMaxPpsStatus": "disabled",
"member_order": 0,
"monitor": "default",
"name": "a2-test_443_vs",
"partition": "Common",
"ratio": 1,
"subPath": "test-dmz-dc1-pair:/Common"
},
{
"disabled": "no",
"enabled": "yes",
"limitMaxBps": 0,
"limitMaxBpsStatus": "disabled",
"limitMaxConnections": 0,
"limitMaxConnectionsStatus": "disabled",
"limitMaxPps": 0,
"limitMaxPpsStatus": "disabled",
"member_order": 1,
"monitor": "default",
"name": "dc2-a2-test_443_vs",
"partition": "Common",
"ratio": 1,
"subPath": "test-dmz-dc2-pair:/Common"
}
],
"name": "a2-test_pool",
"partition": "Common",
"qos_hit_ratio": 5,
"qos_hops": 0,
"qos_kilobytes_second": 3,
"qos_lcs": 30,
"qos_packet_rate": 1,
"qos_rtt": 50,
"qos_topology": 0,
"qos_vs_capacity": 0,
"qos_vs_score": 0,
"ttl": 30,
"verify_member_availability": "yes"
},
{
"alternate_mode": "round-robin",
"dynamic_ratio": "no",
"enabled": "yes",
"fallback_mode": "return-to-dns",
"full_path": "/Common/aci_pool",
"load_balancing_mode": "round-robin",
"manual_resume": "no",
"max_answers_returned": 1,
"members": [
{
"disabled": "no",
"enabled": "yes",
"limitMaxBps": 0,
"limitMaxBpsStatus": "disabled",
"limitMaxConnections": 0,
"limitMaxConnectionsStatus": "disabled",
"limitMaxPps": 0,
"limitMaxPpsStatus": "disabled",
"member_order": 0,
"monitor": "default",
"name": "prod_dc1_servers:aci",
"partition": "Common",
"ratio": 1
},
{
"disabled": "no",
"enabled": "yes",
"limitMaxBps": 0,
"limitMaxBpsStatus": "disabled",
"limitMaxConnections": 0,
"limitMaxConnectionsStatus": "disabled",
"limitMaxPps": 0,
"limitMaxPpsStatus": "disabled",
"member_order": 1,
"monitor": "default",
"name": "prod_dc1_servers:aci",
"partition": "Common",
"ratio": 1
}
],
"name": "aci_pool",
"partition": "Common",
"qos_hit_ratio": 5,
"qos_hops": 0,
"qos_kilobytes_second": 3,
"qos_lcs": 30,
"qos_packet_rate": 1,
"qos_rtt": 50,
"qos_topology": 0,
"qos_vs_capacity": 0,
"qos_vs_score": 0,
"ttl": 30,
"verify_member_availability": "yes"
}
]

Q:">根据上述两个标准(一个列表有子路径,另一个列表没有子路径)从每个gtm_a_pools字典中命名键">

A:代替json_query,迭代列表并比较成员的数量例如
- set_fact:
list1: "{{ list1|default([]) + [item.name] }}"
loop: "{{ gtm_a_pools }}"
when: item_length == subPath_length
vars:
subPath_length: "{{ item.members|map('intersect', ['subPath'])|flatten|length }}"
item_length: "{{ item.members|length }}"

list1:
- a2-test_pool

If"without subPath">表示"任何没有subPath">

的成员
- set_fact:
list2: "{{ list2|default([]) + [item.name] }}"
loop: "{{ gtm_a_pools }}"
when: item_length != subPath_length
vars:
subPath_length: "{{ item.members|map('intersect', ['subPath'])|flatten|length }}"
item_length: "{{ item.members|length }}"

list2:
- aci_pool

如果"不带subPath">表示"所有不带subPath">的成员,下面的任务给出相同的结果

- set_fact:
list2: "{{ list2|default([]) + [item.name] }}"
loop: "{{ gtm_a_pools }}"
when: subPath_length|int == 0
vars:
subPath_length: "{{ item.members|map('intersect', ['subPath'])|flatten|length }}"

谢谢Vladimir!

当我在等待某人的回应时,我想到了这个,所以我想在这里分享一下。我要说的是,这可能并不一定能解释清单2的两种情况,其中我解释了"所有成员",不包括subPath"vs"没有subpath"的任何成员。正因为如此,我使用上面的解决方案在最后运行验证来处理整个负载并绘制比较,并输出具有混合成员类型的任何池:

---
- name: Determine if Server List product is Generic Host (static) or otherwise
bigip_device_info:
gather_subset:
- gtm-servers
provider: "{{ vcmp_guest_provider }}"
delegate_to: localhost
register: device_facts
# server_type = generic-host vs. bigip
# bigip = dynamic
# generic-host = static
- name: Set type fact
set_fact:
server_type: "{{ device_facts | json_query(jmesquery) | join }}"
vars:
jmesquery: "gtm_servers[?name == '{{ gtm_source_server }}'].product"
- name: debug
debug:
var: server_type
- name: Gather GTM pools 
bigip_device_info:
gather_subset:
- gtm-a-pools
provider: "{{ vcmp_guest_provider }}"
delegate_to: localhost
register: device_facts
- name: Create list of pools that use discovered objects and are part of {{ gtm_source_server }}
set_fact:
pool_list: "{{ pool_list | default([]) + [ item.0 ] }}"
loop: "{{ pool_name | zip(payload) | list }}"
when:
- item.1 | json_query('members[*].subPath') | length > 0
- item.1 | json_query('members[*].subPath') is search(gtm_source_server)
- server_type == "bigip"
vars:
pool_name: "{{ device_facts | json_query('gtm_a_pools[*].name') }}"
payload: "{{ device_facts | json_query('gtm_a_pools[*]') }}"
no_log: true
- name: Create list of pools that use static objects and are part of {{ gtm_source_server }}
set_fact:
pool_list: "{{ pool_list | default([]) + [ item.0 ] }}"
loop: "{{ pool_name | zip(payload) | list }}"
when:
- item.1 | json_query('members[*].subPath') | length == 0
- item.1 | json_query('members[*].name') is search(gtm_source_server)
- server_type == "generic-host"
vars:
pool_name: "{{ device_facts | json_query('gtm_a_pools[*].name') }}"
payload: "{{ device_facts | json_query('gtm_a_pools[*]') }}"
no_log: true
- debug:
var: pool_list
- name: Check if any pools contain a mix of static and dynamic members
set_fact:
counter: "{{ counter | default([]) + [ item.name + ' has ' + subPath_length + ' dynamic entries and ' + member_length + ' static entries.' ] }}"
loop: "{{ device_facts | json_query('gtm_a_pools[*]') }}"
vars:
subPath_length: "{{ item.members|map('intersect', ['subPath'])|flatten|length }}"
member_length: "{{ item.members|length }}"
when:
- subPath_length | int > 0
- member_length != subPath_length
no_log: true
- debug:
msg: "{{ counter | default('No mixture found') }}"

最新更新