Ansible loves Docker

In this Article i explain you how you can automatically deploy docker to you're web server via ansible.
First of all open you're ansible inventory file with you're preferred editor (I'm using vim):
vim /etc/ansible/hosts
Than add the following of course replace the ip with you're webserver ones 🙃 :
[webserver]
222.110.10.2
If you have not already have a public ssh keygen generate it with:
ssh-keygen -t rsa
and copy it to the destination location:
ssh-copy-id root@222.110.10.2
Enter you're password and done. Now we can start deploying docker with ansible. in the following i give you the installdocker.yml script for deploying ansible on a red hat based system:
---
- name: Install Docker
hosts: webserver
become: true
become_user: root
tasks:
- name: Update System
command: yum update -y
- name: Add repository docker-ce-stable
ansible.builtin.yum_repository:
name: docker
description: Docker CE Stable - $basearch
baseurl: https://download.docker.com/linux/rhel/$releasever/$basearch/stable
gpgkey: https://download.docker.com/linux/rhel/gpg
repo_gpgcheck: 1
enabled: true
state: present
- name: Install Docker
ansible.builtin.yum:
name:
- docker-ce
- docker-ce-cli
- containerd.io
- docker-buildx-plugin
- docker-compose-plugin
state: latest
- name: Start & Enable Docker
systemd:
name: docker
state: started
enabled: yes
now just run it with:
ansible-playbook installdocker.yml