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.
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 ansibleInstall 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 ansible2. 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 -KThis 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 yumPlaybook 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 playbook3. 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
registerstores 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 failedExecution results are shown in the following screenshots:
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.
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.
