Tests In Ansible

Tests In Ansible

Tests in Ansible

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed functions (filters) in Ansible.

https://cloudaffaire.com/functions-in-ansible/

In this blog post, we will discuss tests in Ansible.

Tests in Ansible:

In the last blog post, we have discussed filters in Ansible. You can extend the functionality of filters by using tests. Tests in Jinja are a way of evaluating template expressions and returning True or False. Jinja2 includes a set of tests and in addition to those Jinja2 tests, Ansible supplies a few more and users can easily create their own.

The main difference between tests and filters are that Jinja tests are used for comparisons, whereas filters are used for data manipulation, and have different applications in jinja. Tests can also be used in list processing filters, like map() and select() to choose items in the list.

Note: Like all templating, tests always execute on the Ansible controller, not on the target of a task, as they test local data.

Next, we are going to explain each test types with a demo.

Tests for variables:

Tests for strings:

Tests for numbers:

Tests for sequences:

Tests for paths:

Tests for tasks:

Hope you have enjoyed this article. We have tried to cover as many tests as possible. But still, if few are left, you can reference below documents

https://docs.ansible.com/ansible/latest/user_guide/playbooks_tests.html

https://jinja.palletsprojects.com/en/2.10.x/templates/#builtin-tests

In the next blog post, we will discuss encryption (Ansible Vault) in Ansible.

To get more details on Ansible, please refer below Ansible documentation.

https://docs.ansible.com/

 

Leave a Reply

Close Menu