Choosing which ansible role to test with molecule using git commit description

My previous entry described how to test ansible roles with molecule and gitlab-ci however most of the times you would probably like to test just a role that you’ve edited. To help you with that I’ve edited my repo on github and I will show you how to make it working.

I’ve edited my .gitlab-ci config file in the way that it will test one ansible role with molecule if the commit description would contain name of the role to test, without commit description it will test all the molecule enabled roles and when there is no role named as a commit description it will fail.

The most important change in the file is shown below:

script:
- |
cd playbooks/roles/
if [[ ! -z "$CI_COMMIT_DESCRIPTION" ]]; then
cd $CI_COMMIT_DESCRIPTION/ && molecule test
else
for role in find . -type d -name "molecule" | cut -d'/' -f 2; do
cd $role && molecule test
cd ..
done
fi

As you might see I’ve just replaced basic script that was used to test just one role with this more complex one that will do the job.

How it works? Basically it will take variable passed to gitlab-runner and will make use of it.

How to use it? It is simple as just commiting to your repo, however when you would like to test just one particular role you have to add new line at the end of the commit message and in the new line add the name of the role, like in this example (just a test commit message and description):

This is just a test commit
basic

In the above example text in the first line is the commit message (This is just a test commit) and the second line is the commit description that tells gitlab to test only basic role.

Another example could be found here. So Testing new pipieline config with commit message is the commit message and basic is the commit description that tells gitlab to test just a basic role.

Hopefully it is somehow useful and someone could make use of this trick. Of course feel free to clone and play with repo and molecule.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.