First, you need to install Ansible. Just follow this link to install Ansible on your operating system installation guide.
After installation, create new directory called ansible-docker.
Create a new file called ansible.cfg as the Ansible configuration setting and then define the inventory file.
Then create a new file called hosts, where the name is defined on ansible.cfg.
NB: Don’t forget to change the IP Address and host name.
After setup the Ansible configuration setting & inventory file, let’s create a YAML file called playbook.yml
Then create roles directory:
Config, On this directory I will create a directory called tasks. After that, I should create yaml file called main.yml to run update, upgrade & install many dependencies.
Docker, On this directory create 2 directories called tasks & templates.
On tasks directory create new file called main.yml. This file contains Docker installation, Docker Compose installation & private registry setup.
In the template, create a template file using a jinja file named daemon.j2. This file contains configuration for private registry settings (optional).
NB: Field the IP using your remote server private IP
After all setup, Your project directory should look like this:
Test & Run
Okay, now test Your playbook.yml file using this command.
If You don’t have any errors, run the playbook using this command.
Wait until finish.
Conclusion
In this post, I just show you how to install Docker in a specific version using Ansible Playbook when you have one or more servers.
Thank You for reading this post, If You have suggestions or questions please leave them below. Thanks
NB: In this case, I just set the user as root. I installed the Docker on Ubuntu Server 22.04. For full code follow this link ansible-docker.