In one of my previous blogs, I explained infrastructure provisioning (which means creating infrastructure on the cloud). We talked about IaC and one of its tools 'terraform'. In this blog, We will deep dive into configuring that infrastructure automatically just like we created our infrastructure.
Terraform Vs Ansible ⚡
We learned that Terraform is used to write instructions for creating our infrastructure on a cloud platform. In easy words, we say - "Hey Terraform, take all the instructions written in the .tf file and create a Linux server with Ubuntu installed in it". After that, the job of Terraform is done. It is only responsible for creating infrastructure.
Before explaining Ansible, let me ask you a simple question - Now that your newly created server ( an infrastructure on the cloud ) is up and running, tell me what is the very next thing you want to do on it? Your answer may be - okay! we can log onto it, and install or update apps, maybe copy some files from your laptop etc. We call these tasks configuring infrastructure (or infrastructure configuration)
Now, it was a single server hence configuring it was not a big deal, but how about installing a bunch of software on 10s or 1000s of similar servers? You would repeat the same step over and over! right? well to solve this repetitive task thing, Ansible is the tool that comes to the rescue!
How does Ansible work? 🤔
Ansible is an open-source configuration management tool that is used to manage and configure computer systems, networks and applications more easily, more efficiently and less error-prone. In easy words - we say to Ansible "Hey Ansible, here is the bunch of tasks that you have to perform on servers." By a bunch of tasks, I mean it could be anything from installing a simple app to copying or downloading some files from the internet.
All we need provide Ansible with a list of IP addresses of servers and a file containing all the tasks that we want to perform on those servers.
Components of Ansible
Agentless - it's not a component but a feature of Ansible, Ansible is an agentless tool which means the target systems that we are configuring do not need to have Ansible installed on it. instead, Ansible communicates via SSH. It ssh into the server and performs the tasks.
Inventory - inventory is a file, which contains the information about the systems which it manages. This file contains a list of hostnames or IP addresses. Ansible uses inventory file to know where to execute tasks.
Playbooks - Playbooks are the heart of ansible automation. They are written in YAML and contain a series of tasks and configurations to be executed on target hosts.
Modules - Ansible uses modules to perform specific tasks on target hosts. suppose you want to install apps or want to create users, Ansible provides a module for such tasks. suppose you want to create a user on the target server, Ansible will use a module ( it's just a small self-contained script) and using that module, the user will be created.
Tasks - Tasks are individual units of work within a playbook. Each task typically calls a specific module to perform a task on one or more target hosts.
Simple workflow of Ansible
You start by defining your inventory ( list of host IP addresses).
You Create playbooks and run Ansible commands from your control machine.
Ansible then connects to the target hosts using SSH and begins executing the tasks defined in the playbook.
Ansible's design promotes idempotence, meaning you can run the same playbook multiple times without causing unintended side effects. If the desired state is already achieved, Ansible won't make unnecessary changes.
That's the wrap for this blog. In future blog, we will do some practical stuff related to Ansible - maybe automate configuring some servers. Stay tuned for that. I hope this blog helps you understand complex terminologies related to Ansible. Do Like, Share and comment down below if you have any queries related to it. Have fun 🍻!