CI/CD pipeline for static inventories

Posted by Christian Jung on Wed, Jul 29, 2020

It’s usually a good idea to use dynamic inventories with Ansible in large or volatile environments, including but not limited to private and public cloud or containers. There are still valid use cases to work with static inventories though.

Ansible Tower makes it easy to use an static inventory files from a source control management system like Git, Subversion or others. However, if your inventory and your playbook are not in the same project, update on launch will not have the desired affect.

CI/CD Pipeline for static inventories

If you want to have one dedicated project for your inventory file(s), adding a CI/CD configuration can be one option to workaround this issue. Here is a simple example for GitLab:

 1stages:
 2- deploy
 3
 4tower:
 5  stage: deploy
 6  only:
 7  - master # only trigger a refresh if the commit was on the master branch
 8  script:
 9  - source ~/.tower # read variable definitions from file
10  - curl -u $username:$password -k -X POST https://$tower_host/api/v2/projects/$inventoryproject/update/

The ~/.tower file contains the respective variable definitions:

1username= # your Tower user with enough privileges to trigger the refresh
2password= # the password of this user
3tower_host= # the FQDN or IP address of your Ansible Tower server
4inventoryproject= # the name of the SCM project, use %20 to replace spaces, if there are eny

Deploy this file on your GitLab Runner and setup the CI/CD pipeline in your project.

Conclusion

Every time you commit a new inventory file the CI/CD pipeline will trigger an inventory refresh in Ansible Tower, making sure whenever the next Job Template using this inventory is launched, it will use the up to date version.