The Playbook
This playbook installs Docker CE on Ubuntu and RHEL/CentOS, adds the Docker Compose plugin, and configures a non-root user:
---
- name: Install Docker
hosts: all
become: true
vars:
docker_user: "{{ ansible_user }}"
tasks:
- name: Install prerequisites (Ubuntu)
ansible.builtin.apt:
name:
- ca-certificates
- curl
- gnupg
state: present
update_cache: true
when: ansible_os_family == "Debian"
- name: Add Docker GPG key (Ubuntu)
ansible.builtin.apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
state: present
when: ansible_os_family == "Debian"
- name: Add Docker repository (Ubuntu)
ansible.builtin.apt_repository:
repo: "deb https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable"
state: present
when: ansible_os_family == "Debian"
- name: Install Docker (Ubuntu)
ansible.builtin.apt:
name:
- docker-ce
- docker-ce-cli
- containerd.io
- docker-compose-plugin
state: present
update_cache: true
when: ansible_os_family == "Debian"
- name: Install prerequisites (RHEL)
ansible.builtin.dnf:
name:
- dnf-plugins-core
state: present
when: ansible_os_family == "RedHat"
- name: Add Docker repository (RHEL)
ansible.builtin.command:
cmd: dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
when: ansible_os_family == "RedHat"
changed_when: true
- name: Install Docker (RHEL)
ansible.builtin.dnf:
name:
- docker-ce
- docker-ce-cli
- containerd.io
- docker-compose-plugin
state: present
when: ansible_os_family == "RedHat"
- name: Start and enable Docker
ansible.builtin.systemd:
name: docker
state: started
enabled: true
- name: Add user to docker group
ansible.builtin.user:
name: "{{ docker_user }}"
groups: docker
append: true
- name: Verify Docker installation
ansible.builtin.command:
cmd: docker --version
register: docker_version
changed_when: false
- name: Show Docker version
ansible.builtin.debug:
msg: "{{ docker_version.stdout }}"Run It
# Single server
ansible-playbook docker-install.yml -i "server1," -u ubuntu
# Multiple servers from inventory
ansible-playbook docker-install.yml -i inventory.iniMaster this topic with hands-on labs
Go beyond reading — build real projects in sandboxed environments with expert video guidance.
Browse Courses →Inventory Example
[docker_hosts]
web1 ansible_host=192.168.1.10
web2 ansible_host=192.168.1.11
db1 ansible_host=192.168.1.20
[docker_hosts:vars]
ansible_user=ubuntu
ansible_ssh_private_key_file=~/.ssh/id_rsaAdd Docker Compose Standalone (Optional)
If you need the standalone docker-compose binary:
- name: Install Docker Compose standalone
ansible.builtin.get_url:
url: "https://github.com/docker/compose/releases/latest/download/docker-compose-linux-{{ ansible_architecture }}"
dest: /usr/local/bin/docker-compose
mode: '0755'Get weekly IT automation tips
Docker, Ansible, Terraform, MLOps — curated insights delivered to your inbox. No spam.
Subscribe Free →Configure Docker Daemon
Add custom daemon settings:
- name: Configure Docker daemon
ansible.builtin.copy:
content: |
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
},
"default-address-pools": [
{"base": "172.17.0.0/16", "size": 24}
]
}
dest: /etc/docker/daemon.json
mode: '0644'
notify: Restart Docker
handlers:
- name: Restart Docker
ansible.builtin.systemd:
name: docker
state: restartedIdempotent and Safe
Run this playbook multiple times — Ansible only changes what is needed. If Docker is already installed and running, nothing happens.
Related Posts
- Ansible Automation in Minutes for Ansible basics
- Getting Started with Docker for Docker fundamentals
- Deploy OpenClaw with Docker Compose to deploy OpenClaw after installing Docker
---
Ready to go deeper? Check out our hands-on course: Ansible Quickstart — practical exercises you can follow along on your own machine.
Ready to learn by doing?
Stop reading tutorials — start building. Expert video courses with hands-on labs in real sandboxed environments.
Related Articles
Ansible vs Terraform When to Use
Ansible and Terraform solve different infrastructure problems. Learn when to use each, when to use both together, and how they complement each other.
Ansible Inventory: Static and Dynamic
Master Ansible inventory in INI and YAML formats. Learn host groups, variables, dynamic inventory plugins for AWS and Azure, and patterns for scaling.
Learn Ansible Free: Beginner Guide
Start learning Ansible for free with hands-on examples. Master playbooks, inventory, modules, and roles to automate infrastructure.
Ansible Playbook Error Handling
Handle errors gracefully in Ansible playbooks. Learn ignore_errors, rescue blocks, retries, assertions, and proven recovery patterns for reliable automation.
Ansible Roles Reusable Automation
Structure Ansible automation with roles for reusability. Directory layout, defaults, handlers, Galaxy integration, and testing with Molecule.
Ansible Vault: Encrypt Secrets Safely
Learn Ansible Vault to encrypt passwords, API keys, and variables. Covers vault create, edit, encrypt_string, and CI/CD usage.
Explore topics
Browse more articles on the topics covered here.