如何使用ansible从yaml文件中获取自定义列表?
How to get a custom list from yaml file using ansible?
使用此 yaml,我正在尝试获取 'Machine' 属性的列表。
domainInfo:
AdminUserName: '--FIX ME--'
AdminPassword: '--FIX ME--'
topology:
Name: 'wld-pil-10'
ConfigBackupEnabled: true
AdminServerName: 'wls-pil-10-sa-adm-n0'
DomainVersion: 12.2.1.4.0
ProductionModeEnabled: true
ArchiveConfigurationCount: 20
Cluster:
'test-bruno-jee-r01a-c01':
ClientCertProxyEnabled: true
WeblogicPluginEnabled: true
Server:
'wls-pil-10-sa-adm-n0':
ListenPort: 11030
WeblogicPluginEnabled: true
ClientCertProxyEnabled: true
Machine: 'wlm-pil-10-n0'
'test-bruno-jee-r01a-it-c01-m1-n1':
ListenPort: 10022
WeblogicPluginEnabled: true
ClientCertProxyEnabled: true
NMSocketCreateTimeoutInMillis: 30000
Machine: 'wlm-pil-10-n1'
'test-bruno-jee-r02a-it-c01-m1-n1':
ListenPort: 10025
WeblogicPluginEnabled: true
ClientCertProxyEnabled: true
NMSocketCreateTimeoutInMillis: 30000
Machine: 'wlm-pil-10-n2'
我可以通过将 yaml 放入名为“yaml_domain_file”的变量中获取服务器列表,此代码:
- set_fact:
服务器:“{{ yaml_domain_file.topology.Server | 列表}}”
我得到:
好的:[wls-pil-10-sa-adm-n0] => {
“消息”:[
"wls-pil-10-sa-adm-n0",
“测试-布鲁诺-jee-r01a-it-c01-m1-n1”,
“测试-布鲁诺-jee-r01a-it-c01-m1-n2”
]
}
我正在尝试使用此代码获取机器列表:
- 调试:
味精:“{{ yaml_domain_file.topology.Server.*.Machine | list }}”
但这是不可能的。如何获取该信息?
谢谢大家!
尝试json_query
- debug:
msg: "{{ yaml_domain_file.topology.Server|json_query('*.Machine') }}"
Q: "what If I want to put each of the servers in an array?"
A:最简单的选项是 dict2items 过滤器。例如
- set_fact:
servers: "{{ yaml_domain_file.topology.Server|dict2items }}"
使用此 yaml,我正在尝试获取 'Machine' 属性的列表。
domainInfo:
AdminUserName: '--FIX ME--'
AdminPassword: '--FIX ME--'
topology:
Name: 'wld-pil-10'
ConfigBackupEnabled: true
AdminServerName: 'wls-pil-10-sa-adm-n0'
DomainVersion: 12.2.1.4.0
ProductionModeEnabled: true
ArchiveConfigurationCount: 20
Cluster:
'test-bruno-jee-r01a-c01':
ClientCertProxyEnabled: true
WeblogicPluginEnabled: true
Server:
'wls-pil-10-sa-adm-n0':
ListenPort: 11030
WeblogicPluginEnabled: true
ClientCertProxyEnabled: true
Machine: 'wlm-pil-10-n0'
'test-bruno-jee-r01a-it-c01-m1-n1':
ListenPort: 10022
WeblogicPluginEnabled: true
ClientCertProxyEnabled: true
NMSocketCreateTimeoutInMillis: 30000
Machine: 'wlm-pil-10-n1'
'test-bruno-jee-r02a-it-c01-m1-n1':
ListenPort: 10025
WeblogicPluginEnabled: true
ClientCertProxyEnabled: true
NMSocketCreateTimeoutInMillis: 30000
Machine: 'wlm-pil-10-n2'
我可以通过将 yaml 放入名为“yaml_domain_file”的变量中获取服务器列表,此代码:
- set_fact: 服务器:“{{ yaml_domain_file.topology.Server | 列表}}”
我得到:
好的:[wls-pil-10-sa-adm-n0] => { “消息”:[ "wls-pil-10-sa-adm-n0", “测试-布鲁诺-jee-r01a-it-c01-m1-n1”, “测试-布鲁诺-jee-r01a-it-c01-m1-n2” ] }
我正在尝试使用此代码获取机器列表:
- 调试: 味精:“{{ yaml_domain_file.topology.Server.*.Machine | list }}”
但这是不可能的。如何获取该信息?
谢谢大家!
尝试json_query
- debug:
msg: "{{ yaml_domain_file.topology.Server|json_query('*.Machine') }}"
Q: "what If I want to put each of the servers in an array?"
A:最简单的选项是 dict2items 过滤器。例如
- set_fact:
servers: "{{ yaml_domain_file.topology.Server|dict2items }}"