How to deploy a container with Ansible
CloudFind out how to deploy a Docker container using an Ansible playbook.
There are so many ways to deploy containers. You can use Docker, Kubernetes, MicroK8s, AWS, Google Cloud Platform…. the list goes on and on. But if you’re an Ansible admin, you have yet another method. That’s right, with an Ansible playbook, you can deploy a Docker container.
And that, my friends, is exactly what I’m going to show you how to do. I’ll be demonstrating with only two servers, both of which will run Ubuntu Server 18.04. You can test this with as many servers as you like, but I prefer to keep it simple at this level.
SEE: Hybrid cloud: A guide for IT pros (TechRepublic download)
What you’ll need
To make this work, you’ll need the following:
- At least two servers (one of which has Ansible), installed and running
- A user with sudo privileges
It is important that you first go through the How to Install Ansible on Ubuntu Server 18.04 guide, otherwise this will not work. And make sure you follow the steps for setting up SSH key authentication.
How to install Docker
Because we’re going to be deploying a Docker container, we need Docker installed on every machine. This can be done by opening a terminal window and issuing the following command:
sudo apt-get install docker.io python3-docker -y
Once the installation is complete, start and enable the docker service 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
Log out and log back in so the changes will take effect.
How to create the directory structure and files
On the Ansible server, create a new directory with the following command:
mkdir docker_project
Change into the docker_project directory with the command cd docker_project and create a hosts file with the command:
nano hosts
In that file, add the following contents:
[webserver] SERVER_IP [webserver:vars] ansible_python_interpreter=/usr/bin/python3
Where SERVER_IP is the IP address of the other server(s). Save and close the file.
We’re going to create a playbook that will do the following:
- Install aptitude
- Install a number of dependencies
- Add a docker repository
- Install docker-ce
- Install the docker Python module
- Pull the official Ubuntu image
- Create four containers based on the Ubuntu image
Issue the command nano ubuntu_playbook.yml. In that new file, paste the following (making sure the formatting follows the standard YAML format):
--- - hosts: all become: true vars: create_containers: 4 default_container_name: docker default_container_image: ubuntu default_container_command: sleep 1d tasks: - name: Install aptitude using apt apt: name=aptitude state=latest update_cache=yes force_apt_get=yes - name: Install required system packages apt: name={{ item }} state=latest update_cache=yes loop: [ 'apt-transport-https', 'ca-certificates', 'curl', 'software-properties-common', 'python3-pip', 'virtualenv', 'python3-setuptools'] - name: Add Docker GPG apt Key apt_key: url: https://download.docker.com/linux/ubuntu/gpg state: present - name: Add Docker Repository apt_repository: repo: deb https://download.docker.com/linux/ubuntu xenial stable state: present - name: Update apt and install docker-ce apt: update_cache=yes name=docker-ce state=latest - name: Install Docker Module for Python pip: name: docker - name: Pull default Docker image docker_image: name: "{{ default_container_image }}" source: pull - name: Create default containers docker_container: name: "{{ default_container_name }}{{ item }}" image: "{{ default_container_image }}" command: "{{ default_container_command }}" state: present with_sequence: count={{ create_containers }}
Save and close the file. Once you have a feel for how this playbook works, you can also modify it to fit your needs. Consider this a solid template for which to deploy containers from Ansible.
How to run the Ansible playbook
We can now run our Ansible playbook. To do this, issue the command:
ansible-playbook -i hosts ubuntu_playbook.yml --ask-become-pass
You will be prompted for the sudo password for the remote user. Type that and hit Enter. The playbook should then execute without fail. When the playbook completes, you should see the output indicates success (Figure A).
Figure A
If you go over to one of your remote servers and issue the command docker ps -a, you should see all four ubuntu containers running (Figure B).
Figure B
And that’s it. You now know how to deploy a Docker container by way of an Ansible playbook. This is certainly not the easiest route, but it does make it possible for you to get really creative with both your containers and Ansible.
Cloud Insights Newsletter
Your go-to knowledge base for the latest about AWS, Microsoft Azure, Google Cloud Platform, Docker, SaaS, IaaS, cloud security, containers, the public cloud, the hybrid cloud, the industry cloud, and much more. Delivered Mondays