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]:失敗しました!=> {"changed":true、 "msg": "non-zero return code"、 "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":["トレースバック(最後の最後の呼び出し):"、 "ファイル" /tmp/ansible-tmp-1611083722.9917288-55849-215378473850896/split_yaml.py "、32行目"、 " yaml_cluster [" topology " ] ["Name"] = yaml_file ["topology"] ["Name"] "、" KeyError: 'topology' " ]、
発生しているエラーyaml_cluster["topology"]["Name"]
は、どちらのキーも存在しない値が割り当てられているためです。yaml_cluster["topology"]
は存在しないため、キーに何かを割り当てることはできませんName
。
標準ライブラリのコレクションモジュールcollections.defaultdict
は、キーが存在しない場合にデフォルト値を与えるクラスを提供します。あなたの場合、defaultdict
デフォルト値として空の辞書を持つaは、の値yaml_cluster["topology]
を{}
(空のdict)に設定し、キーName
は通常どおり辞書に値を割り当てることができます。
from collections import defaultdict
yaml_cluster = defaultdict(dict) # Specifying dictionary as default value for missing keys
yaml_cluster["topology"]["Name"] = yaml_file["topology"]["Name"]
yaml_cluster["topology"]["AdminServerName"] = yaml_file["topology"]["AdminServerName"]
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加