This is a dynamic inventory script for Ansible and the Ansible Tower. It reads hosts and groups from a git repositories' YAML file and "magically" transforms them into a dynamic inventory source for Ansible or the Ansible Tower API.
It has a few advantages over the static definition of hosts / groups in the Tower:
- hosts and groups can be maintained directly in git next to the Ansible plays/roles/variables
- host/group changes will be tagged together with the rest in the git repo
- management of complex host/group structures get simplified
- better overview thanks to the YAML instead of the WebUI
The provided YAML has a predefined layout, which is required for the scripts "magic" host group algorithm. Here's an example of a YAML file called prod.yml:
---
web:
site1:
- web1
- web2
site2:
- web3
- web4
db:
site1:
- db1
- db2
site2:
- db3
- db4When the YAML file is parsed by the script, you'll have access to several host groups:
- tier groups like
web,db: - location groups like
site1,site2: - tier-location groups like
web-site1,web-site2,db-site1,db-site2
Because the file name is based on the environment, for example prod.yml, you also have access to environment based host groups:
- env groups like
prod - env-tier groups like
prod-web,prod-db - env-location groups like
prod-site1,prod-site2 - env-tier-location groups like
prod-web-site1,prod-web-site2,prod-db-site1,prod-db-site2
- Download a relase from this repository.
- Install the requirements by executing
pip install -r requirements.txt
- Browse to the Ansible Tower WebUI
- Login as an user with administration privileges
- Click on the Administration icon on the upper right corner
- Click on the Inventory Scripts link
- Create a new Inventory Script by clicking on the + icon
- Drag n' drop or copy-paste this script into the text field
- Fill out the rest of the form and click Save
- Open an existing inventory in the Ansible Tower WebUI
- Create a new group by clicking the plus icon
- Enter a name for the new (parent) group (e.g. dynamic)
- Click on the Source tab
- Select the custom script you've created in the last step
Please note you can / must set the following environment variables:
URL: URL of the git repository, mandatoryINVENTORY: Path of the inventory file, mandatorySSHKEY: Path to an alternate SSH key, optionalCOMMIT: Commit to checkout (i.e. branch or tag), optional