class

Krikri::InventoryParser

Inherits Reference < Object

Inventory Parser - supports INI and YAML formats

Constants

IGNORED_INVENTORY_EXTENSIONS = [".pyc", ".pyo", ".swp", ".bak", "~", ".rpm", ".md", ".txt", ".rst", ".orig", ".cfg", ".retry"] of ::String

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

expand_host_range(entry : String) : Array(String)

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.

Source
parse(path : String) : Inventory

Parse inventory from a file, a directory of inventory sources, or a comma-separated host list (auto-detect).

Source
parse_directory(path : String) : Inventory

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.

Source
parse_dynamic(path : String) : Inventory

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.

Source
parse_host_list(path : String) : Inventory

-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.

Source
parse_ini(path : String) : Inventory

Parse INI format inventory

Source
parse_yaml(path : String, yaml : YAML::Any) : Inventory

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.

Source
parse_yaml(path : String) : Inventory

Parse YAML format inventory

Source
stats(inventory : Inventory) : Hash(String, Int32)

Get inventory statistics

Source
validate(inventory : Inventory) : Array(String)

Validate inventory

Source