networking - 如何在 Ansible 中循环打印特定内容
问题描述
我有点坚持 Ansible 试图从 F5 的返回中打印一些变量。
我正在尝试打印在确定的池中配置了哪些节点。我可以得到所有结果:
TASK [f5_getpoolmembers : Ver IPs de un pool] ***********************************************************************************
ok: [localhost] => (item=[u'pool_createdby_ansible_f5']) =>
msg:
- pool_createdby_ansible_f5
- - address: 10.10.10.34
connection_limit: 0
description: prova_ansible_pool
dynamic_ratio: 1
ephemeral: 'no'
fqdn_autopopulate: 'yes'
full_path: /Common/10.10.10.34:80
inherit_profile: 'yes'
logging: 'no'
monitors: []
name: 10.10.10.34:80
partition: Common
priority_group: 0
rate_limit: 'no'
ratio: 1
real_session: user-enabled
real_state: unchecked
state: present
- address: 10.100.250.5
connection_limit: 0
dynamic_ratio: 1
ephemeral: 'no'
fqdn_autopopulate: 'no'
full_path: /Common/10.100.250.5:80
inherit_profile: 'yes'
logging: 'no'
monitors: []
name: 10.100.250.5:80
partition: Common
priority_group: 0
rate_limit: 'no'
ratio: 1
real_session: user-enabled
real_state: unchecked
state: present
使用该代码:
- name: "Ver IPs de un pool"
debug:
msg:
- "{{item.name}}"
- "{{item.members}}"
loop: "{{pool_facts.ltm_pools}}"
loop_control:
label:
- "{{item.name}}"
when: item.name is search "ansible"
问题是当我尝试只name
为池中的每个节点打印时。我找不到办法。尝试了许多选项,例如嵌套或子元素,但没有奏效。如果我尝试只打印name
,我只会得到第一个节点:
TASK [f5_getpoolmembers : Ver IPs de un pool] ***********************************************************************************
ok: [localhost] => (item=[u'pool_createdby_ansible_f5']) =>
msg:
- pool_createdby_ansible_f5
- 10.10.10.34:80
这样做:
- name: "Ver IPs de un pool"
debug:
msg:
- "{{item.name}}"
- "{{item.members[0].name}}"
我知道这是错误的,所以这就是我来这里寻求建议的原因。
编辑:添加了池的完整结构:
ltm_pools:
- active_member_count: 0
all_avg_queue_entry_age: 0
all_max_queue_entry_age_ever: 0
all_max_queue_entry_age_recently: 0
all_num_connections_queued_now: 0
all_num_connections_serviced: 0
all_queue_head_entry_age: 0
allow_nat: 'yes'
allow_snat: 'yes'
availability_status: unknown
available_member_count: 3
client_ip_tos: pass-through
client_link_qos: pass-through
current_sessions: 0
enabled_status: enabled
full_path: /Common/pool_createdby_ansible_f5
ignore_persisted_weight: 'no'
lb_method: ratio-member
member_count: 3
members:
- address: 10.10.10.34
connection_limit: 0
description: prova_ansible_pool
dynamic_ratio: 1
ephemeral: 'no'
fqdn_autopopulate: 'yes'
full_path: /Common/10.10.10.34:80
inherit_profile: 'yes'
logging: 'no'
monitors: []
name: 10.10.10.34:80
partition: Common
priority_group: 0
rate_limit: 'no'
ratio: 1
real_session: user-enabled
real_state: unchecked
state: present
- address: 10.100.250.5
connection_limit: 0
dynamic_ratio: 1
ephemeral: 'no'
fqdn_autopopulate: 'no'
full_path: /Common/10.100.250.5:80
inherit_profile: 'yes'
logging: 'no'
monitors: []
name: 10.100.250.5:80
partition: Common
priority_group: 0
rate_limit: 'no'
ratio: 1
real_session: user-enabled
real_state: unchecked
state: present
- address: 10.55.55.4
connection_limit: 0
dynamic_ratio: 1
ephemeral: 'no'
fqdn_autopopulate: 'no'
full_path: /Common/10.55.55.4:80
inherit_profile: 'yes'
logging: 'no'
monitors: []
name: 10.55.55.4:80
partition: Common
priority_group: 0
rate_limit: 'no'
ratio: 1
real_session: user-enabled
real_state: unchecked
state: present
minimum_active_members: 0
minimum_up_members: 0
minimum_up_members_action: failover
minimum_up_members_checking: 'no'
name: pool_createdby_ansible_f5
pool_avg_queue_entry_age: 0
pool_max_queue_entry_age_ever: 0
pool_max_queue_entry_age_recently: 0
pool_num_connections_queued_now: 0
pool_num_connections_serviced: 0
pool_queue_head_entry_age: 0
priority_group_activation: 0
queue_depth_limit: 0
queue_on_connection_limit: 'no'
queue_time_limit: 0
reselect_tries: 0
server_ip_tos: pass-through
server_link_qos: pass-through
server_side_bits_in: 0
server_side_bits_out: 0
server_side_current_connections: 0
server_side_max_connections: 0
server_side_pkts_in: 0
server_side_pkts_out: 0
server_side_total_connections: 0
service_down_action: none
slow_ramp_time: 10
status_reason: The children pool member(s) either don't have service checking enabled, or service check results are not available yet
total_requests: 0
解决方案
我已经建立了一个成功的剧本。我选择列出比成员名称更多的选项。您可以修改它以仅吐出他们的名字。作为注释,我的 playbokk 具有用于调查的变量 poolName 。这是我的剧本:
- hosts: localhost
tasks:
- name: collect device info
bigip_device_info:
gather_subset:
- ltm-pools
delegate_to: localhost
register: f5pools
- name: Display Config for a specific pool 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[*].{name: name, state:real_state }, Monitors: monitors}"
您可以使用以下方法修改它的名称:
- hosts: localhost
tasks:
- name: collect device info
bigip_device_info:
gather_subset:
- ltm-pools
delegate_to: localhost
register: f5pools
- name: Display Config for a specific pool using a variable
debug:
var: item
loop: "{{ f5pools | json_query(pool_name) }}"
vars:
pool_name: "ltm_pools[?name=='{{ poolName }}'].members[*].name"
哪个有效,请参阅输出:
{
"ansible_loop_var": "item",
"_ansible_no_log": false,
"item": [
"1.1.1.2:443",
"1.1.1.3:443"
],
"changed": false,
"_ansible_verbose_always": true,
"_ansible_item_label": [
"1.1.1.2:443",
"1.1.1.3:443"
]
}
推荐阅读
- corda - 如何阻止来自 Corda 网络上其他节点的流量?
- arm - 带手臂调试的刀具
- python-3.x - 将字典数据中的每个键拆分为堆栈和分组
- json - 有没有办法使用 Go 将 JSON 字符串转换为 Mongo 扩展 JSON?
- azure-cosmosdb - 对分区键字段的查询跨越 Cosmos DB 中的多个分区
- c# - 为外键赋值时 POCO 导航属性未更新
- c - C - 重新分配指向结构组件(类型指针)的指针被中止(核心转储)
- gradle - Gradle 6.1 没有找到 com.moowork.gulp 插件
- height - 在水平条形图中设置最大条形高度
- php - 当 json 值包含“<”时 PHP 为空 curl 响应