New Docker\Ansible container per Playbook execution?

dockericon

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.

  1. Install docker on your Jenkins slave server and then verify your version.
    • (docker -v) = Docker version 17.03.1-ce, build c6d412e
  2. Download the following Ansible Docker image.  This image has an ENTRYPOINT of “ansible-playbook” and a WORKDIR of “/ansible/playbooks/.  (This is key!!!)
  3. Now we need to make note of the location Jenkins downloads the Git files to on the slave.
    • Example: /home/build/ansible_git/myPlayBook.yml
    • We will use this location when starting up the docker container.  (-v)
  4. 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
      • –rm = cleanup
      • -it = interative and ttl
      • -v = volume (local host:container)