How to Deploy a Container Using Ansible
Containers. You cannot escape them, as they have continued to march forward in their takeover of enterprise IT rollouts. You deploy them as apps, as services, and more. And, the method by which you deploy containers is as varied as the reasons why you deploy them:
- Docker
- Kubernetes
- Rancher
- Solaris Containers
- Rocket
- MicroK8s
- Tectonic
The list goes on and on. But did you know there’s another way to deploy containers, one that might have remote administrators anxious to give a go? That alternative method is via Red Hat’s open source automation tool, Ansible.
For those that don’t know, Ansible is an open-source software provisioning, configuration management, and application-deployment tool that runs on Linux. Ansible includes its own declarative language and uses playbooks that create the environment for running simple or incredibly complex commands (or groups of commands) on a remote machine.
And of course, Ansible can be used to deploy containers.
Now, before you dive too deep into this, what I’m going to show you isn’t necessarily a best practices scenario. Because everyone’s landscape and needs vary, what you’re going to see here is simply an introduction to using Ansible for the deployment of containers. Are there better methods of doing so? Certainly. But will this give you a launching point that will allow you to craft highly flexible and viable Ansible playbooks? It definitely will.
In the meantime, let’s see how this is done.
What You’ll Need
In order to make this work, you’ll need at least two servers, one of which has Ansible up and running. I’ll be demonstrating with two instances of Ubuntu Server 18.04. You’ll also need a user account with sudo privileges. Finally, you’ll need to have SSH key authentication setup between the servers. Without SSH key authentication, this will not work.
And that’s all you’ll need.
Install Ansible
If you don’t already have Ansible running on one of your servers, follow these steps to install it:
- Log into the Ubuntu Server that will host Ansible
- Install the necessary repository with the command sudo apt-add-repository ppa:ansible/ansible.
- Update apt with the command sudo apt-get update.
- Install Ansible with the command sudo apt-get install ansible -y.
- If necessary, install a Python interpreter with the command sudo apt-get install python -y.
Copy your SSH Key
In order to make this work, you must have SSH key authentication setup. From your Ansible server, create your SSH key with the command:
ssh-keygen
Once you have the key generated, copy it to the remote machine with the command:
ssh-copy-id SERVER_IP
Where SERVER_IP is the IP address of the remote server.
Install Docker
Next you must install the docker engine on both (or all) machines. To install the docker engine, log into one of your servers and issue the command:
sudo apt-get install docker.io python3-docker -y
Once the installation is complete, start and enable the docker engine with the commands:
sudo systemctl start docker
sudo systemctl enable docker
Finally, add your user to the docker group with the command:
sudo usermod -aG docker $USER
In order for the changes to take effect, log out and log back in.
Once you’ve taken care of these steps on all machines, you’re ready to move on.
Directory Structure
We’re now going to create the necessary directory and files. This will be done on the machine running Ansible. Log into that machine and issue the command:
mkdir ~/docker_project
Change into that newly created directory with the command:
cd ~/docker_project
Now we’re going to create our hosts file. Do this with the command:
nano hosts
In that file, paste the following contents:
1 2 3 4 5 |
[webserver] SERVER_IP [webserver:vars] ansible_python_interpreter=/usr/bin/python3 |
Where SERVER_IP is the IP address of the remote server. If you have more than one server, that file would look like:
1 2 3 4 5 6 |
[webserver] SERVER_IP SERVER2_IP [webserver:vars] ansible_python_interpreter=/usr/bin/python3 |
Save and close the file.
We’ll now create our Ansible playbook. This playbook will do the following:
- Install aptitude.
- Install a few dependencies.
- Add a docker repository.
- Install the docker community edition.
- Install the docker Python module.
- Pull the official Ubuntu image.
- Create four containers based on the newly-pulled Ubuntu image.
This new file will be in the standard YAML format, which means you must pay close attention to your line indenting. Inconsistent indenting will cause your playbook to fail. Create a new file with the command:
nano ubuntu_playbook.yml
Our playbook looks like this:
1 |
- hosts: all</code> <code class=""> become: true</code> <code class=""> vars:</code> <code class=""> create_containers: 4</code> <code class=""> default_container_name: docker</code> <code class=""> default_container_image: ubuntu</code> <code class=""> default_container_command: sleep 1d</code> <code class=""> tasks:</code> <code class=""> - name: Install aptitude using apt</code> <code class=""> apt: name=aptitude state=latest update_cache=yes force_apt_get=yes</code> <code class=""> - name: Install required system packages</code> <code class=""> apt: name={{ item }} state=latest update_cache=yes</code> <code class=""> loop: [ 'apt-transport-https', 'ca-certificates', 'curl', 'software-properties-common', 'python3-pip', 'virtualenv', 'python3-setuptools']</code> <code class=""> - name: Add Docker GPG apt Key</code> <code class=""> apt_key:</code> <code class=""> url: https://download.docker.com/linux/ubuntu/gpg</code> <code class=""> state: present</code> <code class=""> - name: Add Docker Repository</code> <code class=""> apt_repository:</code> <code class=""> repo: deb https://download.docker.com/linux/ubuntu xenial stable</code> <code class=""> state: present</code> <code class=""> - name: Update apt and install docker-ce</code> <code class=""> apt: update_cache=yes name=docker-ce state=latest</code> <code class=""> - name: Install Docker Module for Python</code> <code class=""> pip:</code> <code class=""> name: docker</code> <code class=""> - name: Pull default Docker image</code> <code class=""> docker_image:</code> <code class=""> name: "{{ default_container_image }}"</code> <code class=""> source: pull</code> <code class=""> - name: Create default containers</code> <code class=""> docker_container:</code> <code class=""> name: "{{ default_container_name }}{{ item }}"</code> <code class=""> image: "{{ default_container_image }}"</code> <code class=""> command: "{{ default_container_command }}"</code> <code class=""> state: present</code> <code class=""> with_sequence: count={{ create_containers }} |
Save and close the file. Obviously, you can modify the playbook to fit your needs, so go through it carefully and alter it accordingly.
As I said, there are a multitude of ways to handle the ascribed task. What you see above is only one of them. However, it works and is a great way to see how to deploy containers with Ansible.
Running the Playbook
With our playbook in place, we can now run it with the following command:
ansible-playbook -i hosts ubuntu_playbook.yml --ask-become-pass
The –ask-become-pass option instructs Ansible to require the sudo password for the remote user SSH key (Figure 1). Once you type that password, hit Enter on your keyboard and the playbook will run.

Figure 1: Type the SSH key and the playbook will run.
Depending on your network connection and the speed of your machines, the playbook could take a few minutes to complete. Once it finishes, log onto your remote server and issue the command:
docker ps -a
You should see that the new containers have been deployed (Figure 2).

Figure 2. Created with GIMP
And that’s all there is to deploying containers with Ansible. Are there better ways to do this? Certainly. But if you’re already invested in Ansible, why not add the deployment of containers to your growing list of playbooks.