Skip to main content

Raymii.org Logo (IEC resistor symbol)logo

Quis custodiet ipsos custodes?
Home | About | All pages | RSS Feed | Gopher

log_vcs - Ansible callback plugin that creates VCS (git) branches for every Ansible run

Published: 10-07-2018 | Author: Remy van Elst | Text only version of this article


Table of Contents


This callback plugin creates a VCS branch every time you run Ansible. If youever need to go back to a certain state or environment, check out that branchand be sure nothing has changed.

This is useful when you have multiple environments or multiple people deployingand continually develop your Ansible. When you often deploy to test / acceptanceand less often to production, you can checkout the last branch that deployed toproduction if a hotfix or other maintenance is required, without having tosearch back in your commits and logs. I would recommend to developinfrastructure features in feature branches and have the master branch alwaysdeployable to production. However, reality learns that that is not always thecase and this is a nice automatic way to have a fallback.

If you like this article, consider sponsoring me by trying out a Digital OceanVPS. With this link you'll get $100 credit for 60 days). (referral link)

Requirements and installation

The plugin requires 'GitPython'. On Ubuntu this can be installed with thepackage manager:

apt-get install python-git python3-git

The plugin requires the Ansible folder to be a git repository. If you have aseperate 'roles' directory, that is not included in this plugin.

The plugin is hosted on Github: https://github.com/RaymiiOrg/log_vcs

To install the plugin, create a folder in your Ansible folder:

mkdir -p plugins/callbacks

Place the file in there and edit your ansible.cfg file:

  [defaults]  callback_whitelist = log_vcs  callback_plugins = plugins/callbacks

Environments

If you have multiple environments (multiple inventories) then every inventoryneeds a group_var (in group_vars/all.yml) named environment. The pluginuses this in the branch name. In my case it can be dev, tst, acc, prd ormgmt. It is not required, if it is not found the plugin will substitute itwith env.

Branch name format

The branch name format is:

auto-$year$month$dayT$hour$minute-$env-$branch-$username-$playbook-filename

For example:

auto-20180710T100719-env-master-remy-nginx-vps-for-raymii.org.yml

or:

  auto-20180709T161235-tst-refactor-for-odoo-remy-odoo.yml  auto-20180710T091419-prd-refactor-for-odoo-remy-ping.yml

Auto-commit or cleanup?

There is no auto-commit or auto-push to a git server. In my use-case deploymentis always done from a management machine, otherwise you have to extend theplugin to do auto-commit and push. I decided in my case it would not be useful.

Auto-cleanup is also not implemented. We have bash for that:

git branch | grep 'auto-' | xargs -L 1 -I % git branch -d %
Tags: ansible, configuration-management, deployment, devops, git, python, software, sudoers