Ansible best practices, private git repos and ansible-galaxy

Our experience on tweaking the Ansible's default role structure using git!

Ansible roles and private git repos

Ansible is a python tool that automates the management of an IT infrastructure. It is easy to learn but rushing to use it with a limited knowledge of its best practices (roles) leads to not reusable code.
To quote the documentation about roles: “You absolutely should be using roles. Roles are great. Use roles. Roles! Did we say that enough? Roles are great.”.

So, writing reusable ansible roles is not easy but it's a task worth doing. This post will walk you through the construction of an ansible role scaffold supported by private git repos.

Install Ansible and git

Be sure to have git and Ansible installed. You should at least be working with ansible==2.3.0. Install Ansible in a virtualenv using pip to keep this environment clean.

Create an Ansible role

The first step

The first step in creating a role is creating its directory structure.
Use the init command of ansible-galaxy, which comes bundled with Ansible, to
initialize the base structure of a new role, saving time (and increase
reproducibility) on creating the various directories.

What if you don't use Github?

Keep in mind that Ansible relies on Github, therefore the test files created
by ansible-galaxy init command are meant to run CI (Continuous Integration)
tests on Travis. But we work with GitLab! Our CI
files are called .gitlab-ci.yml files and have a different syntax compared
to Travis files.
So, how can one leverage the ansible-galaxy command when he/she is not
testing on Travis?

Write your own role scaffold

Starting from ansible==2.3.0 the ansible-galaxy command supports creating
roles using a different role skeleton:

biodec.template is git project that we have developed during this year. It
is an Ansible role scaffold to work and test things on GitLab. The main
difference with respect to the default scaffold is the test file: .gitlab-ci.yml: