Automate Docker & Docker‑Compose Installation with Ansible – A Step‑by‑Step Guide
This article explains how to install Ansible, use its basic playbook commands, structure a playbook, and leverage built‑in modules, register and when statements to fully automate Docker and Docker‑Compose deployment on CentOS systems.
Ansible is a Python‑based automation tool that enables cluster management and common operational tasks across dozens to thousands of virtual machines.
In many companies, services are deployed on clusters, and Ansible allows batch operations on one or multiple clusters.
The author works on service automation and has encountered several automation platforms, including:
An Ansible‑script‑driven deployment and upgrade platform.
A Jenkins‑style CI/CD pipeline platform.
A Python‑script‑driven SDK‑based operations platform.
Docker containers with orchestration.
This article shares experience automating the installation of Docker and Docker‑Compose with Ansible.
1. Install Ansible
The control machine must have Python 2; Windows cannot be used as a control host, but Red Hat, Debian, CentOS, macOS, BSD, etc., are supported.
Using pip
<code>sudo pip install ansible</code>Using system package managers
On CentOS:
<code>sudo yum install ansible</code>On Ubuntu:
<code>sudo apt-get install software-properties-common
sudo apt-add-repository ppa:ansible/ansible
sudo apt-get update
sudo apt-get install ansible</code>2. Basic Ansible Usage
Using ansible‑playbook
An
ansible‑playbook(playbook) combines a series of automation tasks into an ordered, logical workflow.
Typical command to run a playbook:
<code>ansible-playbook docker.yml -i hosts -u alex -k -K</code>This command specifies the remote user and prompts for the SSH password and sudo password.
To discover module usage, Ansible provides
ansible‑doc:
<code># List all modules
ansible-doc -l
# Show documentation for the yum module
ansible-doc yum</code>Playbook Directory Structure
<code>├── group_vars <- common variables for all hosts
│ └── all
├── hosts <- inventory file
├── roles <- reusable roles (e.g., etcd, initial, loop)
│ ├── etcd
│ │ ├── files <- files to copy to target
│ │ ├── handlers
│ │ ├── tasks
│ │ └── templates
│ ├── initial
│ │ ├── files
│ │ ├── handlers
│ │ ├── tasks
│ │ └── templates
│ └── loop
│ ├── files
│ ├── handlers
│ ├── tasks
│ └── templates
└── site.yml <- main entry playbook</code>3. Ansible Playbook to Install Docker
The following playbook automates Docker installation on CentOS, including checks, package installation, adding the user to the Docker group, and installing pip and Docker‑Compose.
Prefer Built‑in Modules
When possible, use Ansible's built‑in modules instead of raw shell commands. For example:
<code># Using a shell command
- name: install yum-utils
shell: yum install yum-utils
# Using the yum module
- name: install yum-utils
yum:
name: yum-utils
state: present</code>Similarly, install Docker‑Compose with the
pipmodule:
<code>- name: install docker-compose
pip:
name: docker-compose
extra_args: "-i {{ pip.index_url }} --trusted-host {{ pip.trusted_host }}"</code>Using register and when
The
registerkeyword stores a task's result in a variable, and
whencan conditionally run subsequent tasks based on that result. Example:
<code>- name: check docker
shell: docker -v
register: result
ignore_errors: true
- name: include tasks if Docker is missing
include_tasks: install.yml
when: result is failed</code>Execution results (example screenshots):
Source: https://cloud.tencent.com/developer/article/2123531
Efficient Ops
This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.