Apache Webserver Configuration on top of Docker using Ansible
In this article, I will show you how to configure Docker and pull httpd (Apache Webserver) image and exposing it to public using Ansible.
So, without wasting much time, let’s just quickly move towards the hands-on and start making Ansible Playbook for the same.
Step 1: Install Ansible on Controller Node
For installing ansible on controller node, we have 2 ways to install it, totally dependent on you
a. We can install it by using python3 with pip command
# pip3 install ansible is the command for same
b. Another way is we can use the package manager command according to the operating system, in my case I’m using RedHat OS, so we can install it with yum command
# yum install ansible is the command for same
So, I have already installed it in my controller node and we can check it with the help of ansible --version command. So let’s quickly check
Step 2: Create Inventory File and configure Ansible Configuration File.
In ansible we have to create an inventory file in which we define the IP of our Target Node and credentials for ssh access username and password in case of BareMetal and VM and public key in case Target node is on cloud.
And we can create inventory file anywhere in the controller node with the extension of .txt
In my case the IP of my Target Node is 192.168.43.221.
And I have created my inventory file with the name of ip.txt at location /root/ip.txt
After that we have to configure the configuration file of ansible in which we have to define the path of our inventory file.
And set host_key_checking=False, this is basically while connecting to ssh this ask yes or no, by doing this it won’t prompt us with that yes/no pop-up.
Step 3: Creating ansible playbook to install docker and configure webserver on top of it
a. First we have to configure yum repository for docker and for that we have to use yum_repository module in Playbook.
b. Now we will install docker in all the managed nodes and for that we use command module
c. So now as our docker software is successfully installed and we have to start services of docker for that we have to use service module
d. We will now create one directory in which we have to copy our web page and after that need for mounting purpose with the help of file module.
e. And now we have to put the webpage into our Target Node for that we have already prepared a simple web page in our Controller Node and we will copy that web page to our Target Node with the help of copy module.
f. Since our docker is installed on our managed node and its services is start now we’ll pull httpd (Apache Webserver) image with the help of docker_image module.
g. And at the last we’ll launch our docker container and expose it to public on some port number with the help of docker_container module.
Step 4: Run Ansible Playbook in Controller Node
To run playbook in controller node we use command
# ansible-playbook playbook-path
After successful configuration of all the things we can see our page content through browser.
Target_node_IP:port_no
Thanks for Reading!!