How has the recent turmoil within the OpenAI offices changed your plans to use GPT in a business process or product in 2024?
Increased uncertainty means we are more likely to evaluate alternative AI chatbots and LLMs.
No change in plans, though we will keep an eye on the situation.
With Sam Altman back in charge, we are more likely to go all-in with GPT and LLMs.
What recent turmoil?

How to Deploy a Container Using Ansible

How to use Ansible to manage conatiners.
Dec 27th, 2019 6:00am by
Featued image for: How to Deploy a Container Using Ansible
Feature image by Bec T from Pixabay.

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:

  1. Log into the Ubuntu Server that will host Ansible
  2. Install the necessary repository with the command sudo apt-add-repository ppa:ansible/ansible.
  3. Update apt with the command sudo apt-get update.
  4. Install Ansible with the command sudo apt-get install ansible -y.
  5. 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:


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 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:

Where SERVER_IP is the IP address of the remote server. If you have more than one server, that file would look like:

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:

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.

Group Created with Sketch.
TNS owner Insight Partners is an investor in: Docker.
THE NEW STACK UPDATE A newsletter digest of the week’s most important stories & analyses.