Configure Docker Using Ansible.
What is Ansible ?
Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code. It runs on many Unix-like systems, and can configure both Unix-like systems as well as Microsoft Windows. It includes its own declarative language to describe system configuration. Ansible is agentless, temporarily connecting remotely via SSH or Windows Remote Management (allowing remote PowerShell execution) to do its tasks.
What is Docker?
Docker is a set of platform as a service (PaaS) products that use OS-level virtualization to deliver software in packages called containers. Containers are isolated from one another and bundle their own software, libraries and configuration files; they can communicate with each other through well-defined channels. All containers are run by a single operating system kernel and therefore use fewer resources than virtual machines.
Task Description
🔹 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
Let’s start writing the playbook :-
🔹 Configure Docker
To configure docker we have to add docker repo in yum
when we run this playbook our docker repo will be add
Now we have to install docker into managed nodes , to install docker using ansible we have to install python library for docker
let’s the write playbook to install docker:-
⚠️ We know Ansible is a configuration management tool. when we run any playbook ansible only call the system command to perform the task. when we want to install docker package, in some system we have to use some extra keyword like “ — nobest” to install docker otherwise it will not install. In Ansible package module we have not any option to write extra keyword so we have to use command module. if above task is not running in your system than you can run this below task playbook.
after run this playbook your docker software installed properly.
🔹 Start and enable Docker services
After install the docker software we have to start the services let’s the write the playbook to start and enable docker service
⚠️ when we start docker service they start it on temporary basis if the system restart our service will stop so we have to start our service permanently , to start our service permanently we have to enable this.
now our service start and enabled.
🔹 Pull the httpd server image from the Docker Hub
To launch docker container we need OS image, here our requirement to configure webserver so we have to download httpd images , so let’s write playbook to download httpd image from docker hub
🔹 Run the docker container and expose it to the public
Now we will run docker container and expose it , after expose we can access our webpages.
🔹 Copy the html code in /var/www/html directory and start the web server
⚠️ In httpd webserver by default document root is /var/www/html but we have download httpd image from docker hub it is a official docker so as per this image by default document root is /usr/local/apache2/htdocs/ .
So we have copy our webpages in /usr/local/apache2/htdocs/ .
Now let’s see our complete playbook
This is complete ansible playbook , now run this playbook in your controller node
Now we can check from managed nodes , everything is configure which we write in our playbook.
Now let’s check Can we access our webpages or not ?
Copy your docker host IP with port number which you allow and paste it on your browser.
http://dockerhost_ip:port_number/webpage_name
In my case it is
http://192.168.43.226:8080/task.html
Now our webserver is configure on docker container using Ansible.