Last time when we got our little Ansible setup to spin up Docker containers as systemd services, I suggested that we should perhaps have Drone running the syntax checks and playbook runs for us. Let's give it a try now, shall we?

This doesn't sound terribly complicated to get it running, right? So let's begin with..

Actually run the playbook (deploy it) if the code has been pushed to master

.drone.yml

build : image : rics3n/drone-ansible commands : - ansible-playbook -vvv playbook.yml -i inventory --syntax-check deploy : ansible : image : rics3n/drone-ansible inventory : inventory playbook : playbook.yml when : branch : master

The build step is really just our test step in this case. We're using rics3n/drone-ansible image for this as it comes with ansible-playbook inside and we're going to use it as a deployment plugin anyway. In theory any docker image with Ansibe installed would be just as good for the build phase. What Drone does in the background for this step is following..

It will: Spin up plugins/drone-git and checkout the code you just pushed somewhere into /drone/src/repo.server/repo/name . This happens before the build phase and while you can customize the behavior, this is what it does by default.

During the build phase, it will spin up rics3n/drone-ansible container

Attach the /drone/src

Owerride its entrypoint with /bin/sh (this as a sideffect won't start the usual plugin binary)

cd into the /drone/src/repo.server/repo/name and run command(s) specified for the build phase. In our case this will just run the desired --syntax-check.

Obviously you can add more commands here - even multiple build steps with different Docker images if you want. If you have any unit tests to run or some checks to do, this phase is exactly for that.

Once all build steps pass, Drone will execute the deploy step. The when condition makes sure, that it will do that only for code pushed to master. This is the most basic scenario - you can add deployment to dev environment from its own, dedicated, branch like this:

deploy : ansible : image : rics3n/drone-ansible inventory : dev_inventory playbook : playbook.yml when : branch : dev

Add more deploy steps for each of your environments and you can "promote" your Ansible configuration from Dev to Test to QA,.. just by merging the changes to appropriate branch. (Obviously you can do the same on repository level or some other combination) This way you can have your basic CI pipeline in couple lines of yaml.

Before we execute the whole configuration, there's couple outstanding steps we need to do.