Docker using Ansible Playbook

Docker using Ansible Playbook

Ansible is a open-source automation tool, used for It task like Configuration Management. This is the world of automation and declarative language plays an important role in the automation field, ansible is the best example of Declarative language. Ansible automates the deployment work for you, it runs on many UNIX-like system , and can configure any OS.

In this task, we configured docker, and created a container and exposed it to the public.

First we need to install the Ansible software, inside your OS (rhel8). Install the software using "pip3" as ansible runs over python.

  • pip3 install ansible

This will install ansible for you. now we need to create inventory which would have the information for the managed nodes for ansible and create the config file that would have the inventory info.

No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

Whenever ansible will run, it will fetch information from this config file, which will direct it to the managed node info.

Ansible behind the scenes connect to the managed nodes with SSH, and perform its task. For the proper SSH connection we need to give the username and password in the host file, and in order to use the password via SSH we need to install "sshpass". Now to install sshpass we need a extra repo, which will install the software for us, the extra repo that we required will be automatically configured through "epel-release" software , so we need to install the software. Get the software link form internet and install it in your controller node.

No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

Now we are in a state to create the ansible playbook which will configure docker for us, create a yaml file.

No alt text provided for this image
No alt text provided for this image

In this code we have several modules that will configure the docker for us, First Copy module will copy the entire docker repo from our local system to the managed node and using "package" module we will install docker software and httpd in the managed node, with the help of the docker repo. Further service module will start and enable the docker service for us, in managed node. We have used pip module as well, it is because Ansible is a tool that runs over python and entire work is done within python, in order to configure docker, we need to perform the entire work within python. We have a python library called "docker-py" that performs every command that docker does, but form within python hence to install that library we use pip module.Once the library is installed we are in the mode of creating container. But for creating a docker container we need the image, in our local system where the docker is configured i.e. managed node. Docker_image is the module which will fetch/pull the image for us from docker hub, and docker_container is the module that will create a container for us. Further the copy command will copy our website from controller node to managed node, which will be hosted using Apache server.

This is my vj.html file.

No alt text provided for this image

Now, we will check the manage node by pinging ,that it is connected or not.

No alt text provided for this image

Once our code is ready we are in the state to deploy it to configure docker in the managed node.

No alt text provided for this image
No alt text provided for this image

Once the playbook runs with return code=0 , our entire docker is configured.

Now go to managed node and check for the things we have configured.

No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

This is the software and status for the docker which was started and enabled by service module, and installed by package module.

No alt text provided for this image
No alt text provided for this image

Now , we ca access our webpage by giving url in browser.

No alt text provided for this image

THANKS ALOT !!!!

#thanks_to_vimal_daga_sir

#right_education #right_mentor

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

Vaibhav Jain的更多文章

社区洞察

其他会员也浏览了