Setting up Docker Configuration By Ansible
When we talk about the industrial level which contains thousands and thousands of computers and its very difficult to go manually and configure the application in each system. Although we have to take care of the mistakes which generally happens while configuring manually. Today generation is very fast and the industry has to cope with it.So, here we need such a tool that will configure all systems automatically.
For this type of automation of configuration, there is a lot of tools available in the market like Puppet, Chef, Saltstack. Then why we choose ansible, other than ansible no tool support Declarative language. Ansible is a radically simple IT automation platform that makes your applications and systems easier to deploy. Avoid writing scripts or custom code to deploy and update your applications. Ansible is written in python and supports YAML command scripts. Although we need ansible to be set up in a controlled node only, we have to just provide the IP, username, and password of the managed nodes only and with a single command, we can configure thousands of systems at a time.
How to set up Ansible in Controlled Node?
- Min 2GB RAM
- 64-bit support required (kernel and runtime)
Here I am using RHEL8 for this practical and even though ansible is a product of RedHat, its not available in yum repositories, so we have to download it from other resources, as it is written in python, we can install it by using pip command.
pip3 install ansible
You can confirm it by the running the command
Here in the 3rd line you can see the ‘config file’ where i have setup this file in directory /etc/ansible/. Actually this directory contains the iventory in which hosts data is exit like ip, username, password, file
here myhosts.txt contains the data like ip, username, password. You can put the file ansible.conf in any folder but the file name must be same i.e ansible.cfg.
192.168.1.102 ansible_ssh_user=<username> ansible_ssh_pass=<password>192.168.1.104 ansible_ssh_user=<username> ansible_ssh_pass=<password>
here you can pass any no of IP’s where the docker to be configured
Now we have to install the sshpass which will help the ansible to communicate via ssh to all IP’s. As ansible is not available in yum repositories so we have to install it externally. After installing the sshpass, start and enabled it. We can check is ansible configured properly or not by using the command
$ansible all -m ping
If it is pinging successfully it means our setup is ready.
Let's start writing the playbook of ansible and you also come to know that how easy is to write the code in YAML format, because it is a declarative language, so there is no need to write much code for it. You can write your code within no time.
Ansible has a number of packages for diff purpose we use them to make ansible intelligent, we don't have to think about the commands of the os, ansible will mange by itself
- So here we first make a repository of docker by the yum_repository package, here we have to provide the name, description, file, baseurl, gpgcheck for more parameters see the official documentation of ansible. Before it, we have to provide the host.
- hosts: <name>
- name: docker-repo
description: 'Docker Repo'
2. After creating repo now its time to install the docker, basically docker is not compatible with rhel8, so we have to put nobest at last of the command. Here I use command module to install the docker-ce.
- name: docker_install
cmd: 'yum install docker-ce --nobest'
3. Now, let's start the docker service and enabled it so that don't have to start it again, and again as soon as the os launch docker service automatically starts. For this, we have to use the package module.
- name: docker-service
4. When you will try to launch the containers you will encounter an error which is related to docker, or docker-py python module needs to be installed in managed nodes, depends on the version on the version of ansible yo are using. For this, we use pip module to install docker or docker-py python module.
- name: python for docker
5. Now, its time to pull the required image from the docker hub by using the module docker_image.
- name: pulling_image
6. Before starting the container image lets first move our file to the managed nodes so that at the launching we will attach or mount this folder to the container. Here I am copying my file index.html to the managed index.html for this I’m using copy module.
- name: copy-file
7. ok, lets complete the last step of which is launching the container here I have mounted my files also, and don't forget to enable the container otherwise it will stop automatically.
- name: start-container
detach: yes # run in background
- "8081:80" #linking port 80 to 8081
save this yaml file with format .yaml or .yml and run this file by using command
$ansible-playbook <name of file>.yml
Here is the last view of ansible-playbook
Here you are confused about register and the debug keyword, actually, the register is used to store the complete output of the module in a variable, and debug is to print the value of that variable.
Now the docker is configured in all the managed nodes we can check it manually or we can browse the <ip>:8081, if it shows the result correct it means everything is working properly
So, that's all for this article, will meet you in another article, if you like this don't forget to clap and leave a comment if you any queries and suggestions.