So in many cases, you may need to do some complex
transformation or even fetch data externally.
So while Ansible is not a full data processing manipulation tool,
you can use existing Jinja2 templating in
conjunction with the many Ansible filters,
lookups, and tests to carry out very complex operations.
For example, we have the lookup plugin. It's mainly used to
query external data for Ansible. Then we have filters in
Ansible that are a form of Jinja2 and are used to transform
data inside a template expression.
So the Jinja2 ships with many different types of filters.
So filters process the value of a variable on the control host
before further using it in the playbook.
So because templating happens on the Ansible controller and
not on the target host, filters execute on the controller
and transform data locally.
So filters can be used to change the way variables are interpreted.
Filters allow variables to be processed before using them in Ansible. So
we have a variable that is treated with a filter,
so we are kind of applying a filter to a variable.
So you may be thinking, what can filters be used for? Well, we can use
filters to manipulate data and carry out a range of tasks. Filters let
you transform JSON data into YAML data, spit a URL to extract the hostname,
get the SHA‑1 hash of a string, and many other tasks.
So we have filters that are built into Jinja2 filtering, and also
Ansible has its own filters to augment the Jinja2 filters.
So you can use Ansible‑specific filters here to manipulate your data
or use any of the standard filters shipped with Jinja2. You can also
use Python methods to transform your data.
So we also have filters that help us work with network‑related
information, for example, the ipaddr that I will demo in just a
moment. As you will see with this filter,
it allows you to extract specific network‑related information.
Plugins are a piece of code that kind of augment Ansible's core functionality.
We have both default plugins and also custom plugins. So to use plugins,
you need to activate them in the ansible.cfg file.
So plugins are another way of extending or, let's say, modifying the core
functionality of Ansible. So plugins are a piece of code that augment
Ansible's core functionality, and Ansible ships with several handy plugins,
and if you want, you can write your own. We have, for example, the filter
plugin. This is used to modify text. Then we have the cache plugin. That
allows the caching of facts.
We also have various inventory plugins, and they allow
different types of dynamic inventories.
We also have the popular lookup plugin. It's a very wide range of plugin that
allows fetching of data externally. We also have the callback plugin that's
quite popular. These are used to collect usage information about Ansible. Now
let us look at the lookup plugin. So more than likely, all of your
configuration information will not be stored as Ansible variables. And you
know now that there are various places that we can store these variables. We
have, for example, the vars section in the play. We can have files loaded from
var files, and we also have hosting group vars, but more than often, there
will be pieces of your configuration data that will live somewhere else.
And here we can use the lookup feature that allows you to read
configuration data from various sources and then use that data
in your playbooks and templates.
We have the lookup plugin, which is probably the most important plugin category.
Lookup plugins are created to use data from external
sources like files and environment variables.
So lookup plugins retrieve data from outside, and this may include files,
databases, key value stores,
API, and many other services. So keep in mind that all of
the Ansible lookup plugins execute on the control machines
and not on the remote hosts.
So we have two main ways we can call these lookup plugins. We have lookup and
also query. So with lookup, it is used to read file contents into variables, so
it will return a string in comma‑separated items.
Then, on the other hand, we have the query argument. That
returns an actual YAML list of items.
The query is much easier, as Ansible does recommend YAML syntax. So callback
plugins enable Ansible to behave and respond to certain events,
So callback plugins extend Ansible by modifying how it responds to events.
So callbacks are placed in Ansible execution that could be
plugged in for additional functionality.
There are expected callback points that can be registered
against to trigger custom actions at those points.
Callback plugins are popular, for example,
sending a Slack message or writing an entry to a remote logging server.