在 Ansible 中解析一个 JSON 输出以嵌套显示信息
Parse a JSON Output in Ansible to display information in nested display
我正在尝试 运行 一个查询我们的 F5 特定池并仅显示我需要的内容的剧本。我可以直接在 ltm_pools 下解析任何数据,但如果它是像“成员”这样的子集,我无法为此过滤特定信息。我想过滤成员子集的特定信息。您可以看到我将成员列为输出显示,但它显示成员下的每个字段。我想过滤特定字段,例如“成员”字段下的“姓名”。
查看 JSON 输出:
"ltm_pools": [
{
"all_max_queue_entry_age_recently": 0,
"pool_queue_head_entry_age": 0,
"current_sessions": 0,
"pool_max_queue_entry_age_recently": 0,
"server_side_max_connections": 0,
"monitors": [
"/Common/tcp"
],
"available_member_count": 0,
"server_side_bits_in": 0,
"pool_max_queue_entry_age_ever": 0,
"member_count": 4,
"priority_group_activation": 0,
"allow_snat": "yes",
"reselect_tries": 0,
"enabled_status": "enabled",
"active_member_count": 0,
"server_ip_tos": "pass-through",
"server_link_qos": "pass-through",
"queue_on_connection_limit": "no",
"queue_depth_limit": 0,
"minimum_up_members_action": "failover",
"allow_nat": "yes",
"lb_method": "round-robin",
"all_avg_queue_entry_age": 0,
"ignore_persisted_weight": "no",
"server_side_bits_out": 0,
"all_num_connections_serviced": 0,
"minimum_active_members": 0,
"service_down_action": "none",
"server_side_current_connections": 0,
"members": [
{
"real_session": "monitor-enabled",
"rate_limit": "no",
"inherit_profile": "yes",
"real_state": "down",
"address": "1.1.1.1",
"logging": "no",
"monitors": [],
"ratio": 1,
"name": "1.1.1.1:3268",
"partition": "Common",
"ephemeral": "no",
"connection_limit": 0,
"state": "offline",
"full_path": "/Common/1.1.1.1:3268",
"fqdn_autopopulate": "no",
"priority_group": 0,
"dynamic_ratio": 1
},
{
"real_session": "monitor-enabled",
"rate_limit": "no",
"inherit_profile": "yes",
"real_state": "down",
"address": "2.2.2.2",
"logging": "no",
"monitors": [],
"ratio": 1,
"name": "2.2.2.2:3268",
"partition": "Common",
"ephemeral": "no",
"connection_limit": 0,
"state": "offline",
"full_path": "/Common/2.2.2.2:3268",
"fqdn_autopopulate": "no",
"priority_group": 0,
"dynamic_ratio": 1
},
{
"real_session": "monitor-enabled",
"rate_limit": "no",
"inherit_profile": "yes",
"real_state": "down",
"address": "3.3.3.3",
"logging": "no",
"monitors": [],
"ratio": 1,
"name": "3.3.3.3:3268",
"partition": "Common",
"ephemeral": "no",
"connection_limit": 0,
"state": "offline",
"full_path": "/Common/3.3.3.3:3268",
"fqdn_autopopulate": "no",
"priority_group": 0,
"dynamic_ratio": 1
},
{
"real_session": "monitor-enabled",
"rate_limit": "no",
"inherit_profile": "yes",
"real_state": "down",
"address": "3.3.3.3",
"logging": "no",
"monitors": [],
"ratio": 1,
"name": "3.3.3.3:3268",
"partition": "Common",
"ephemeral": "no",
"connection_limit": 0,
"state": "offline",
"full_path": "/Common/3.3.3.3:3268",
"fqdn_autopopulate": "no",
"priority_group": 0,
"dynamic_ratio": 1
}
],
"all_max_queue_entry_age_ever": 0,
"queue_time_limit": 0,
"status_reason": "The children pool member(s) are down",
"server_side_pkts_out": 0,
"server_side_total_connections": 0,
"pool_num_connections_serviced": 0,
"client_link_qos": "pass-through",
"name": "test",
"pool_num_connections_queued_now": 0,
"minimum_up_members": 0,
"all_queue_head_entry_age": 0,
"slow_ramp_time": 10,
"server_side_pkts_in": 0,
"client_ip_tos": "pass-through",
"minimum_up_members_checking": "no",
"all_num_connections_queued_now": 0,
"availability_status": "offline",
"pool_avg_queue_entry_age": 0,
"total_requests": 0,
"full_path": "/Common/test"
},
这是我的剧本:
- hosts: localhost
tasks:
- name: collect device info
bigip_device_info:
gather_subset:
- ltm-pools
delegate_to: localhost
register: f5pools
# Show the name, IP address, port and status of a virtual server
- name: Display Config for a specific Virtual Server using a variable
debug:
var: item
loop: "{{ f5pools | json_query(pool_name) }}"
vars:
pool_name: "ltm_pools[?name=='{{ poolName }}'].{name: name, Method: lb_method, Members: members Monitors: monitors}"
您可以在多选中嵌套 JMESPath 查询,在您的情况下,以实现您要查找的内容。
所以不用普通的
Members: members
在你的多选中,你可以使用类似
的东西
Members: members[*].{name: name}
鉴于剧本:
- hosts: all
gather_facts: no
tasks:
- name: Display Config for a specific Virtual Server using a variable
debug:
var: item
loop: "{{ f5pools | json_query(pool_name) }}"
loop_control:
label: "{{ item.name }}"
vars:
pool_name: "ltm_pools[?name=='{{ poolName }}'].{name: name, Method: lb_method, Members: members[*].{name: name}, Monitors: monitors}"
f5pools:
ltm_pools:
- all_max_queue_entry_age_recently: 0
pool_queue_head_entry_age: 0
current_sessions: 0
pool_max_queue_entry_age_recently: 0
server_side_max_connections: 0
monitors:
- /Common/tcp
available_member_count: 0
server_side_bits_in: 0
pool_max_queue_entry_age_ever: 0
member_count: 4
priority_group_activation: 0
allow_snat: yes
reselect_tries: 0
enabled_status: enabled
active_member_count: 0
server_ip_tos: pass-through
server_link_qos: pass-through
queue_on_connection_limit: no
queue_depth_limit: 0
minimum_up_members_action: failover
allow_nat: yes
lb_method: round-robin
all_avg_queue_entry_age: 0
ignore_persisted_weight: no
server_side_bits_out: 0
all_num_connections_serviced: 0
minimum_active_members: 0
service_down_action: none
server_side_current_connections: 0
members:
- real_session: monitor-enabled
rate_limit: no
inherit_profile: yes
real_state: down
address: 1.1.1.1
logging: no
monitors: []
ratio: 1
name: 1.1.1.1:3268
partition: Common
ephemeral: no
connection_limit: 0
state: offline
full_path: /Common/1.1.1.1:3268
fqdn_autopopulate: no
priority_group: 0
dynamic_ratio: 1
- real_session: monitor-enabled
rate_limit: no
inherit_profile: yes
real_state: down
address: 2.2.2.2
logging: no
monitors: []
ratio: 1
name: 2.2.2.2:3268
partition: Common
ephemeral: no
connection_limit: 0
state: offline
full_path: /Common/2.2.2.2:3268
fqdn_autopopulate: no
priority_group: 0
dynamic_ratio: 1
- real_session: monitor-enabled
rate_limit: no
inherit_profile: yes
real_state: down
address: 3.3.3.3
logging: no
monitors: []
ratio: 1
name: 3.3.3.3:3268
partition: Common
ephemeral: no
connection_limit: 0
state: offline
full_path: /Common/3.3.3.3:3268
fqdn_autopopulate: no
priority_group: 0
dynamic_ratio: 1
- real_session: monitor-enabled
rate_limit: no
inherit_profile: yes
real_state: down
address: 3.3.3.3
logging: no
monitors: []
ratio: 1
name: 3.3.3.3:3268
partition: Common
ephemeral: no
connection_limit: 0
state: offline
full_path: /Common/3.3.3.3:3268
fqdn_autopopulate: no
priority_group: 0
dynamic_ratio: 1
all_max_queue_entry_age_ever: 0
queue_time_limit: 0
status_reason: The children pool member(s) are down
server_side_pkts_out: 0
server_side_total_connections: 0
pool_num_connections_serviced: 0
client_link_qos: pass-through
name: test
pool_num_connections_queued_now: 0
minimum_up_members: 0
all_queue_head_entry_age: 0
slow_ramp_time: 10
server_side_pkts_in: 0
client_ip_tos: pass-through
minimum_up_members_checking: no
all_num_connections_queued_now: 0
availability_status: offline
pool_avg_queue_entry_age: 0
total_requests: 0
full_path: /Common/test
poolName: test
这产生了回顾:
PLAY [all] ********************************************************************************************************
TASK [Display Config for a specific Virtual Server using a variable] **********************************************
ok: [localhost] => (item=test) => {
"ansible_loop_var": "item",
"item": {
"Members": [
{
"name": "1.1.1.1:3268"
},
{
"name": "2.2.2.2:3268"
},
{
"name": "3.3.3.3:3268"
},
{
"name": "3.3.3.3:3268"
}
],
"Method": "round-robin",
"Monitors": [
"/Common/tcp"
],
"name": "test"
}
}
PLAY RECAP ********************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
我正在尝试 运行 一个查询我们的 F5 特定池并仅显示我需要的内容的剧本。我可以直接在 ltm_pools 下解析任何数据,但如果它是像“成员”这样的子集,我无法为此过滤特定信息。我想过滤成员子集的特定信息。您可以看到我将成员列为输出显示,但它显示成员下的每个字段。我想过滤特定字段,例如“成员”字段下的“姓名”。
查看 JSON 输出:
"ltm_pools": [
{
"all_max_queue_entry_age_recently": 0,
"pool_queue_head_entry_age": 0,
"current_sessions": 0,
"pool_max_queue_entry_age_recently": 0,
"server_side_max_connections": 0,
"monitors": [
"/Common/tcp"
],
"available_member_count": 0,
"server_side_bits_in": 0,
"pool_max_queue_entry_age_ever": 0,
"member_count": 4,
"priority_group_activation": 0,
"allow_snat": "yes",
"reselect_tries": 0,
"enabled_status": "enabled",
"active_member_count": 0,
"server_ip_tos": "pass-through",
"server_link_qos": "pass-through",
"queue_on_connection_limit": "no",
"queue_depth_limit": 0,
"minimum_up_members_action": "failover",
"allow_nat": "yes",
"lb_method": "round-robin",
"all_avg_queue_entry_age": 0,
"ignore_persisted_weight": "no",
"server_side_bits_out": 0,
"all_num_connections_serviced": 0,
"minimum_active_members": 0,
"service_down_action": "none",
"server_side_current_connections": 0,
"members": [
{
"real_session": "monitor-enabled",
"rate_limit": "no",
"inherit_profile": "yes",
"real_state": "down",
"address": "1.1.1.1",
"logging": "no",
"monitors": [],
"ratio": 1,
"name": "1.1.1.1:3268",
"partition": "Common",
"ephemeral": "no",
"connection_limit": 0,
"state": "offline",
"full_path": "/Common/1.1.1.1:3268",
"fqdn_autopopulate": "no",
"priority_group": 0,
"dynamic_ratio": 1
},
{
"real_session": "monitor-enabled",
"rate_limit": "no",
"inherit_profile": "yes",
"real_state": "down",
"address": "2.2.2.2",
"logging": "no",
"monitors": [],
"ratio": 1,
"name": "2.2.2.2:3268",
"partition": "Common",
"ephemeral": "no",
"connection_limit": 0,
"state": "offline",
"full_path": "/Common/2.2.2.2:3268",
"fqdn_autopopulate": "no",
"priority_group": 0,
"dynamic_ratio": 1
},
{
"real_session": "monitor-enabled",
"rate_limit": "no",
"inherit_profile": "yes",
"real_state": "down",
"address": "3.3.3.3",
"logging": "no",
"monitors": [],
"ratio": 1,
"name": "3.3.3.3:3268",
"partition": "Common",
"ephemeral": "no",
"connection_limit": 0,
"state": "offline",
"full_path": "/Common/3.3.3.3:3268",
"fqdn_autopopulate": "no",
"priority_group": 0,
"dynamic_ratio": 1
},
{
"real_session": "monitor-enabled",
"rate_limit": "no",
"inherit_profile": "yes",
"real_state": "down",
"address": "3.3.3.3",
"logging": "no",
"monitors": [],
"ratio": 1,
"name": "3.3.3.3:3268",
"partition": "Common",
"ephemeral": "no",
"connection_limit": 0,
"state": "offline",
"full_path": "/Common/3.3.3.3:3268",
"fqdn_autopopulate": "no",
"priority_group": 0,
"dynamic_ratio": 1
}
],
"all_max_queue_entry_age_ever": 0,
"queue_time_limit": 0,
"status_reason": "The children pool member(s) are down",
"server_side_pkts_out": 0,
"server_side_total_connections": 0,
"pool_num_connections_serviced": 0,
"client_link_qos": "pass-through",
"name": "test",
"pool_num_connections_queued_now": 0,
"minimum_up_members": 0,
"all_queue_head_entry_age": 0,
"slow_ramp_time": 10,
"server_side_pkts_in": 0,
"client_ip_tos": "pass-through",
"minimum_up_members_checking": "no",
"all_num_connections_queued_now": 0,
"availability_status": "offline",
"pool_avg_queue_entry_age": 0,
"total_requests": 0,
"full_path": "/Common/test"
},
这是我的剧本:
- hosts: localhost
tasks:
- name: collect device info
bigip_device_info:
gather_subset:
- ltm-pools
delegate_to: localhost
register: f5pools
# Show the name, IP address, port and status of a virtual server
- name: Display Config for a specific Virtual Server using a variable
debug:
var: item
loop: "{{ f5pools | json_query(pool_name) }}"
vars:
pool_name: "ltm_pools[?name=='{{ poolName }}'].{name: name, Method: lb_method, Members: members Monitors: monitors}"
您可以在多选中嵌套 JMESPath 查询,在您的情况下,以实现您要查找的内容。
所以不用普通的
Members: members
在你的多选中,你可以使用类似
的东西Members: members[*].{name: name}
鉴于剧本:
- hosts: all
gather_facts: no
tasks:
- name: Display Config for a specific Virtual Server using a variable
debug:
var: item
loop: "{{ f5pools | json_query(pool_name) }}"
loop_control:
label: "{{ item.name }}"
vars:
pool_name: "ltm_pools[?name=='{{ poolName }}'].{name: name, Method: lb_method, Members: members[*].{name: name}, Monitors: monitors}"
f5pools:
ltm_pools:
- all_max_queue_entry_age_recently: 0
pool_queue_head_entry_age: 0
current_sessions: 0
pool_max_queue_entry_age_recently: 0
server_side_max_connections: 0
monitors:
- /Common/tcp
available_member_count: 0
server_side_bits_in: 0
pool_max_queue_entry_age_ever: 0
member_count: 4
priority_group_activation: 0
allow_snat: yes
reselect_tries: 0
enabled_status: enabled
active_member_count: 0
server_ip_tos: pass-through
server_link_qos: pass-through
queue_on_connection_limit: no
queue_depth_limit: 0
minimum_up_members_action: failover
allow_nat: yes
lb_method: round-robin
all_avg_queue_entry_age: 0
ignore_persisted_weight: no
server_side_bits_out: 0
all_num_connections_serviced: 0
minimum_active_members: 0
service_down_action: none
server_side_current_connections: 0
members:
- real_session: monitor-enabled
rate_limit: no
inherit_profile: yes
real_state: down
address: 1.1.1.1
logging: no
monitors: []
ratio: 1
name: 1.1.1.1:3268
partition: Common
ephemeral: no
connection_limit: 0
state: offline
full_path: /Common/1.1.1.1:3268
fqdn_autopopulate: no
priority_group: 0
dynamic_ratio: 1
- real_session: monitor-enabled
rate_limit: no
inherit_profile: yes
real_state: down
address: 2.2.2.2
logging: no
monitors: []
ratio: 1
name: 2.2.2.2:3268
partition: Common
ephemeral: no
connection_limit: 0
state: offline
full_path: /Common/2.2.2.2:3268
fqdn_autopopulate: no
priority_group: 0
dynamic_ratio: 1
- real_session: monitor-enabled
rate_limit: no
inherit_profile: yes
real_state: down
address: 3.3.3.3
logging: no
monitors: []
ratio: 1
name: 3.3.3.3:3268
partition: Common
ephemeral: no
connection_limit: 0
state: offline
full_path: /Common/3.3.3.3:3268
fqdn_autopopulate: no
priority_group: 0
dynamic_ratio: 1
- real_session: monitor-enabled
rate_limit: no
inherit_profile: yes
real_state: down
address: 3.3.3.3
logging: no
monitors: []
ratio: 1
name: 3.3.3.3:3268
partition: Common
ephemeral: no
connection_limit: 0
state: offline
full_path: /Common/3.3.3.3:3268
fqdn_autopopulate: no
priority_group: 0
dynamic_ratio: 1
all_max_queue_entry_age_ever: 0
queue_time_limit: 0
status_reason: The children pool member(s) are down
server_side_pkts_out: 0
server_side_total_connections: 0
pool_num_connections_serviced: 0
client_link_qos: pass-through
name: test
pool_num_connections_queued_now: 0
minimum_up_members: 0
all_queue_head_entry_age: 0
slow_ramp_time: 10
server_side_pkts_in: 0
client_ip_tos: pass-through
minimum_up_members_checking: no
all_num_connections_queued_now: 0
availability_status: offline
pool_avg_queue_entry_age: 0
total_requests: 0
full_path: /Common/test
poolName: test
这产生了回顾:
PLAY [all] ********************************************************************************************************
TASK [Display Config for a specific Virtual Server using a variable] **********************************************
ok: [localhost] => (item=test) => {
"ansible_loop_var": "item",
"item": {
"Members": [
{
"name": "1.1.1.1:3268"
},
{
"name": "2.2.2.2:3268"
},
{
"name": "3.3.3.3:3268"
},
{
"name": "3.3.3.3:3268"
}
],
"Method": "round-robin",
"Monitors": [
"/Common/tcp"
],
"name": "test"
}
}
PLAY RECAP ********************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0