【发布时间】:2021-01-19 19:50:30
【问题描述】:
我使用 yaml 库中的 safe_load 在 python 中的变量中有这个 yaml_file:
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 文件,我尝试将键和值放入新字典中,但没有成功。我错过了什么?我知道我需要某种方式的字典,是否需要使用其他模块,例如 pyyaml 或 ruamel?
yaml_cluster = {}
yaml_cluster["topology"]["Name"] = yaml_file["topology"]["Name"]
yaml_cluster["topology"]["AdminServerName"] = yaml_file["topology"]["AdminServerName"]
结果:
致命:[wls-pil-103-sa-adm-n0]:失败! => {“改变”:真,“味精”: "非零返回码", "rc": 1, "stderr": "Traceback (最近 最后调用):\n 文件 "/tmp/ansible-tmp-1611083722.9917288-55849-215378473850896/split_yaml.py", 第 32 行,在 \n yaml_cluster["topology"]["Name"] = yaml_file["topology"]["Name"]\nKeyError: 'topology'\n", "stderr_lines": ["Traceback (最近一次调用最后一次):", " File "/tmp/ansible-tmp-1611083722.9917288-55849-215378473850896/split_yaml.py", 第 32 行,在 ", " yaml_cluster["topology"]["Name"] = yaml_file["topology"]["Name"]", "KeyError: 'topology'"], "stdout": "", "stdout_lines": []}
【问题讨论】:
标签: python dictionary yaml