Tests
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 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 easily create their own.
Test syntax varies from (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:
- result is failed
Testing strings
To match strings against a substring or a regex, use the “match” or “search” filter:
- vars:
- url: "http://example.com/users/foo/resources/bar"
- tasks:
- - debug:
- msg: "matched pattern 1"
- when: url is match("http://example.com/users/.*/resources/.*")
- - debug:
- msg: "matched pattern 2"
- when: url is search("/users/.*/resources/.*")
- - debug:
- msg: "matched pattern 3"
- when: url is search("/users/")
‘match’ requires a complete match in the string, while ‘search’ only requires matching a subset of the string.
Note
In 2.5 version_compare
was renamed to version
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 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:
- <, lt, <=, le, >, gt, >=, ge, ==, =, eq, !=, <>, ne
This test also accepts a 3rd parameter, strict
which defines if strict version parsing shouldbe used. The default is , but this setting as True
uses more strict version parsing:
- {{ sample_version_var is version('1.0', operator='lt', strict=True) }}
Set theory tests
New in version 2.1.
Note
To see if a list includes or is included by another list, you can use ‘subset’ and ‘superset’:
New in version 2.4.
You can use any and all to check if any or all elements in a list are true or not:
- vars:
- mylist:
- - "{{ 3 == 3 }}"
- - True
- myotherlist:
- - False
- - True
- tasks:
- - debug:
- msg: "all are true!"
- when: mylist is all
- - debug:
- msg: "at least one is true"
- when: myotherlist is any
Note
In 2.5 the following tests were renamed to remove the prefix
The following tests can provide information about a path on the controller:
- - debug:
- msg: "path is a directory"
- when: mypath is directory
- - debug:
- msg: "path is a file"
- when: mypath is file
- - debug:
- msg: "path is a symlink"
- when: mypath is link
- - debug:
- when: mypath is exists
- - debug:
- msg: "path is {{ (mypath is abs)|ternary('absolute','relative')}}"
- - debug:
- msg: "path is the same file as path2"
- when: mypath is same_file(path2)
- - debug:
- msg: "path is a mount"
- when: mypath is mount
Task results
The following tasks are illustrative of the tests meant to check the status of tasks:
Note
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.
- Working With Playbooks
- An introduction to playbooks
- Conditional statements in playbooks
- Using Variables
- All about variables
- Looping in playbooks
- Roles
- Playbook organization by roles
- Best practices in playbooks
- User Mailing List
- Have a question? Stop by the google group!