Explaining of Ansible Filters

Explaining of Ansible Filters
Techiio-author
Written by Nilima PaulFebruary 1, 2022
9 min read
Ansible
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Techiio-author
Nilima Paul

Technology Security Analyst

We will know in this article, what's the Ansible Filters.

Introduction to Ansible Filters

In Ansible, when we really want information control, handling, organizing, and transformation. We have a bunch of channels, jinja2 layout channels, and custom channels made by clients. Behind the scenes, channels in Ansible are from jinja2, so the information is changed inside a layout articulation. Additionally note that the templating occurs on the Ansible regulator hub, not the objective remote hosts. Along these lines, channels get executed on nearby information given by the Ansible regulator. This is useful as thusly, how much information which should be moved to remote hosts is lesser subsequent to handling locally, in the majority of the cases.

Explaining Ansible Filters

Ansible has a rich arrangement of channels supported by jinja2 templating. We input a few information into these formats and the jinja2 layout motor interacts with that information and gives the result likewise. Channels are generally utilized for designing or changing information. Then, at that point, in light of the result, some other assignments are performed. Likewise, separating is exceptionally valuable in investigating. The rundown of inherent channels transported by jinja2 can be checked from the underneath connect, which is an authority interface for jinja2 format creator documentation.

How does Ansible Filters Works?

Ansible has a rich arrangement of channels supported by jinja2 templating. We input a few information into these formats and the jinja2 layout motor interaction that information and give the result likewise. Channels generally utilized for designing or changing the information. Then, at that point, in light of the result some other assignments are performed. Likewise, separating is exceptionally valuable in investigating. The rundown of inherent channels transported by jinja2 can be checked from the underneath connect, which is an authority interface for jinja2 format creator documentation.

{{ <input>| <filter> }}

Examples to Implement Ansible Filters

Presently by utilizing models, we will attempt to find out with regards to a portion of the well known channels, which you may need to use in everyday tasks. We will take a few models, however prior to going there, we initially comprehend our lab utilized for the end goal of testing. Here we have an Ansible control server named ansible-regulator and two controllers has named have one and host-two. We will make playbooks and run Ansible orders on the ansible-regulator hub and deal with the clients on remote hosts.

Example #1

Defaulting vague factors is finished by utilizing channel default, which will appoint the default worth to a variable on the off chance that this isn't characterized. To check this, we can make a playbook with content like underneath, here when we characterized var_1 variable with esteem 5, then, at that point, running it will give us the test_var variable worth equivalent to 5

Code:

---
hosts: localhost vars:
var_1: 5 tasks:
set_fact:
test_var: "{{ var_1 | default(2) }}"
debug:
var: test_var

We will run this playbook as below:

Output:

ansible-playbook filter_mandatory.yaml

blogpost

Example #2

If remove the definition of var_1 variable and make the playbook like below:

Code:

---
hosts: localhost tasks:
set_fact:
test_var: "{{ var_1 | default(2) }}"
debug:
var: test_var

Then executing it will give you the value of the test_var variable equal to 2, which is the default value.

Output:

ansible-playbook filter_mandatory.yaml
blogpost

Example #3

There are some mathematics-related modules as well, like abs, int, pow, root. To check some of these, we can make a playbook with content like below:

Code:

---
hosts: localhost vars:
var_one: -2.5 tasks:
set_fact:
var_abs: "{{ var_one | abs }}" var_pow: "{{ var_one | pow(5) }}"
debug:
msg: Absolute value -2.5 is "{{ var_abs }}"
debug:
msg: Fifth power of -2.5 is "{{ var_pow }}"

Output:

ansible-playbook filter_math.yaml
blogpost

Example #4

If we want to filter a list, we can use a select filter, this is used with a keyword Other filters like the list can be used along with it. To check this, we can make a playbook with content like below:

Code:

---
hosts: all tasks:
set_fact:
interfaces_list: "{{ ansible_interfaces | select('match', '^(eth|wlan)[0-9]+') | list
}}"
debug:
msg: The interface's list on "{{ ansible_hostname }}" is "{{ interfaces_list }}"

Output:

ansible-playbook filter_select_match.yaml
blogpost

Example #5

To convert some string into upper or lower, we can use map To check this, we can make a playbook with content like below:

Code:

---
hosts: localhost vars:
var_one: Test-machine tasks:
debug:
msg: var_one is now {{ var_one | map('upper')| list}}

Output:

ansible-playbook filter_map.yaml
blogpost

Example #6

Some filters can give you the hash of some string or data. To check this, we can make a playbook with content like below:

Code:

---
hosts: localhost vars:
var_one: apple tasks:
debug:
msg: Variable value is "{{ var_one | hash('sha1') }}"

Output:

ansible-playbook filter_hash.yaml

Conclusion

Channels are significant when you are troubleshooting a few issues with the playbook or you want to change a few information prior to utilizing it on distant servers. Rich arrangement of default channels and the capacity to make one for your necessities makes the extent of ease of use sufficiently wide.

Ansible
Ansible Filters
Linux
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-author
Nilima Paul
Technology Security Analyst
Techiio-followerTechiio-followerTechiio-follower
201 Blog Posts
0 Discussion Threads
Trending Technologies
15
Software40
DevOps46
Frontend Development24
Backend Development20
Server Administration17
Linux Administration26
Data Center24
Sentry24
Terraform23
Ansible83
Docker70
Penetration Testing16
Kubernetes21
NGINX20
JenkinsX17
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance