Javatpoint Logo
Javatpoint Logo

Ansible Handlers

What is Ansible?

Ansible is a basic open-source IT motor that mechanizes application organization, intra-administration coordination, cloud provisioning, and numerous other IT devices.

Ansible is a device that permits us to make and control three key regions that we would have inside our tasks climate.

To start with, there is IT mechanization, so we can compose guidelines that robotize the IT arrangement that we would ordinarily do physically before.

The second is a setup the board and keeping a steady design of all frameworks in the foundation.

At long last, we have programmed arrangement, so we really want to increase our current circumstance; we can push out guidelines that can convey naturally to various servers. Basically we need to accelerate and make our tasks group more productive.

Why Ansible?

We need to consider Ansible as one more apparatus inside our DevOps climate to assist with dealing with the help, which falls on the activity side of the DevOps condition. Thus, on the off chance that we look here, we have an image of the individual who is a framework manager. That individual is liable for keeping up with the foundation for every one of the various servers inside the individual's organization, so a portion of the servers that the individual might have that the individual needs to keep up with could be web servers running apache or data set servers running MySql.

Assuming we just have a couple of servers, that is genuinely simple to keep up with in the event that we have three web and two data set servers.

Ansible-pull setup Apparatus

There are various approaches to setting up various conditions for server ranches.

One is to have a key server that has every one of the directions on and afterward on every one of the servers that interface with that fundamental expert server, we would have a piece of programming known as client introduced on every one of those servers that would convey to the principal ace server and afterward would occasionally either refresh or change the design of the slave server, this is kn own as a force setup.

An option is a push setup, which is somewhat unique. The primary contrast is, similarly as with a draw design, we have an expert server where we set up the guidelines, dissimilar to the force design where we have a client introduced on every one of the administrations with a push design. We have no client introduced on the far off servers. We are just pushing out the design to those servers and compelling a rebuild or a new establishment in that climate.

Thus, Ansible is one of those second conditions where it is a push setup waiter, and this difference with other famous items like culinary specialist and manikin, which have the expert slave engineering with an expert waiter interfacing with a client on a remote slave climate where we would then be pushing out the updates with Ansible we are pushing out the help and the construction of the waiter to far off equipment. We are putting it onto the equipment unessential to the design.

Ansible Engineering

While setting up an Ansible climate, we first need to have a neighborhood machine where we will have the entirety of our guidelines and the force of the control that we would push out to the distant server. In this way, the nearby machine is where we will begin and do the entirety of our work; associated from the neighborhood machine are the various hubs pushing out the various designs that we would set up on the nearby machine. The setups we would compose would be those in code or inside a module. So we do this on our neighborhood machine for making these modules, and each is a reliable playbook.

The neighborhood machine likewise has a subsequent work: to deal with the stock of the hubs we have in our own current circumstance. The neighborhood machine can associate with every hub in our equipment network through SSH clients.

Playbook

To start with, we investigate playbooks we would compose and establish for the ansible conditions. Thus, the center of Ansible is the playbook; this is where we make the guidelines we write to characterize the design of our equipment. In this way, the playbook is a bunch of directions that design our various hubs. Each arrangement of directions is written in a language called YAML, a standard language for designing server conditions.

Working of Ansible

Let us have a look at how Ansible works in the real world. In the real-world environment, we would have the Ansible software installed on a local machine, which then connects to different nodes within our network. On the local machine, we will first have out the playbook, which is the set of instructions for setting up the remote nodes, and then identify how we will connect to those nodes. We will have an inventory; we use secure SSH connections to each server. So, we encrypt the communication to those servers. We can grab some basic facts on each server to understand how to push out the playbook to each server and configure that server remotely. The end goal is to have a consistent environment.

Ansible Tower

Ansible tower is an extra product that a red hat created that puts the cherry on top of the ice cream or the icing on our cake.

Ansible is a command-line tool, and as well tower is a framework designed to access Ansible. Through the Ansible tower framework, we now have an easy-to-use GUI; this makes it easy for non-developers to create the environment they want to manage in their DevOps plan without having to constantly work with the command and prompt window. Instead of opening the terminal or command window and writing out complex instructions only in text, we can now use drag and drop and mouse click actions to create appropriate playbooks, inventories, and pushes for our nodes.

Use case by Hootsuite

Let us look at a specific company that is using Ansible today, and in this example, we will look at Hootsuite. It is a social media management system. They can help us manage our pushes for social media content across all of the popular social media platforms that can provide the analytics; they can provide the tools that marketing and sales teams can use to access a sentiment analysis of the messages being pushed out. It is a great tool and very popular. But part of their probability drove a specific problem straight to Hootsuite. The challenge they had at Hootsuite was that they had to go back and rebuild their server environment constantly, and they needed to do this more continuously and be consistent. There was no standard documentation, and they had to rely on our memory to do this consistently. So, Ansible came in and helped the folks over at Hootsuite. Today the DevOps team at Hootsuite writes out playbooks with specific instructions that define the architecture and structure of their hardware nodes and environments. They can do that as a standard product. Instead of it being a problem in scaling up its environment, it can now rebuild and create new servers in a matter of seconds.

