Operations 8 min read

Master Ansible: Automate Docker Installation on CentOS with Playbooks

This guide explains how to install Ansible, use its basic commands and playbooks, and create a complete automation workflow to install Docker and Docker‑Compose on CentOS, covering installation methods, directory structure, built‑in modules versus shell commands, and conditional task execution with register and when.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Master Ansible: Automate Docker Installation on CentOS with Playbooks

Ansible Overview

Ansible is a Python‑written automation tool that enables cluster management and common operations tasks.

Many companies deploy services on clusters ranging from a few virtual machines to thousands; Ansible can perform batch operations across such clusters.

Automation platform based on Ansible scripts

Jenkins‑style CI/CD automation platform

SDK‑based Python automation platform

Docker containers with orchestration

This article shares experience using Ansible to automatically install Docker and Docker‑Compose.

1. Install Ansible

The control node must have Python 2. Windows cannot be used as a control node; supported OS include Red Hat, Debian, CentOS, macOS, BSD, etc.

Install via pip

sudo pip install ansible

Install via system package manager

CentOS (yum): sudo yum install ansible Ubuntu (apt):

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 in a defined order and logic, making task management easier.

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 and sudo passwords.

To view module documentation:

# List all modules
ansible-doc -l
# Show usage of 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
│   │   │   └── etcd-proxy.service
│   │   ├── handlers
│   │   │   └── main.yml
│   │   ├── tasks
│   │   │   ├── config.yml
│   │   │   ├── main.yml
│   │   │   ├── package.yml
│   │   │   └── service.yml
│   │   └── templates
│   │       └── etcd-proxy.conf
│   ├── initial
│   │   ├── files
│   │   │   ├── hosts
│   │   │   ├── resolv.conf
│   │   │   └── updatedb.conf
│   │   ├── handlers
│   │   ├── tasks
│   │   │   ├── main.yml
│   │   │   ├── mlocate.yml
│   │   │   ├── package.yml
│   │   │   ├── sysctl.yml
│   │   │   └── yumrepo.yml
│   │   └── templates
│   │       ├── centos7.repo
│   │       └── docker.repo
│   └── loop
│       ├── files
│       ├── handlers
│       ├── tasks
│       │   ├── main.yml
│       │   └── t1.yml
│       └── templates
└── site.yml          ← main entry playbook

3. Ansible Install Docker

The playbook checks whether Docker is available, installs Docker, adds the user to the Docker group, and installs pip and Docker‑Compose.

Prefer Built‑in Modules

When a built‑in module can accomplish a task, use it instead of raw shell commands.

# Using shell to install a package
- name: install yum-utils
  shell: yum install yum-utils

# Using the yum module
- name: install yum-utils
  yum:
    name: yum-utils
    state: present
# Using pip module to install docker‑compose
- name: install docker-compose
  pip:
    name: docker-compose
    extra_args: "-i {{ pip.index_url }} --trusted-host {{ pip.trusted_host }}"

Using register and when

register

stores a task's result in a variable; when evaluates that variable to conditionally run subsequent tasks.

- name: check docker
  shell: docker -v
  register: result
  ignore_errors: true

- name: include install tasks if docker not present
  include_tasks: install.yml
  when: result is failed

Execution results are shown in the following screenshots:

Result screenshot 1
Result screenshot 1
Result screenshot 2
Result screenshot 2
CentOSAnsibleplaybook
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

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.