I would like to write a YAML ansible inventory file from a python script. It seems that the expected format from ansible is with key pairs only, with a colon at the end of each host et no - in front such as:
pe:
hosts:
host1:
host2:
host3:
host4:
I created a structure in python like this:
inventory_struct = {
'pe': {
'hosts': [],
},
}
and I am adding hosts in the 'hosts' list. But when I write the inventory file with:
yaml.dump(inventory_struct, outfile, default_flow_style=False, allow_unicode=True)
I get this format which ansible does not recognize:
pe:
-hosts:
- host1
- host2
- host3
Error message when I run the playbook on this inventory:
Attempted to read "../inventories/inv-xyz555" as YAML: list indices must be integers, not AnsibleUnicode
Is there a way to dump the structure in the expected YAML format?
Thanks,
bundyboy
Just use replace to get rid of null string. For example: print(yaml.dump(inventory_struct, default_flow_style=False).replace('null', ''))