The bottom line is that Ansible has provided Hootsuite with It automation and consistent configuration and freed up time from the operations team so that instead of managing servers, they can provide additional new value to the company.

Benefits of Ansible

Let us look at the various benefits of Ansible, which gives this position.

Ansible is agentless, efficient, flexible, simple, idempotent, and automated reporting.

  1. Agentless requires no supporting software or plugin installed on our node or the client system. So, the master has complete control.
  2. Automatically, this means that Ansible is more efficient because now we have more space in our client and node systems for other resources, and we can get Ansible up and running quickly.
  3. Ansible is also flexible, so the infrastructure is prone to change often, and Ansible takes no time to adjust.
  4. Ansible can only get simpler with our playbooks written in a language such as YAML, which is as close to English as possible.
  5. Idempotent means that if we have a playbook that needs to be run "n" number of systems, it would have the same effect on all of these systems without any side effect.
  6. Finally, we have automated reporting, so in the case of Ansible, our playbook has several tasks, and all these stars are named. So, whenever we run or execute our playbook, it reports which tasks ran successfully and which failed, the clients were not reachable, and so on. All this information is crucial when dealing with a large infrastructure.

Ansible Handlers

A handler is the same as a normal task but will run when another task calls. A handler will act when called by an event it listens for. Handlers are meant to perform an extra action when a task changes a managed host, and they should not be used as a replacement for normal tasks.

Handlers are tasks that only run when notified. They are used in cases when we want a task to run only when a change is made on a machine.

Handlers always run in the order specified by the handlers section of the play, and they do not run in the order in which they are listed by notify statements in a task or in the order in which tasks notify them. Handlers normally run after all other tasks in the play are complete. A handler called by a task in the tasks part of the playbook will not run until all tasks under tasks have been processed. Handler names exist in a global namespace; only one will run if two handlers are incorrectly given the same name. Even if more than one task notifies a handler, the handler will only run once. If no tasks notify it, a handler will not run.

The handler is not notified if a task with a notification statement does not report a changed result. The handler is skipped unless another task notifies it.

In Ansible, handlers are typically used to start, reload, restart, and stop services. If our playbook involves changing configuration files, we may need to restart a service so that the changes take effect. In this case, we will need to define a handler for that service and include the notify directive in any tasks that require that service handler.

There are a few other considerations when dealing with handlers:

  1. Handlers will only be run if a task notifies the handler; if a task that would've notified the handlers is skipped due to a when condition or something of the like, the handler will not be run.
  2. Handlers will run once, and only once, at the end of a play. If you need to override this behavior and run handlers in the middle of a playbook, you can use the meta module to do so (e.g., meta: flush_handlers).
  3. If the play fails on a particular host (or all hosts) before handlers are notified, the handlers will never be run. If it's desirable always to run handlers, even after the playbook has failed, you can use the meta module described above as a separate task in the playbook or use the command line flag --force-handlers when running your playbook. Handlers won't run on hosts that became unreachable during the playbook's run.

Handlers:

- name: restart Apache

service: name=apache2 state=restarted

tasks:

- name: Enable Apache rewrite module.

apache2_module: name=rewrite state=present

notify: restart Apache

In some circumstances, we may want to notify multiple handlers or even have handlers notify additional handlers. Both are easy to do with Ansible. To notify multiple handlers from one task, use a list for the notify option:

- name: Rebuild application configuration.

command: /opt/app/rebuild.sh

notify:

- restart Apache

- restart memcached

Add a notify option onto the handler to have one handler notify another. Handlers are glorified tasks that the notify option can call, but since they act as tasks themselves, they can chain themselves to other handlers:

Handlers:

- name: restart Apache

service: name=apache2 state=restarted

notify: restart memcached

- name: restart memcached

service: name=memcached state=restarted

Ansible roles

  1. An Ansible role is a concept that deals with ideas rather than events. It's another level of abstraction used to organize playbooks.
  2. They provide a skeleton for an independent and reusable collection of variables, tasks, templates, files, and modules that can be automatically loaded into the playbook.

Roles are a core concept in Ansible, and they perform such a core function that they even have their repository and accompanying command-line tool. Ansible Galaxy is a website where people can upload roles they have developed for others to use.

When installing a role, we can either install it globally on your machine or locally to a project. As with any dependency, we want it to be local to our project in case multiple projects want different versions of the same dependency. To download a role, we call the ansible-galaxy command, providing the -p (for path ) flag to make it install the role in a folder named roles. We should run the ansible-galaxy command in the same directory where playbook.yml can be found.


Next Topic#





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA