Web Server on Docker using Ansible

Web Server on Docker using Ansible

Task 1 : Linux Automation using Ansible (RH294) @LinuxWorld

Statement:

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 into the Docker container.


Let's get started,

Prerequisites

I'll use 2 VMs, I assigned them hostnames as:

  • Manager Node : rhel8GUI
  • Managed Node : rhel8cli1

Make sure there is connectivity between them. Internet Connectivity must be available so that softwares can be downloaded. Also, have Ansible installed in the Manager node with inventory configured with the managed node's IP address and SSH Credentials. Install sshpass in the Manager Node so that SSH connection is possible with passwords. Also disable host key checking in the Ansible config file.

No alt text provided for this image
No alt text provided for this image

Inventory file is /etc/myHosts.txt as configured here in the config file:

Finally when above prerequisites are satisfied proceed further.

Ansible Playbook

Create an Ansible Playbook, task1.yml file with the following code:

No alt text provided for this image

Running the Playbook

And then just run the below command and everything will be configured for you:

$ ansible-playbook Task1.yml

No alt text provided for this image

Access the Webpage in a browser by navigating to port 8080 of the managed node or by using the curl command.

No alt text provided for this image

Also, verify docker is running properly in the managed node:

No alt text provided for this image

Now, Let's breakdown the Code step by step.

Step 1: Configuring the yum repository

- hosts: web
  tasks:
    - name: Add Docker Yum Repository
      yum_repository:
        name: docker
        description: Docker-CE YUM repo
        baseurl: "https://download.docker.com/linux/centos/7/x86_64/stable/"
        gpgcheck: no

This will create a new docker.repo in /etc/yum.repos.d/

Step 2: Now we will install docker.

- hosts: web
  tasks:
    - name: Installing Docker CE
      package:
        name: "docker-ce-18.06.3.ce-3.el7.x86_64"
        state: present

Step 3: Next we will start the docker service.

 - hosts: web
   tasks: 
    - name: Start the Docker service
      service:
        name: "docker"
        state: started

Step 4: Install Docker SDK for Python

Using the Docker Ansible modules requires having the Docker SDK for Python installed on the host running Ansible. You will need to have >= 1.7.0 installed. For Python 2.7 or Python 3, you can install it as follows:

 - hosts: web
   tasks:    
    - name: Installing Python
      package:
        name: "python36"
        state: present


    - name: Installing the Docker Python SDK
      pip:
        name: docker


Step 5: Pulling the httpd docker image from docker hub

 - hosts: web
   tasks:
    - name: Pull httpd Image from DockerHub
      docker_image:
        name: "httpd"
        source: pull
       

Step 6: Creating a sample HTML page

- hosts: web
  tasks:
    - name: Create a sample html page "index.html"
      copy:
        content: "It Works! "
        dest: "/home/index.html"


This will create index.html file to the /home dir of the host machine.

Step 7: Running the httpd container

This will create a new container with the name myHTTPContainer having the index.html file and with port forwarding as 8080:80.

- hosts: web
  tasks:    
    - name: Creating the container with httpd image
      docker_container:
        name: myHTTPContainer
        image: httpd
        state: started
        ports:
          - "8080:80"
        volumes: 
          
          - /home:/usr/local/apache2/htdocs/

That's the final step.

Github Repo:


To view or add a comment, sign in

More articles by Shivam Baghel

Others also viewed

Explore content categories