Ansible Tower Smarter inventories.

I love the idea of smart inventories, I want a large dynamic inventory and then create small subset inventories based on filters to pass onto my other groups. However; There is a huge issue with the smart inventory that it doesn’t present the group information down to them. So if my DBA team has Dev, QA and Prod environments I would need to create 3 inventories for them DB-Dev, DB-QA and DB-Prod instead of being able to just create one DB group and let them set there job template based on a limit to the environment.

The good news is by creating a custom inventory script we can fix this. For this to work you will need to install the networkx pip module onto the tower server/s, make sure that umask is set to 0022 before hand

umask 0022

pip install networkx

Next in Tower

Go to Inventory Scripts and create a new one call it Smarter Inventory and paste in the following.

Next you will want to create an inventory to authenticate to the Tower Server that has access to the master inventory type.

The credential type is Ansible Tower

Next you need to know the Inventory ID number go to the master inventory you want to filter off and in the address bar you will see the id

Example would look like;order_by:name

The Above inventory I went to has an ID of 16

Now we can put this all together

Create a new Inventory,

then go to Sources and create a new source with the Smarter Inventory and the Tower credentials

You need to have 2 Environment Variables set


The TOWER_INVENTORY needs to be the ID number of the Master Inventory you want to use and the TOWER_INVENTORY_GROUP is going to be the filter/s you want set.

Finally set the permissions of this inventory to the team that would need it, this will bring in the filtered list with all the existing group information.

Leave a Reply

Your email address will not be published. Required fields are marked *