Ansible Integration with Docker by writing a Playbook and launching Containers in our Managed Node.
NIRBHAY MAITRA
Full-stack Developer at Mobius by Gaian with expertise in React.js, Node.js and MongoDB
Server automation now plays an essential role in systems administration, due to the disposable nature of modern application environments. Configuration Management tools such as Ansible are typically used to streamline the process of automating server setup by establishing standard procedures for new servers while also reducing human error associated with manual setups.
Ansible offers a simple architecture that doesn’t require special software to be installed on nodes. It also provides a robust set of features and built-in modules which facilitate writing automation scripts.
Prerequisites
In order to execute the automated setup provided by the playbook , we’ll need:
- One Ansible control node: an Ubuntu 18.04 machine with Ansible installed and configured to connect to your Ansible hosts using SSH keys. Make sure the control node has a regular user with sudo permissions and a firewall enabled.
- One or more Ansible Hosts.
Ansible Playbook
Playbooks are the files where Ansible code is written. Playbooks are written in YAML format. YAML stands for Yet Another Markup Language. Playbooks are one of the core features of Ansible and tell Ansible what to execute. They are like a to-do list for Ansible that contains a list of tasks.
Playbooks contain the steps which the user wants to execute on a particular machine. Playbooks are run sequentially. Playbooks are the building blocks for all the use cases of Ansible.
Docker
Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and deploy it as one package. By doing so, thanks to the container, the developer can rest assured that the application will run on any other Linux machine regardless of any customized settings that machine might have that could differ from the machine used for writing and testing the code.
So let's start with our task.
- With a fresh installation of Ansible, like every other software, it ships with a default configuration file. This is the brain and the heart of Ansible, the file that governs the behavior of all interactions performed by the control node. In Ansible’s case that default configuration file is (ansible.cfg) located in /etc/ansible/ansible.cfg. The host file is used to store connections for Ansible playbooks.
- This is my Managed Node. As you can see, Docker is not installed yet on the Managed Node. We will do it with the help of Ansible configured at Controller Node.
- As you can see, my Yum is not configured as well. Before Installing Docker we need to configure Yum in our Managed Node as well. This will be also done with the help of Ansible.
- We will go to our controller node and create a yml file. Let it be named as docker.yml
- So we create a Playbook named as docker.yml where we will write our yml code for Yum configuration.
- Save the Configuration file and run the command. Make sure the identation of yml file is correct otherwise it will give errors.
- As you can see in my Managed Node yum is configured as well.
- Now we need to install the docker. It is not yet installed in my Managed node.
- After configuration of yum we can write a command of installation of Docker with nobest keyword. Ansible will do it for us.
- As you can see , the installation is successful and changes has been done in my managed Node denoted by yellow text.
- As you can see , the managed node now has Docker installed and ready to be used.
- But the Docker services has not been started yet. So we will start it's services by writing a script in the Controller Node again.
- So we start the services of Docker by writing the above script and assigning the state as started.
- Changes has been done and let's check our Managed Node.
- So Docker services has been started.
- Now here's the main point of our task. We need to pull the ubuntu image. But we cannot do with the help of a python Library called PIP. But PIP also requires some Python software called Python36. So we first install Python36 and then pip. At last we pull the docker Image.
- Changes has been Done and required softwares along with docker image has been pulled.
- Here is my pip library on my managed node showing it has been successfully installed.
- Here is my Docker image that has been downloaded successfully.
- Now we will Launch our Container .
- So the Container has been successfully launched.
- And here it is!!!.
So our first task of Integration of Ansible with Docker has been Done.