What would it look like to setup a job in Jenkins that calls out to a Linux server, starts an Ansible Docker container that executes a playbook, and then shuts down the docker container like it never existed? Time to find out…..
For this exercise, we are going to have a Jenkins slave installed on the same Linux server that we’re going to use to launch our Ansible Docker container. Also, in our Jenkins job, we are going to configure Git as our source code repo, which is where out playbook and host files will be stored. So when the Jenkins job is executed, the playbooks\host files will be copied to the slave.
We are going to follow these steps to setup the docker piece of this puzzle.
Install docker on your Jenkins slave server and then verify your version.
(docker -v) = Docker version 17.03.1-ce, build c6d412e
Download the following Ansible Docker image. This image has an ENTRYPOINT of “ansible-playbook” and a WORKDIR of “/ansible/playbooks/. (This is key!!!)
This docker image does not have the WINRM package…. I’ll create a pull request for this soon….
Now we need to make note of the location Jenkins downloads the Git files to on the slave.
We will use this location when starting up the docker container. (-v)
Running the following command on the Jenkins slave, should then start up the Docker container that has Ansible installed, link the local folder “/home/build/ansible_git” to the container folder “/ansible/playbooks”, execute the playbook, and then shut everything down.
docker run –rm -it -v /home/build/ansible_git:/ansible/playbooks <image_id> myPlayBook.yml
Download your preferred Linux flavor. In my case, I decided to us Red Hat Enterprise Linux (RHEL) 7. The following link will show you how to download and install a RHEL iso with VirtualBox. Remember the Ansible “controller” is not supported on Windows yet.