Automation using Ansible on Docker

Automation using Ansible on Docker

Task description:-

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 /var/www/html directory and start the web server

What is Ansible? :- Ansible is an automation tool that is mainly used for configuration management. We can Configure so many things using Ansible like web-server, Infrastructure, etc. Ansible works on declarative approach which means we don't have to tell how to install just tell what to install.

Why Ansible? :- Ansible is an Agentless tool that means we don't need to install it on the managed node just installed on the controller node and start working with ansible. Ansible is very intelligent tool and its intelligence comes from modules. As an IT Admin, we don't have to remember all os commands just tell which package you wanna install and ansible will do that thing for you!

Docker :- Docker is a container engine which is used to create the container. Container is Just Enough Operating system which includes all the required library,packages which is used for running any specific program.Docker is so fast means we can quickly create our container using the docker.

Due to Ram issue here i am using AWS Cloud for performing these steps

Now lets move towards the task:-

We will have to first install the Ansible:-

pip3 install ansible 
No alt text provided for this image

Before configuration, we have to create the Inventory file

vim /etc/myec2.txt
No alt text provided for this image


After this, we'll tell this location in the configuration file

Now we will have to configure the Ansible

vim /etc/ansible/ansible-cfg

Here we have to tell that where is our Inventory file. The inventory file contains the detail about the managed nodes we can also create group. Here We are passing host_key_checking=false so that we don't have to worry about key acceptance.

No alt text provided for this image

As I am Doing this task on AWS Cloud so we will have to enable root login in my machine and for this just log in to the instance and changed the permission in sshd configuration

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

one more thing we have to do just set the password of root

# passwd root

Now we can login using root account and perform the further steps.

Adding Docker repository in our system:-

No alt text provided for this image

to run this file we have to run this command

ansible-playbook repo.yml
No alt text provided for this image

Now we have added the Repository, so let's install docker I have done both in a single go.

ansible also needs one library installed (docker-py) so we have to first install the docker-py using pip and if pip is not available so we also have to install pip.

 tasks:
    - package:
             name: python-pip
             state: present

Now Pip is installed so we can install the docker-py library

 - name: Install
      command: "pip install docker-py"

Now we can start the docker services

service:
            name: docker
            state: started

Docker is installed and started. Next, we have to pull the image

- name: pull httpd image
      docker_image:
          name: httpd
          source: pull

We want to copy our file. so we can first create one directory on the managed node and then copy the file inside this directory and afterward, we can attach this volume to our container while creating the container.

 - name: Copy file
      file:
        path: /myweb
        state: directory
    - copy:
        src: "index.html"
        dest: "/myweb/"


 - name: create the container
      docker_container:
        name: myapplication
        image: httpd
        state: started
        restart: yes
        exposed_ports: 80
        ports: "1234:80"
        volumes: "/myweb/:/usr/local/apache2/htdocs"

By default httpd render the content of /usr/local/apache2/htdocs/ folder so we will mount our volume here. And we want to see our site so we can expose our container. Now if we go to our site we can see our container is launched and the website is running well.

No alt text provided for this image


No alt text provided for this image
Thanks for visiting my article!!

Code link:- Ansible-task-1

要查看或添加评论,请登录

Yogesh kumar Prajapati的更多文章

  • Microsoft automates to achieve more with Red Hat Ansible Automation Platform

    Microsoft automates to achieve more with Red Hat Ansible Automation Platform

    Hello, Connections! In this article, we are going to talk about what is Redhat Ansible and How Microsoft is using…

  • Deploy a Load Balancer and multiple Web Servers on AWS instances through ANSIBLE!

    Deploy a Load Balancer and multiple Web Servers on AWS instances through ANSIBLE!

    Hello connections!!Here is my another task of Ansible! Task Description:- ??Provision EC2 instances through ansible. ??…

    4 条评论
  • NETFLIX ON AWS

    NETFLIX ON AWS

    Netflix is the world’s leading internet television network, with more than 100 million members in more than 190…

  • Big DATA

    Big DATA

    Big Data refers to the amount of huge data that is Coming day by day and large data can not be stored and processed by…

  • Integrating RDS with k8s

    Integrating RDS with k8s

    Hello Connections Welcome to this article! Task Description:- Deploy the WordPress application on Kubernetes and AWS…

  • GCP automation using Terraform

    GCP automation using Terraform

    Hello connections! Welcome this article,we are going to create a Infrastrcture as code. Task Description:- 1.

    5 条评论
  • Automating Cloud infrastructure using terraform (task-4)

    Automating Cloud infrastructure using terraform (task-4)

    Hello, connections! In this article, we will create a highly secure environment in which our site will running in the…

  • Deploy Web Server on AWS through ANSIBLE!

    Deploy Web Server on AWS through ANSIBLE!

    ??Provision EC2 instance through ansible. ??Retrieve the IP Address of instance using dynamic inventory concept.

    4 条评论
  • Creating website using Terraform with all security appliances

    Creating website using Terraform with all security appliances

    Task Description:- Statement: We have to create a web portal for our company with all the security as much as possible.…

  • Automating web deployment on AWS using Terraform(Task2)

    Automating web deployment on AWS using Terraform(Task2)

    Hello Connections ! Welcome to this Article.In this article i am gonna show you how to deploy an web application on top…

社区洞察

其他会员也浏览了