-
- generate an Ansible inventory using a list of hosts and a parsing template
- inventory is in INI
-
- host file: file that is a list of hosts to be parsed
- template: a YAML template that uses regexp to parse a list of hosts
-
- define a list of hosts in a file, as in test_hosts
- define a template, as in test_template.yml
- run:
python inventory_generator.yml --hosts='<host file>' --template='<YAML template file>'
- test resulting inventory:
ansible -m ping -i <resulting inventory as file> all
-
- the template YAML file is parsed into a template dict
- one template dict becomes one group in the resulting inventory
-
template dict == group (in inventory)
- the dict has a structure similar to (almost identical to) an Ansible YAML inventory
- the parser is defined in GroupWorks.py
- parsing
- the "children" key
- in Ansible inventory, a group YAML object has a key named "children", which is a set of groups
- similarly, a template dict has a key named "children" that is a set (dict) of templates
- the "hosts" key
- defines which hosts are present in a group
- a child templates hosts are a subset of its parent's hosts
- i.e. the hosts key narrows the entire set of hosts when parsing recurses
- the "children" key
- when the parser finishes defining a group, it will recurse on the template's children to create child groups
- the parser handles the INI parent child relation (e.g. creates [all:children] using children defined in "all" template)
-
- Group objects
- defined in Group.py
- will not copy hosts, rather they point to a list of hosts
- therefore, if a list of hosts is defined, then passed to a Group object, changing that list will change the list for the Group object
- this is desireable for now, because if multiple groups rely on a host in one list, then changing that host will change it in each Group that references it
- Capture groups
- only the first match in a regex pattern is used as the name for the (child) groups generated by regex by the parser
- to work around this limitation, nest child group regexp in a group defined by literal name
- (TODO) other workarounds are under work
- Group objects
-
- https://regex101.com
- use the Python flavor