Exclude hosts from Constructed Inventory or overwrite inventory project variables inside AWX

Is there a way to exclude hosts from a Constructed Inventory that exists in the upstream based on some criteria?

I’m reading the documentation on the docs.ansible.com site and it doesn’t look like a “filter” option is included, but I’m not sure if there’s an intended way to do this.

Context - One of my upstream sources is the Nutanix inventory plugin. Because it’s not supported in AWX it exists in its own project. I then source the project into the source list for an inventory, but any filtering seems to have to be done at the project level - I tried adding filters at the source level in AWX, but they didn’t seem to take.

I can add the filters at the project level, but then I’d have to make/maintain multiple distinct projects for each host list which seems suboptimal.

I’d like to have one project and one inventory that contains the entirety of our Nutanix hosts and then split them out in our downstream constructed inventories - say one for Windows and one for Linux/Unix.

Anyone know if this is possible?

Inventories have not supported way to subtract host from inventory, because this is definitional, hosts exist or they don’t. You CAN subtract hosts from play selection as that is choosing what to target.

Hi Brian - thanks for the reply. There are a number of inventory plugins that do support host exclusion or filtering when ingesting from some upstream source (VMWare and Nutanix are examples), so I guess I was wondering if there was a way to do it with the constructed inventory plugin in order to build specific, targeted constructed inventories for downstream projects to leverage without them having to tailor all of their playbooks to deal with a mega-inventory.

I’m assuming by your reply that functionality hasn’t been considered or isn’t possible with the constructed inventory plugin (or has been considered and rejected). That’s unfortunate. It looks like I’ll have to go the “maintain multiple inventory projects with different filters” route for what I’m trying to accomplish.

Thanks again for your reply!

Those plugins generate inventory from an external source, they filter that source, there is no facility for plugins to remove hosts from already existing inventory, constructed operates on existing inventory.

1 Like

We use a similar scheme: a handful of larger inventories synced with dynamic plugins (Azure, AWS, etc…) and multiple constructed inventories for filters. The job templates mostly point to the constructed inventories. We heavily use workflows so we build in inventory syncs to get the most up-to-date data at runtime prior to the job templates running.

I’d love to hear more about this setup, as it’s what I’m trying to do. How are you filtering the hosts using constructed inventory? I basically want to build a constructed inventory that’s a smaller subset of the larger inventory and i’m unclear how to do that.

Some of this may depend on how you’re generating the inventory but we use dynamic inventory plugins to create groups in the main inventory based on filters then sync the constructed inventories using those groups in the “limit” field. This may be overkill for most situations as you could just point to the groups in the playbook/job template but we have a couple of use cases:

  • RBAC to allow users access to some of the constructed inventories without needing access to the underlying inventory source
  • Different instance groups need to be used to access hosts which can be set on the constructed inventory

Main inventory using the Azure dynamic plugin (Source variables only)

Constructed inventory

From what we found, using a constructed inventory in a playbook/job template doesn’t trigger a source sync on the main so that has to handled manually. This can be someone who has access to the main inventory triggering the sync, a scheduled task or building in the sync as a workflow task.

We’re an AAP user so a lot of this based on tools that AAP/AWX provide but the concept should be usable with CLI ansible.

1 Like

Ooh, using the Limit is a fantastic idea. I’m going to play around with this some more. We’re also AAP users, so I can already see ways to make this work.

Thanks!