Ansible config docker-ce in Redhat8

Write an Ansible PlayBook that does the following operations in the managed nodes:

🔹 Configure Docker
🔹 Start and enable Docker services
🔹 Pull the httpd server image from the Docker Hub
🔹 Run the httpd container and expose it to the public
🔹 Copy the HTML code in /usr/local/apache2/htdocs/directory and start the webserver

Prerequisite:

  • Ansible installed.
  • configured Managed Node.

Step-1)

  • Here we have to first config docker-ce repo.
  • code:
- name: Docker repo
yum_repository:
name: docker
description: repo for docker
baseurl: https://download.docker.com/linux/centos/7/x86_64/stable/
gpgcheck: no

Step-2)

  • Install docker-ce.
  • In Redhat 8, docker-ce is not available so we have to manually installed.
  • code:
- name: Installing docker
command:
cmd: yum install docker-ce --nobest -y

Step-3)

  • Start the service of docker.
  • code:
- name: Starting and enabling docker service
service:
name: docker
state: started
enabled: yes

Step-4)

  • Now for pulling iso from docker hub, we need to install python and config pip, install docker-py using pip.
  • code:
- name: install python3.6
package:
name: python36
state: present
- name: conf pip for docker
pip:
name: docker-py

Step-5)

  • pull httpd iso.
  • code:
- name: Pull httpd docker iso
docker_image:
name: httpd
source: pull

Step-6)

  • Here I am using git to download our website code.
  • code:
- name: git
git:
repo: GITHUB_REPO
clone: yes
dest: /var/www/html

Step-7)

  • Start docker httpd container , expose it ,and for our code, I have already copied our code in /var/www/html , so it is good to mount the /usr/local/apache2/htdocs/ in the /var/www/html.
  • code:
- name: docker container
docker_container:
name: mywebserver
image: httpd
exposed_ports:
- 80
ports:
- 4444:80
volumes:
- /var/www/html/:/usr/local/apache2/htdocs/

Final Code:

- hosts: IP_ADDRESS
tasks:
- name: Docker repo
yum_repository:
name: docker
description: repo for docker
baseurl: https://download.docker.com/linux/centos/7/x86_64/stable/
gpgcheck: no
- name: Installing docker
command:
cmd: yum install docker-ce --nobest -y
- name: install python3.6
package:
name: python36
state: present
- name: Starting and enabling docker service
service:
name: docker
state: started
enabled: yes
- name: conf pip for docker
pip:
name: docker-py
- name: Pull httpd docker iso
docker_image:
name: httpd
source: pull
- name: git
git:
repo: GITHUB_REPO
clone: yes
dest: /var/www/html
- name: docker container
docker_container:
name: mywebserver
image: httpd
exposed_ports:
- 80
ports:
- 4444:80
volumes:
- /var/www/html/:/usr/local/apache2/htdocs/

Command:

ansible-playbook filename.yaml

Output:

The output of the Website:

  • Now use IP:4444 port.
Website

Full Look of Website:

Full Website

Github Link:

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store