Dictionary of new variable is:
yaml_cluster = defaultdict(dict)
I have this yaml_file in a variable in python using safe_load from yaml library:
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:
'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'
'wls-pil-10-sa-adm-n0':
ListenPort: 11030
WeblogicPluginEnabled: true
ClientCertProxyEnabled: true
Machine: 'wlm-pil-10-n0'
How to select just the first two of them? I tried this but without success:
servers = sorted(yaml_file["topology"]["Server"])[:-1]
for server in yaml_file["topology"]["Server"]:
if server in servers:
yaml_cluster["topology"]["Server"][server] = yaml_file["topology"]["Server"][server]
ERROR:
fatal: [wls-pil-103-sa-adm-n0]: FAILED! => {"changed": true, "msg": "non-zero return code", "rc": 1, "stderr": "Traceback (most recent call last):\n File "/home/split_yaml.py", line 40, in \n
yaml_cluster["topology"]["Server"][server] = yaml_file["topology"]["Server"][server] #TypeError: string indices must be integers\nKeyError: 'Server'\n", "stderr_lines": ["Traceback (most recent call last):", " File "/home/split_yaml.py", line 40, in ", "
yaml_cluster["topology"]["Server"][server] = yaml_file["topology"]["Server"][server] #TypeError: string indices must be integers", "KeyError: 'Server'"], "stdout": "", "stdout_lines": []}
I think that there are two problems here
yaml_cluster["topology"]["Server"][server] = yaml_file["topology"]["Server"][server] The "Server" and the variable server in the side of the yaml_cluster. I think the dictionary does not let me assign those values.