Tweaking an inventory's groups

utoddl
Tue, 23 Dec 2025 19:49:14 +0000
Get Help

I want to experiment with applying tweaks to an otherwise static inventory at runtime, but I’m not sure how to accomplish this. The existing inventory is a straightforward “ini” format single file, all hosts are in groups, and it contains no variables whatsoever. (None of that should matter, unless the answer turns out to be to make a custom inventory plugin from lib/ansible/plugins/inventory/ini.py, but that feels like a bad idea.)

By convention, all of our group names start with mw_ followed by a “service line” and an “environment” (examples: mw_splunk_tst, mw_sso_dev, mw_tableau_prd, mw_infoporte_spt) followed by as many additional qualifiers as needed per service line, concatenated with joining underscore characters.

While the vast majority of our group expressions target hosts within a particular environment (think mw_splunk_tst_ix_dc1 i.e. the test environment Splunk indexers in the data center #1), it’s occasionally convenient to address qualified hosts across all environments (like mw_splunk_all_ix_dc1).

It’s certainly possible to maintain such a static inventory by hand; we’ve done it for years. But it’s tedious; most of the inventory file is taken up defining the intermediate groups and their parents/children. With our group naming conventions, it should be possible to specify only the leaf groups – the ones actually containing hosts – and programmatically generate all the middle groups. And, given a list of allowed environments (dev, tst, spt, prd) we could generate the corresponding _all_ groups. From the example above, if the static inventory has a host-containing group mw_splunk_tst_ix_dc1, then we know we should have all these groups:

mw_splunk_tst_ix_dc1  mw_splunk_all_ix_dc1
mw_splunk_tst_ix      mw_splunk_all_ix
mw_splunk_tst         mw_splunk_all
mw_splunk

That would allow us to throw away most of our static inventory file while giving us consistent intermediate groups which have on occasion been inadvertently omitted, typo’d, etc. in the past.

So that’s the goal. But what are the reasonable options for achieving it? I’ve been bouncing all over the inventory plugin docs and sources without getting noticeably smarter.

5 posts - 5 participants