We are going to learn ansible from scratch so you need to understand why we need ansible and how ansible works.
What is Ansible used for?
Ansible is a tool used to automate tasks such as application deployment, server updates and patching, and cloud provisioning. configuration management etc. Linux admins use Ansible to do the repetitive task that needs to run weekly, does daily ansible not require an agent to work or is no other security infrastructure needed?
Let’s start learning ansible from basic
Why do we need ansible?
Let’s take a practical example. Let’s say you are the Linux admin of a company you are managing more than 100 + servers in your day-to-day life. You need to update the server packages once a week. The traditional way is to ssh into all servers and run the update command. Still, the problem is that the task is very much boring and time-consuming let’s say you are using ansible and assume that all the servers have been configured in your control node you can update all 100 servers by just running one command in your control node. This is just an example case you can do similar operations using ansible automation
So I hope you have got a basic understanding of ansible and why we need ansible.?
Ansible doesn’t manage one system at a time which means it executes the tasks in all servers at the same time. After connecting to your nodes, Ansible pushes small programs called “Ansible Modules”. Ansible runs the modules on your nodes and removes them when finished. Ansible manages your inventory in simple text files (These are the host’s files).
How does ansible work?
In order to understand how ansible works, we need to be familiar with all the terms related to ansible.
- Control node
- Managed node
- Inventory
- Playbooks
Plays
Roles
Tasks
Handlers - Modules
- Plugins
Control node
The Ansible control node is the machine where ansible is installed and runs various ansible commands. it can either be your own localhost machine or any server. Using the control node we can manage all other nodes by connecting them over ssh and using ansible as in the figure.
Managed nodes
These are called target systems. In the system we are planning to manage using ansible it is not necessary that all the target systems must have ansible installed.The control node uses an inventory file to connect to various managed nodes and determine which task to execute in which group of hosts.
Inventory
Ansible works at multiple managed nodes at the same time inventory is a defined list or group of lists which contains the details of the managed node ie either a hostname or IP address once inventory is defined we can use patterns to select the groups or host in which we want to run ansible commands.
Playbooks
Playbooks are commands written in YAML which is easy to read and write. it is an ordered list of tasks which can run repeatedly. They can orchestrate steps of any manually ordered process similar to bash script.
Plays:
Each playbook is composed of one or more ‘plays’ in a list. The goal of a play is to map a group of hosts to some well-defined roles, represented by things ansible calls tasks.
Task:
At a basic level, a task is nothing more than a call to an ansible module. We can define one or more tasks in a playbook. Ansible executes these tasks one by one and provide an output for each task completed.
Roles:
Roles let you automatically load related vars, files, tasks, handlers, and other Ansible artifacts based on a known file structure. After you group your content into roles, you can easily reuse them and share them with other users. Similar to like defining variables inside a code.
Modules
Modules are simply the unit of code which ansible executes in order to do a task. Each module has a specific use case you can use either a single module or multiple modules in a task or create an ansible module yourself
take a look at various ansible modules
https://docs.ansible.com/ansible/2.9/modules/modules_by_category.html#modules-by-category
Now let’s answer the question of how ansible works.
Ansible works by connecting to nodes over ssh from the control node and pushing out various programs using the ansible modules and removing those after finished. We can also execute tasks on our localhost using an ansible playbook we will dive deeper into it in the next blog.
Summary:
In this tutorial, We have learned the basics of Ansible in this article.