Operations 8 min read

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.

Efficient Ops
Efficient Ops
Efficient Ops
Automate Docker & Docker‑Compose Installation with Ansible – A Step‑by‑Step Guide

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

sudo pip install ansible

Using system package managers

On CentOS: sudo yum install ansible On Ubuntu:

sudo apt-get install software-properties-common
sudo apt-add-repository ppa:ansible/ansible
sudo apt-get update
sudo apt-get install ansible

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:

ansible-playbook docker.yml -i hosts -u alex -k -K

This command specifies the remote user and prompts for the SSH password and sudo password.

To discover module usage, Ansible provides ansible‑doc:

# List all modules
ansible-doc -l
# Show documentation for the yum module
ansible-doc yum

Playbook Directory Structure

├── 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

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:

# 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

Similarly, install Docker‑Compose with the pip module:

- name: install docker-compose
  pip:
    name: docker-compose
    extra_args: "-i {{ pip.index_url }} --trusted-host {{ pip.trusted_host }}"

Using register and when

The register keyword stores a task's result in a variable, and when can conditionally run subsequent tasks based on that result. Example:

- 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

Execution results (example screenshots):

Source: https://cloud.tencent.com/developer/article/2123531
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DockerautomationDevOpsLinuxAnsiblePlaybook
Efficient Ops
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.