Ansible

How Do Ansible Filters Work?

One of the primary parts of any framework or management software is the way it handles and processes data. Considering the direction in which the world of information technology is going, it is fair to say that data is king. Huge amounts of data are being generated daily to be used for different purposes. Thus, data management and processing  very important.

Software such as Ansible is required to handle huge amounts of data. Looking at the facilitations these types of applications are supposed to provide, their data processing capabilities must be top-notch.

Whenever we need to process, format, or manipulate data in Ansible, we use filters. There are two sets of filters available in Ansible. One set is the jinja2 template filters and the other set is of the custom-built filters made by users.

However, in the background, all the filters are derived from the jinja2 template, which works in a way that the data being input into the filters is first converted into template expressions so the filters can be applied to them.

Another important point to take note of is that all the filters are applied locally, meaning that no filtering can be done using remote hosts. So, all the manipulation and processing take place on the Ansible control machine.

If you are looking to learn about the filters available in Ansible and how they are used, you have found the right article as we will be guiding you on how these filters work and how you can create Ansible playbooks to process data the way you want your system to.

How Do Filters work?

The working behind data filters is simple. You give pre-developed filters some data or information as input; the filter analyzes the data according to the commands given in the playbook and the conditions you have set. After the data is analyzed, you will receive the customized output you wanted from the data you had.

Different kinds of filters perform different kinds of filtering on the data. As previously mentioned, all of them work based on the jinja2 template and there is a list of all the filters available. You can see that list by clicking on this link.

There are numerous advantages of using filters as they greatly enhance the capabilities of Ansible. You can also use filters for debugging in certain cases.

How to Implement Filters in Ansible?

The basic syntax of any Ansible filter is given by:

{{<input> | <filter>}}

Another important point to take note of is that the data transformation in the background is done using Python. In case a certain type of template is not available with Python, the filter may not give you the correct output. So, you need to be careful while using some filters.

Till now you have learnt the basics of Ansible filters. To clarify any further queries, let’s take a look at some examples of filtering and different types of filters.

Examples

- name: Generate multiple arbitrary hostnames
  ansible.builtin.debug:
    msg: "{{ ['abc', 'xyz'] | product(['com']) | map('join', '.') | join(',') }}"

Also, we’d like to mention here that to run a playbook, the following command would have to be executed using the Linux terminal:

ansible-playbook testbook.yml

In this example, we have used the “product” filter. Product filter is used for implementing cartesian multiplication. The “product” filter is somewhat an analogue of using for loops in traditional programming languages. The output you will receive by running such a code would be.

{ "msg": "abc.com,xyz.com" }

Here’s how you can get combinations for sets of two numbers.

- name: Give me combinations for sets of two
  ansible.builtin.debug:
    msg: "{{ [10,20,30,40,50] | ansible.builtin.combinations(2) | list }}"

We have used the “combination” filter to get combinations of two numbers from a given set. The 2 specified in brackets instructs to make combinations of 2 and the “list” keyword will give you the output in the form of a list.

- name: Largest permutations (order matters)
      ansible.builtin.debug:
           msg: "{{ [0,2,4,6,8] | ansible.builtin.permutations | list }}"
- name: Permutations of sets of three
      ansible.builtin.debug:
           msg: "{{ [1,3,5,7,9] | ansible.builtin.permutations(3) | list }}"

There is the provision of a “permutation” filter as well. This filter gives us permutations of a given set of variables or data. In the above example, we will receive the largest permutations from the even number set and permutations of sets of three from the odd numbers.

"{{ ['a','b','c'] | random }}"

This example uses the “random” filter to generate a random number from a given set of data.

{{ 51 | random(step=10) }}

This example also used the “random” filter to generate a random number between 0 and 50. We have also given the condition that the step size should be 10 so, the output you will get would be a number which is a multiple of 10.

{{ ['a','b','c',’d’,’e’] | shuffle }}

The “shuffle” filter can be used to shuffle the data set to produce an output in which the given will be shuffled and then shown to you.

{{ [{'val': 1}, {'val': 2},{‘val’:3},{‘val’:4}] | min(attribute='val') }}

Finally, we have used the “min” filter that will compare the two input numbers that we have given as input and give you the smallest number as output.

{{ [{'val': 1}, {'val': 2}] | max(attribute='val') }}

Similarly, here we use the “max” filter to get the maximum number from a given dataset.

So, these were the examples of some of the filters available in Ansible. There are many other filters using which you can process and format data.

Conclusion

In this article, we looked at Ansible filters. We established why we need filters in data analysis and looked at some filters provided by the plug-in. We hope that we were able to help you in understanding why filters are important and more importantly, you now understand how you can use them for your benefit. Feel free to drop any questions in the comments section.

About the author

Zeeman Memon

Hi there! I'm a Software Engineer who loves to write about tech. You can reach out to me on LinkedIn.