APACHE WEBSERVER CONFIGURATION IN DOCKER ?? USING ANSIBLE
Amit Sharma
CKA || 1xAWS || 4xGCP || 1xAzure || 2xRedHat Certified || DevOps Engineer [???????]@Searce Inc || Freelancer || Terraform || Ansible || GitLab || Jenkins || Kubernetes || Docker || Openshift || AWS || GCP || Azure
?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.
You can install ansible via pip command as it cam from python pip install ansible. after that you can check the version of Ansible.
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.
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
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.
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.
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/
To run this playbook you need to write ansible-playbook your_playbook_name.yml.
After Successfully running the playbook, you can check the container whether it is launched successfully or not.
As you can see that we have launched our container Myweb121 successfully. You can also check the ip of that container.
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.
Hope You had find this article interesting. Do like and comment !
Thank You ! ??????????????
Former SDE Intern @Raja Software Labs, Pune
4 年why you don't stop firewalld of localhost docker computer using firewalld module ???
Cloud Technical Solutions Engineer @ Google
4 年Great Amit Sharma
Terraform || Openshift(EX180) || AWS(CLF-CO2) || AWS(SAA-C03) Certified
4 年well written sir...??
Building Resilient Systems | Cloud & DevOps Engineer
4 年Good Amit Sharma???