mirror of https://github.com/ansible/ansible.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
231 lines
6.1 KiB
ReStructuredText
231 lines
6.1 KiB
ReStructuredText
8 years ago
|
Tests
|
||
|
-----
|
||
8 years ago
|
|
||
|
.. contents:: Topics
|
||
|
|
||
|
|
||
7 years ago
|
`Tests <http://jinja.pocoo.org/docs/dev/templates/#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.
|
||
|
|
||
7 years ago
|
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.
|
||
8 years ago
|
|
||
8 years ago
|
Like all templating, tests always execute on the Ansible controller, **not** on the target of a task, as they test local data.
|
||
8 years ago
|
|
||
|
In addition to those Jinja2 tests, Ansible supplies a few more and users can easily create their own.
|
||
|
|
||
7 years ago
|
.. _test_syntax:
|
||
|
|
||
|
Test syntax
|
||
|
```````````
|
||
|
|
||
|
`Test syntax <http://jinja.pocoo.org/docs/dev/templates/#tests>`_ varies from `filter syntax <http://jinja.pocoo.org/docs/dev/templates/#filters>`_ (``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::
|
||
|
|
||
|
variable is test_name
|
||
|
|
||
|
Such as::
|
||
|
|
||
|
result is failed
|
||
|
|
||
8 years ago
|
.. _testing_strings:
|
||
|
|
||
|
Testing strings
|
||
8 years ago
|
```````````````
|
||
8 years ago
|
|
||
|
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:
|
||
7 years ago
|
- debug:
|
||
|
msg: "matched pattern 1"
|
||
7 years ago
|
when: url is match("http://example.com/users/.*/resources/.*")
|
||
8 years ago
|
|
||
7 years ago
|
- debug:
|
||
|
msg: "matched pattern 2"
|
||
7 years ago
|
when: url is search("/users/.*/resources/.*")
|
||
8 years ago
|
|
||
7 years ago
|
- debug:
|
||
|
msg: "matched pattern 3"
|
||
7 years ago
|
when: url is search("/users/")
|
||
8 years ago
|
|
||
|
'match' requires a complete match in the string, while 'search' only requires matching a subset of the string.
|
||
|
|
||
|
|
||
|
.. _testing_versions:
|
||
|
|
||
|
Version Comparison
|
||
8 years ago
|
``````````````````
|
||
8 years ago
|
|
||
|
.. versionadded:: 1.6
|
||
|
|
||
7 years ago
|
.. note:: In 2.5 ``version_compare`` was renamed to ``version``
|
||
|
|
||
8 years ago
|
To compare a version number, such as checking if the ``ansible_distribution_version``
|
||
7 years ago
|
version is greater than or equal to '12.04', you can use the ``version`` test.
|
||
8 years ago
|
|
||
7 years ago
|
The ``version`` test can also be used to evaluate the ``ansible_distribution_version``::
|
||
8 years ago
|
|
||
7 years ago
|
{{ ansible_distribution_version is version('12.04', '>=') }}
|
||
8 years ago
|
|
||
7 years ago
|
If ``ansible_distribution_version`` is greater than or equal to 12, this test returns True, otherwise False.
|
||
8 years ago
|
|
||
7 years ago
|
The ``version`` test accepts the following operators::
|
||
8 years ago
|
|
||
|
<, lt, <=, le, >, gt, >=, ge, ==, =, eq, !=, <>, ne
|
||
|
|
||
|
This test also accepts a 3rd parameter, ``strict`` which defines if strict version parsing should
|
||
|
be used. The default is ``False``, but this setting as ``True`` uses more strict version parsing::
|
||
|
|
||
7 years ago
|
{{ sample_version_var is version('1.0', operator='lt', strict=True) }}
|
||
8 years ago
|
|
||
|
|
||
|
.. _math_tests:
|
||
|
|
||
|
Group theory tests
|
||
8 years ago
|
``````````````````
|
||
8 years ago
|
|
||
8 years ago
|
.. versionadded:: 2.1
|
||
|
|
||
7 years ago
|
.. note:: 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'::
|
||
8 years ago
|
|
||
|
vars:
|
||
|
a: [1,2,3,4,5]
|
||
|
b: [2,3]
|
||
|
tasks:
|
||
7 years ago
|
- debug:
|
||
|
msg: "A includes B"
|
||
7 years ago
|
when: a is superset(b)
|
||
8 years ago
|
|
||
7 years ago
|
- debug:
|
||
|
msg: "B is included in A"
|
||
7 years ago
|
when: b is subset(a)
|
||
8 years ago
|
|
||
|
|
||
|
.. _path_tests:
|
||
|
|
||
8 years ago
|
.. versionadded:: 2.4
|
||
|
|
||
|
You can use `any` and `all` to check if any or all elements in a list are true or not::
|
||
|
|
||
|
vars:
|
||
|
mylist:
|
||
|
- 1
|
||
|
- 3 == 3
|
||
|
- True
|
||
|
myotherlist:
|
||
|
- False
|
||
|
- True
|
||
|
tasks:
|
||
|
|
||
7 years ago
|
- debug:
|
||
|
msg: "all are true!"
|
||
8 years ago
|
when: mylist is all
|
||
|
|
||
7 years ago
|
- debug:
|
||
|
msg: "at least one is true"
|
||
7 years ago
|
when: myotherlist is any
|
||
8 years ago
|
|
||
|
|
||
8 years ago
|
Testing paths
|
||
8 years ago
|
`````````````
|
||
8 years ago
|
|
||
7 years ago
|
.. note:: In 2.5 the follwing tests were renamed to remove the ``is_`` prefix
|
||
|
|
||
8 years ago
|
The following tests can provide information about a path on the controller::
|
||
|
|
||
7 years ago
|
- debug:
|
||
|
msg: "path is a directory"
|
||
7 years ago
|
when: mypath is directory
|
||
8 years ago
|
|
||
7 years ago
|
- debug:
|
||
|
msg: "path is a file"
|
||
7 years ago
|
when: mypath is file
|
||
8 years ago
|
|
||
7 years ago
|
- debug:
|
||
|
msg: "path is a symlink"
|
||
7 years ago
|
when: mypath is link
|
||
8 years ago
|
|
||
7 years ago
|
- debug:
|
||
|
msg: "path already exists"
|
||
7 years ago
|
when: mypath is exists
|
||
8 years ago
|
|
||
7 years ago
|
- debug:
|
||
|
msg: "path is {{ (mypath is abs)|ternary('absolute','relative')}}"
|
||
8 years ago
|
|
||
7 years ago
|
- debug:
|
||
|
msg: "path is the same file as path2"
|
||
7 years ago
|
when: mypath is same_file(path2)
|
||
8 years ago
|
|
||
7 years ago
|
- debug:
|
||
|
msg: "path is a mount"
|
||
7 years ago
|
when: mypath is mount
|
||
8 years ago
|
|
||
|
|
||
|
.. _test_task_results:
|
||
|
|
||
|
Task results
|
||
8 years ago
|
````````````
|
||
8 years ago
|
|
||
|
The following tasks are illustrative of the tests meant to check the status of tasks::
|
||
|
|
||
|
tasks:
|
||
|
|
||
|
- shell: /usr/bin/foo
|
||
|
register: result
|
||
|
ignore_errors: True
|
||
|
|
||
7 years ago
|
- debug:
|
||
|
msg: "it failed"
|
||
7 years ago
|
when: result is failed
|
||
8 years ago
|
|
||
|
# in most cases you'll want a handler, but if you want to do something right now, this is nice
|
||
7 years ago
|
- debug:
|
||
|
msg: "it changed"
|
||
7 years ago
|
when: result is changed
|
||
8 years ago
|
|
||
7 years ago
|
- debug:
|
||
|
msg: "it succeeded in Ansible >= 2.1"
|
||
7 years ago
|
when: result is succeeded
|
||
8 years ago
|
|
||
7 years ago
|
- debug:
|
||
|
msg: "it succeeded"
|
||
7 years ago
|
when: result is success
|
||
8 years ago
|
|
||
7 years ago
|
- debug:
|
||
|
msg: "it was skipped"
|
||
7 years ago
|
when: result is skipped
|
||
8 years ago
|
|
||
|
.. 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.
|
||
|
|
||
|
|
||
|
|
||
|
.. _builtin tests: http://jinja.pocoo.org/docs/templates/#builtin-tests
|
||
|
|
||
|
.. seealso::
|
||
|
|
||
|
:doc:`playbooks`
|
||
|
An introduction to playbooks
|
||
|
:doc:`playbooks_conditionals`
|
||
|
Conditional statements in playbooks
|
||
|
:doc:`playbooks_variables`
|
||
|
All about variables
|
||
|
:doc:`playbooks_loops`
|
||
|
Looping in playbooks
|
||
8 years ago
|
:doc:`playbooks_reuse_roles`
|
||
8 years ago
|
Playbook organization by roles
|
||
|
:doc:`playbooks_best_practices`
|
||
|
Best practices in playbooks
|
||
|
`User Mailing List <http://groups.google.com/group/ansible-devel>`_
|
||
|
Have a question? Stop by the google group!
|
||
|
`irc.freenode.net <http://irc.freenode.net>`_
|
||
|
#ansible IRC chat channel
|
||
|
|
||
|
|