Introduction
Docker setup through Ansible using roles is an efficient method to configure Docker environments using Ansible roles. This guide simplifies the intricate process of deploying and managing Docker by leveraging Ansible’s role-based automation. By breaking down complex tasks into modular roles, this approach ensures a structured and streamlined Docker setup across multiple hosts.
The guide elucidates step-by-step procedures, illustrating the creation and utilization of Ansible roles to seamlessly install Docker, manage containers, set configurations, and maintain consistency. Through this comprehensive yet straightforward guide, users gain insight into orchestrating Docker environments efficiently with Ansible’s versatile role-based automation.
Prerequisites:
- Learn the role in Ansible in more depth from my post
- Learn Docker.
Please follow the below steps for Docker setup through Ansible Using roles
Step 1 – Init docker-setup role
Use the script below to initialize the docker-setup role
# Go to Ansible root directory
cd /path/to/ansible
# Go inside roles directory
cd roles
# Create docker setup role
ansible-galaxy role init docker-setup
Step 2 – Remove unnecessary default files and directories
Use the below command to remove unused default files and directories
rm -rf README.md files meta templates tests vars
Step 3 – Add the below content into tasks/main.yml and handlers.yml
Overwrite tasks/main.yml with below content
# tasks file for docker
- name: Install dependencies
apt:
name:
- apt-transport-https
- ca-certificates
- curl
- software-properties-common
update_cache: yes
- name: Add Docker GPG key
apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
- name: Add Docker APT repository
apt_repository:
repo: 'deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable'
state: present
- name: Install Docker
apt:
name: docker-ce
state: present
update_cache: yes
notify: Restart Docker service
- name: Add {{user}} user to docker group to enable this user can run docker commands without sudo
user:
name: '{{user}}'
groups: docker
append: yes
Overwrite handlers/main.yml with below content
# handlers file for docker
- name: Restart Docker service
service:
name: docker
state: restarted
Step 4 – Create docker-setup.yml playbook with the below contents
- name: Docker setup
hosts: docker_servers
become: yes
roles:
- roles/docker-setup
Please add other roles also as per your need
Step 5 – Add docker_servers group(optional) in inventory file
Let us assume you have inventory file inventories/backend_servers.yml
# backend_servers.yml
all:
children:
docker_servers:
hosts:
prod-docker-01:
prod-docker-02:
vars:
system_user: ubuntu
user: {{system_user}} # user is a dynamic variable whose value is assignd to the value of system_user
Please make sure prod-docker-01 is accessible through SSH.
Step 6 – Execute docker-setup.yml playbook
# First execute docker-setup playbook in check mode to identify and resolve any issues (e.g syntax issue, undefined variable issues)
ansible-playbook docker-setup.yml -i inventories/backend_servers.yml -l prod-docker-01 --check
# After identifying and fixing issues in the above step, we should execute it first on one host
ansible-playbook docker-setup.yml -i inventories/backend_servers.yml -l prod-docker-01 -v
# If playbook executes successfully on one host then we should execute it on all hosts
ansible-playbook docker-setup.yml -i inventories/backend_servers.yml -vv
Note: Increase verbosity in the above command with –vv or –vvv in case you run into any issues.
Key Takeaways from this post
- How to develop a docker-setup role in Ansible.
- How to create a docket-setup playbook and its inventory file
- How to execute a playbook
Frequently Asked Questions
Q1 – Where should I add default variables related to the docker-setup role?
Answer – Ansible creates a defaults/main.yml file when we create a role. This file is meant for putting default variables (e.g. docker_version).
Conclusion
In conclusion, utilizing Ansible’s roles for Docker setup streamlines the deployment, configuration, and maintenance processes with remarkable efficiency. This approach, outlined in the guide, simplifies complex tasks by organizing them into modular roles, ensuring consistency and scalability across diverse environments.
By employing Ansible’s role-based automation, users achieve a structured and reproducible Docker setup, enabling swift provisioning and management of containers. The guide’s step-by-step instructions empower users to orchestrate Docker environments seamlessly, from initial installation to configuration and ongoing maintenance.
Embracing Ansible roles for Docker not only enhances productivity but also promotes standardization and reliability, making it an invaluable tool for orchestrating containerized applications and infrastructure with ease.
Enjoy the post!!