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.
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.
Now all that's left is to write the code. Ansible uses YAML format for the playbooks. Here our code for the same.
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
Now let's check the managed node
As you can see our container is running and configured successfully. Now let's check if our container web app is running
Our web app inside the container is running as it should. So this way we can provision Docker containers using ansible