+

Search Tips   |   Advanced Search

Tests

Tests in Jinja are a way of evaluating template expressions and returning True or False. Jinja ships with many of these. See builtin tests in the official Jinja template documentation.

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.

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

In addition to those Jinja2 tests, Ansible supplies a few more and users can create their own.


Test syntax

Test syntax varies from filter syntax (variable | filter). Historically Ansible has registered tests as both jinja tests and jinja filters, allowing for them to be referenced using filter syntax.

As of Ansible 2.5, using a jinja test as a filter will generate a warning.

The syntax for using a jinja test is as follows:

Such as:


Testing strings

To match strings against a substring or a regular expression, use the match, search or regex tests:

match succeeds if it finds the pattern at the beginning of the string, while search succeeds if it finds the pattern anywhere within string. By default, regex works like search, but regex can be configured to perform other tests as well, by passing the match_type keyword argument. In particular, match_type determines the re method that gets used to perform the search. The full list can be found in the relevant Python documentation here.

All of the string tests also take optional ignorecase and multiline arguments. These correspond to re.I and re.M from Python's re library, respectively.


Vault (vault_encrypted)

New in version 2.10.

You can test whether a variable is an inline single vault encrypted value using the vault_encrypted test.


Testing truthiness (truthy)

As of Ansible 2.10, you can now perform Python like truthy and falsy checks.

Additionally, the truthy and falsy tests accept an optional parameter called convert_bool that will attempt to convert boolean indicators to actual booleans.


Comparing versions

To compare a version number, such as checking if the ansible_facts['distribution_version'] version is greater than or equal to '12.04', you can use the version test.

The version test can also be used to evaluate the ansible_facts['distribution_version']:

If ansible_facts['distribution_version'] is greater than or equal to 12.04, this test returns True, otherwise False.

The version test accepts the following operators:

This test also accepts a 3rd parameter, strict which defines if strict version parsing as defined by distutils.version.StrictVersion should be used. The default is False (using distutils.version.LooseVersion), True enables strict version parsing:

When using version in a playbook or role, don't use {{ }} as described in the FAQ:


Set theory tests

In 2.5 issubset and issuperset were renamed to subset and superset

To see if a list includes or is included by another list, you can use 'subset' and 'superset':


Testing if a list contains a value

Ansible includes a contains test which operates similarly, but in reverse of the Jinja2 provided in test. The contains test is designed to work with the select, reject, selectattr, and rejectattr filters:


Testing if a list value is True

You can use any and all to check if any or all elements in a list are true or not:


Testing paths

In 2.5 the following tests were renamed to remove the is_ prefix

The following tests can provide information about a path on the controller:


Testing size formats

The human_readable and human_to_bytes functions let you test your playbooks to make sure you are using the right size format in your tasks, and that you provide Byte format to computers and human-readable format to people.


Human readable (human_readable)

Asserts whether the given string is human readable or not.

For example:

This would result in:


Human to bytes (human_to_bytes)

Returns the given string in the Bytes format.

For example:

This would result in:


Testing task results

The following tasks are illustrative of the tests meant to check the status of tasks:

From 2.1, you can also use success, failure, change, and skip so that the grammar matches, for those who need to be strict about it.


See also

Intro to playbooks
An introduction to playbooks
Conditionals
Conditional statements in playbooks
Using Variables
All about variables
Loops
Looping in playbooks
Roles
Playbook organization by roles
Tips and tricks
Tips and tricks for playbooks
irc.freenode.net

#ansible IRC chat channel

Next Previous