Provisioning Docker Containers with Ansible

Provisioning Docker Containers with Ansible

Hi there! Today we are going to see how to provision docker containers using Ansible as a tool.

What is Ansible?

Ansible is an open-source automation tool, or platform, used for IT tasks such as configuration management, application deployment, intraservice orchestration, and provisioning.

Ansible automates and simplifies repetitive, complex, and tedious operations. Everybody likes it because it brings huge time savings when we install packages or configure large numbers of servers. Its architecture is simple and effective. It works by connecting to your nodes and pushing small programs to them.

Let's Start

So first we need to install ansible. As it is base on python so we can install it using pip

sudo pip install ansible

Or you can refer this official documentation for installation.

After installation, we need to configure ansible using its configuration file which is mainly located at /etc/ansible/ansible.cfg but you can find where it is using.

ansible --version

Here some basic configuration you need to do.

VirtualBox_Ansible Controller Redhat_20_12_2020_13_33_17.png

Also, we need to create an inventory file. In the inventory file, we store the IPs of nodes we want to configure. We can create this file anywhere with any name but just need to pass the path under the inventory tab in the configuration file.

Here's how an inventory looks like.

VirtualBox_Ansible Controller Redhat_20_12_2020_13_37_21.png

Now all that's left is to write the code. Ansible uses YAML format for the playbooks. Here our code for the same.

Screenshot (1013).png

You can see the whole code here

Now let's run the code.You can run the playbook using

ansible-playbook <path_to_playbook>

Here my playbook running

VirtualBox_Ansible Controller Redhat_20_12_2020_13_12_26.png

VirtualBox_Ansible Controller Redhat_20_12_2020_13_13_19.png

VirtualBox_Ansible Controller Redhat_20_12_2020_13_13_30.png Now let's check the managed node

docker service started

docker container running

As you can see our container is running and configured successfully. Now let's check if our container web app is running

Screenshot (1012).png Our web app inside the container is running as it should. So this way we can provision Docker containers using ansible

Thank you for your time...!!

Did you find this article valuable?

Support Written Wisdom by becoming a sponsor. Any amount is appreciated!