CI/CD pipeline for static inventories

How to refresh static inventories in Ansible Twoer

Posted by Christian Jung on Wed, Jul 29, 2020
In Ansible
Tags ansible, ci/cd, tower, awx

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:

- deploy

  stage: deploy
  - master # only trigger a refresh if the commit was on the master branch
  - source ~/.tower # read variable definitions from file
  - curl -u $username:$password -k -X POST https://$tower_host/api/v2/projects/$inventoryproject/update/

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

username= # your Tower user with enough privileges to trigger the refresh
password= # the password of this user
tower_host= # the FQDN or IP address of your Ansible Tower server
inventoryproject= # 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.


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.