APACHE WEBSERVER CONFIGURATION IN DOCKER ?? USING ANSIBLE

APACHE WEBSERVER CONFIGURATION IN DOCKER ?? USING ANSIBLE

?Task 1 - Ansible?

STEPS TO DO THIS TASK :

?? 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.

So lets start this task. First we have to create VM for Controller Node and Managed Node. As you can see in the below figure i am using Redhat VM as Controller Node where as Ansible Managed Node is for Managed Node.

No alt text provided for this image

You can install ansible via pip command as it cam from python pip install ansible. after that you can check the version of Ansible.

No alt text provided for this image

We can create a group of IP in /etc/myhosts.txt, in that you have to write that Managed Node Ip as a host in Controller Node. Give your Username and password of that Managed Node. By creating groups of IP known as Inventory can helps us to do automation that Node which are in group. As you can see there are three groups below in the figure. In all the groups we have a single Managed Node IP. If you want to do automation/configuration on multiple Managed Node then you can give their Ip in one of the Group. You can give any name to group.

No alt text provided for this image

Before running your playbook you need to check that all the Managed Node is connected or pingable with the Controller Node or not. This can be checked by following command. Those Managed Node IP is connected with Controller Node that only

No alt text provided for this image

Now, we are good to go for creating our own Playbook to run the code. Generally, Ansible Support YAML language in playbook. So, i have created two files one is a playbook ansible.yml and the other is home.html which we will be copying inside the container.

No alt text provided for this image

Also you need to give the path of myhost.txt (IP data) in this /etc/ansible/ansible.cfg file. To run this playbook we need to give ssh permission. This can be done via host_key_checking=FASLE. Also to disable command warnings you can give command_warning=FALSE.

No alt text provided for this image

Codes inside ansible.yml is explained in detailed way in the following image.

- name: Configuring Webserver using Docker
  hosts: webserver
  tasks:
  — name: Adding Docker repository
    yum_repository:
      name: Docker
      description: Docker Repo
      baseurl: https://download.docker.com/linux/centos/7/x86_64/stable
      gpgcheck: no




- name: Installing Docker package
  package:
    name: 
    — "docker-ce-18.09.1-3.el7.x86_64"
    state: present




- name: Starting docker service 
  service:
    name: "docker"
    state: started
    enabled: yes




- name: Installing Docker SDK for python3
  command: pip3 install docker




- name: Creating folder and copying html file...
  file:
    path: /webpages
    state: directory
- name: Copying html files
  copy:
    src: "home.html"
    dest: "/workspace/"


- name: Creating container using HTTPD Image 
  docker_container:
    name: MyWebServer
    image: httpd
    state: started
    exposed_ports:
    — "80"
    ports:
    — "8888:80"
    volumes:
    — /workspace:/usr/local/apache2/htdocs/


No alt text provided for this image

To run this playbook you need to write ansible-playbook your_playbook_name.yml.

No alt text provided for this image

After Successfully running the playbook, you can check the container whether it is launched successfully or not.

No alt text provided for this image

As you can see that we have launched our container Myweb121 successfully. You can also check the ip of that container.

No alt text provided for this image

Now you can connect this ip and see the web page i.e home.html.You can see this Output THIS IS ANSIBLE which i have written in home.html.

No alt text provided for this image

Hope You had find this article interesting. Do like and comment !

Thank You ! ??????????????




GAURAV DESHMUKH

Former SDE Intern @Raja Software Labs, Pune

4 年

why you don't stop firewalld of localhost docker computer using firewalld module ???

回复
Adarsh Kumar

Cloud Technical Solutions Engineer @ Google

4 年

Great Amit Sharma

Deepak Shah

Terraform || Openshift(EX180) || AWS(CLF-CO2) || AWS(SAA-C03) Certified

4 年

well written sir...??

Suyog Shinde

Building Resilient Systems | Cloud & DevOps Engineer

4 年

Good Amit Sharma???

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

Amit Sharma的更多文章

社区洞察

其他会员也浏览了