Configure Webserver on docker container using Ansible
🔰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 docker container and expose it to the public
🔹 Copy the html code in /var/www/html directory and start the web server
For this task, we should have atleast two Virtual Machines, one as a Controller Node and the other as a Manage Node. I am using RHEL8 for this task. Now, in the controller node, install ansible via pip (pip3 install ansible). Here, we need to create a inventory having list of IP address of the manage nodes. We also need to give username and password for doing SSH and authentication. In the global configuration file /etc/ansible/ansible.cfg file, we need to write the path to Inventory so that ansible knows its nodes. While running playbook, it will need SSH permissions, so add host_key_checking=False in the ansible global configuration file. If want to disable command warning you can use command_warning=False in the same file above.
Create a workspace and create a .yml file inside it. This yml file will be our ansible playbook to configure webserver on top of docker.
- name: Configuring Webserver using docker
— name: Adding docker repository to yum configuration file
description: “Docker Repo”
- name: Installing docker package
- name: Starting docker service
- name: Installing Python36
- name: Installing docker for python3
command: pip3 install docker
- name: Creating our WorkSpace for Manage Node
- name: Copying html file to the workspace created
- name: Pulling httpd image and creating container and exposing ports
Please go through below image for the explaination of the code:
I have also created a index.html file in the same directory, which I need to /root/docker-web directory in ansible code.
Now, that we have our code ready, to run the playbook write command ansible-playbook <filename>.yml
If the code runs fine, you should get similar output on the screen.
To check our website has been hosted or not we can use curl. Before that we make sure our container has been booted up and we need IP address of the container created. You can do docker inspect <containerName> to get the IP address and docker ps to get the current running containers on your manage node.
You can see the output of index.html file has come up. Hence our webpage has been successfully hosted on docker container using Ansible.
Feel free to get connected with me and to drop any suggestion. Thank You!