0

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.

shamaN
  • 23
  • 4

1 Answers1

0

The problem is

yaml_cluster = defaultdict(dict)

If you do yaml_cluster["topology"] and that key doesn't exist, the defaultdict will use the given factory to construct it. Thus, it will return an empty dict. You then do ["Server"] on that dict, and it doesn't exist. Since you now have a simple dict, not a defaultdict, this will result in a KeyError.

Do this instead (taken from here):

nested_dict = lambda: defaultdict(nested_dict)
yaml_cluster = nested_dict()

Also, you can simplify the loop to

for server in servers:
  yaml_cluster["topology"]["Server"][server] = yaml_file["topology"]["Server"][server]
flyx
  • 35,506
  • 7
  • 89
  • 126
  • thanks ! my output file is full of this `topology: !!python/object/apply:collections.defaultdict args: - *id001 dictitems: Server: !!python/object/apply:collections.defaultdict args: - *id001 dictitems: test-bruno-jee-r01a-it-c01-m1-n1:` – shamaN Jan 22 '21 at 09:31
  • @shamaN I assume you don't it to look like that. I suggest using normal dicts and implementing a function that handles the auto-creation then, like `set_path(yaml_cluster, ["topology", "Server", server], yaml_file["topology"]["Server"][server])` but if you need further assistance, please ask another question since that is not the focus of this one. – flyx Jan 22 '21 at 11:52