In the agile world of modern system administration, server automation has become indispensable. Tools like Ansible have streamlined the server setup process, reducing human error and standardizing procedures. This article will guide you on how to use Ansible Docker to automate the installation and setup of Docker on Ubuntu 22.04.
Establishing the Context
Docker and Ansible, two powerful tools in the DevOps arsenal, help manage containers and automate complex tasks, respectively. When combined, they can automate Docker setup and streamline configuration management. This guide will provide an alternative to manual setup, leveraging Ansible’s robust feature set and Docker’s container management capabilities.
Prerequisites
Before proceeding with this Ansible playbook, ensure you have the following:
- Ansible Control Node: An Ubuntu 22.04 machine with Ansible installed and configured to connect to your hosts using SSH keys. The control node should have a regular user with sudo permissions and a firewall enabled.
- Ansible Hosts: One or more remote Ubuntu 22.04 servers set up as per the guide on using Ansible for initial server setup on Ubuntu 22.04.
Ensure your Ansible control node can connect and execute commands on your hosts. Test the connection as illustrated in Step 3 of the guide on installing and configuring Ansible on Ubuntu 22.04.
Overview of the Playbook
This Ansible playbook automates the Docker installation process on Ubuntu 22.04. Once set up, you can use it for each subsequent installation. The playbook will perform the following tasks on your Ansible hosts:
- Install aptitude and the required system packages.
- Install the Docker GPG APT key.
- Add the official Docker repository to the apt sources.
- Install Docker.
- Install the Python Docker module via pip.
- Pull the default image from Docker Hub.
- Create the specified number of containers, each using the default image and executing a default command.
After running the playbook, your configuration variables will determine the number of containers created.
Step 1: Preparing Your Playbook
Start by creating your playbook file and defining your tasks. The playbook.yml
file is where all tasks are defined. A task is the smallest unit of action automated using an Ansible playbook.
The playbook should start with declarations like hosts
, become
, and vars
. Hosts
declares the servers the Ansible control node will target, become
states whether all commands will be executed with escalated root privileges, and vars
allows you to store data in variables for easy future editing.
Step 2: Adding Package Installation Tasks to the Playbook
Next, add tasks to install aptitude and the required system packages. Ansible executes tasks synchronously from top to bottom in your playbook, so task ordering is critical.
Step 3: Adding Docker Installation Tasks to the Playbook
The task will install the latest Docker version from the official repository, add the Docker GPG key to verify the download, add the official repository as a new package source, install Docker, and install the Docker module for Python.
Step 4: Adding Docker Image and Container Tasks to the Playbook
Pull your desired Docker image from the official Docker Hub. Using this image, create containers according to the specifications laid out by the variables declared at the top of your playbook.
Step 5: Reviewing the Complete Playbook
Your playbook should look something like this:
---
hosts: all
become: true
vars:
container_count: 4
default_container_name: docker
default_container_image: ubuntu
default_container_command: sleep 1d
tasks:
- name: Install aptitude
apt:
name: aptitude
state: latest
update_cache: true
- name: Install required system packages
apt:
pkg:
- apt-transport-https
- ca-certificates
- curl
- software-properties-common
- python3-pip
- virtualenv
- python3-setuptools
state: latest
update_cache: true
- 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 jammy stable
state: present
- name: Update apt and install docker-ce
apt:
name: docker-ce
state: latest
update_cache: true
- name: Install Docker Module for Python
pip:
name: docker
- name: Pull default Docker image
community.docker.docker_image:
name: "{{ default_container_image }}"
source: pull
- name: Create default containers
community.docker.docker_container:
name: "{{ default_container_name }}{{ item }}"
image: "{{ default_container_image }}"
command: "{{ default_container_command }}"
state: present
with_sequence: count={{ container_count }}
Ensure to modify the playbook to fit your individual needs. Also, remember to maintain the correct indentation structure as YAML files can be particular about it.
Step 6: Running Your Playbook
Once satisfied with your playbook, you can run it on one or more servers. To execute the playbook only on a specific server, use the following command: ansible-playbook playbook.yml -l server1 -u sammy
. The -l
flag specifies your server and the -u
flag specifies which user to log into on the remote server.
Upon playbook completion, log in via SSH to the Ansible-provisioned server to check if the containers were successfully created.
Conclusion
Server automation using Ansible to install and set up Docker can save time and ensure standard, customizable server configuration. With the distributed nature of modern applications and the need for consistency between different staging environments, automation has become a central component in many teams’ development processes.
For a more efficient experience, consider Shape.host services, offering SSD Linux VPS for an enhanced, fast, and reliable hosting experience. Shape.host provides the perfect environment for practicing and implementing the lessons you’ve learned in this guide.
For more information and use cases of the docker_container Ansible module, check out the official Ansible documentation. For more tasks to customize your initial server setup, refer to our introductory Ansible guide Configuration Management 101: Writing Ansible Playbooks.