Krikri::InventoryParser
Inventory Parser - supports INI and YAML formats
Constants
Real Ansible's INVENTORY_IGNORE_EXTS default: files with these
suffixes are skipped when an inventory DIRECTORY is read, so an
editor backup or a stray README next to the real sources doesn't
get parsed as inventory. Taken verbatim from ansible-config dump
on ansible-core 2.19.4.
Class methods
Expands one inventory host entry into every hostname it names.
Numeric bounds keep the zero-padding of the FIRST bound
(web[01:03] -> web01..web03), alphabetic bounds step through
letters, and an optional third field is the step
(x[01:10:3] -> x01 x04 x07 x10). Text on either side of the
bracket is preserved, so srv[1:2].ex.com works. Anything that is
not a well-formed range is returned unchanged - including a plain
hostname, which is the overwhelming common case.
Parse inventory from a file, a directory of inventory sources, or a comma-separated host list (auto-detect).
An inventory DIRECTORY: every source inside it is parsed and the
results merged, which is how real Ansible lets a project split
production/ into 01-web.ini, 02-db.ini, hosts.yml and so
on. Sources are read in filename order; ignored extensions
(IGNORED_INVENTORY_EXTENSIONS), hidden files and nested
directories are skipped, as they are there.
Group vars are re-applied AFTER the merge, not just per file: an
[all:vars] block in one file has to reach hosts defined in
another (verified against real Ansible), and each file's own
parse only ever saw its own hosts.
Parse a dynamic inventory: run the executable with --list and parse its JSON output, in Ansible's standard dynamic inventory shape:
{ "group1": {"hosts": ["h1", "h2"], "vars": {...}, "children": [...]}, "group2": ["h3"], # shorthand: bare host list "_meta": {"hostvars": {"h1": {...}}} # optional but preferred - # avoids a --host call per host }
If the script doesn't provide _meta.hostvars, falls back to the
older, slower per-host --host <name> convention real Ansible also
supports. Only inventory scripts (the original, universal dynamic
inventory mechanism - any executable, any language) are implemented;
Ansible's newer YAML-defined inventory plugins (aws_ec2.yml and
friends, each with its own config schema and API calls) are not.
-i "alpha,beta," - a literal list of hosts rather than a file.
Range syntax works here exactly as it does in an INI file
(-i "web[01:03],"), since real Ansible runs the same expansion
over host_list entries.
Overload taking an already-parsed document - parse_directory hands over the doc it already read for its constructed-source sniff, so each directory source is read + parsed once, not twice.