c05b61777c
* Update windows_setup.rst (#55535): Wrong protocol and port in command. (cherry picked from commit6ea3eca8ff
) * Clarify the two targets of vault encryption, with notes about advantages and drawbacks of each Co-Authored-By: tacatac <taca@kadisius.eu> (cherry picked from commit79198cad7a
) * Improve consistency of loop documentation (#55674) (cherry picked from commita5cb47d697
) * Add Microsoft Document URL for WinRM Memory Hotfix (#55680) Co-Authored-By: hiyokotaisa <thel.vadam2485@gmail.com> (cherry picked from commit7b86208fcd
) * Clarify the documentation for `async` and `poll`; describe the behavior when `poll` = 0 and when it does not. Co-Authored-By: tacatac <taca@kadisius.eu> (cherry picked from commitdbc64ae64c
) * Add security group info and example to AWS guide (#55783): expand documentation on how to use lookup plugin aws_service_ip_ranges with ec2_group module (cherry picked from commitbb5059f2c7
) * correct description of modules vs plugins (#55784) (cherry picked from commit9d5b5d7ddd
) * Fix var naming (#55795): Make vars match tasks in Google Compute guide. (cherry picked from commit943f7334c5
) * Clarifies how Ansible processes multiple `failed_when` conditions (#55941): multiple failed_when conditions join with AND not OR to counter third-party pages online incorrectly stating that it uses `OR`. ([example](https://groups.google.com/d/msg/ansible-project/cIaQTmY3ZLE/c5w8rlmdHWIJ)). (cherry picked from commit5439eb8bd8
) * Docs: edits & expands module_utils & search path info in dev guide (#55931) (cherry picked from commit8542459b95
) * Add faq note about ssh ServerAliveInterval (#55568) (cherry picked from commit76dba7aa4f
) * docsite: correct path, list requirements for testing module docs, etc. (#52008) * dev_guide: correct path, list requirements, etc.; module HTML docs are in '_build/html/module' subdir (cherry picked from commitb14f477bee
) * Developer documentation update involving module invocation (#55747) * Update docs for the 2.7 change to AnsiballZ which invokes modules with one less Python interpreter * Add a section on how module results are returned and on trust between modules, action plugins, and the executor. * Update docs/docsite/rst/dev_guide/developing_program_flow_modules.rst Co-Authored-By: abadger <a.badger@gmail.com> (cherry picked from commitedafa71f42
) * add doc example of multiline failed_when with OR (#56007) * add variety to multiple OR failed_when doc example (cherry picked from commit7d5ada7161
) * Note that by default the regex test is identical to match, but can do much more (#50205) * Note that the regex test behaves like 'match', with default settings (cherry picked from commit86e98c5213
) * more info on how vaults work (#56183) also add warning about what it covers. (cherry picked from commit8ff27c4e0c
) * Fix var naming in GCE guide (cherry picked from commitdae5564e2b
) * dev_guide: Various small updates (#53273) * Document the clarifications that I usually remark when doing reviews * Update docs/docsite/rst/dev_guide/developing_modules_documenting.rst Co-Authored-By: dagwieers <dag@wieers.com> (cherry picked from commiteac7f1fb58
) * Lack of "--update" flag in older Ubuntu distros (#56283): when installing on older Ubuntu distributions be aware of the lack of ``-u`` or ``--update`` flag. (cherry picked from commitdd0b0ae47b
) * should have gone into 52373 (#56306) (cherry picked from commit3c8d8b1509
)
267 lines
7.3 KiB
ReStructuredText
267 lines
7.3 KiB
ReStructuredText
.. _playbooks_tests:
|
|
|
|
Tests
|
|
-----
|
|
|
|
.. contents:: Topics
|
|
|
|
|
|
`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.
|
|
|
|
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 easily create their own.
|
|
|
|
.. _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
|
|
|
|
.. _testing_strings:
|
|
|
|
Testing strings
|
|
```````````````
|
|
|
|
To match strings against a substring or a regular expression, use the "match", "search" or "regex" filters::
|
|
|
|
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/")
|
|
|
|
- debug:
|
|
msg: "matched pattern 4"
|
|
when: url is regex("example.com/\w+/foo")
|
|
|
|
'match' requires zero or more characters at the beginning of the string, while 'search' only requires matching a subset of the string. By default, 'regex' works like `search`, but `regex` can be configured to perform other tests as well.
|
|
|
|
.. _testing_versions:
|
|
|
|
Version Comparison
|
|
``````````````````
|
|
|
|
.. versionadded:: 1.6
|
|
|
|
.. 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 ``version`` test.
|
|
|
|
The ``version`` test can also be used to evaluate the ``ansible_facts['distribution_version']``::
|
|
|
|
{{ ansible_facts['distribution_version'] is version('12.04', '>=') }}
|
|
|
|
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 as defined by ``distutils.version.StrictVersion`` should be used. The default is ``False`` (using ``distutils.version.LooseVersion``), ``True`` enables strict version parsing::
|
|
|
|
{{ sample_version_var is version('1.0', operator='lt', strict=True) }}
|
|
|
|
|
|
.. _math_tests:
|
|
|
|
Set theory tests
|
|
````````````````
|
|
|
|
.. versionadded:: 2.1
|
|
|
|
.. 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'::
|
|
|
|
vars:
|
|
a: [1,2,3,4,5]
|
|
b: [2,3]
|
|
tasks:
|
|
- debug:
|
|
msg: "A includes B"
|
|
when: a is superset(b)
|
|
|
|
- debug:
|
|
msg: "B is included in A"
|
|
when: b is subset(a)
|
|
|
|
.. _contains_test:
|
|
|
|
Test if a list contains a value
|
|
```````````````````````````````
|
|
|
|
.. versionadded:: 2.8
|
|
|
|
Ansible includes a ``contains`` test which operates similarly, but in reverse of the Jinja2 provided ``in`` test.
|
|
This is designed with the ability to allow use of ``contains`` with filters such as ``map`` and ``selectattr``::
|
|
|
|
vars:
|
|
lacp_groups:
|
|
- master: lacp0
|
|
network: 10.65.100.0/24
|
|
gateway: 10.65.100.1
|
|
dns4:
|
|
- 10.65.100.10
|
|
- 10.65.100.11
|
|
interfaces:
|
|
- em1
|
|
- em2
|
|
|
|
- master: lacp1
|
|
network: 10.65.120.0/24
|
|
gateway: 10.65.120.1
|
|
dns4:
|
|
- 10.65.100.10
|
|
- 10.65.100.11
|
|
interfaces:
|
|
- em3
|
|
- em4
|
|
|
|
tasks:
|
|
- debug:
|
|
msg: "{{ (lacp_groups|selectattr('interfaces', 'contains', 'em1')|first).master }}"
|
|
|
|
.. _path_tests:
|
|
|
|
.. 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:
|
|
|
|
- debug:
|
|
msg: "all are true!"
|
|
when: mylist is all
|
|
|
|
- debug:
|
|
msg: "at least one is true"
|
|
when: myotherlist is any
|
|
|
|
|
|
Testing paths
|
|
`````````````
|
|
|
|
.. note:: 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::
|
|
|
|
- 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:
|
|
msg: "path already exists"
|
|
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
|
|
|
|
|
|
.. _test_task_results:
|
|
|
|
Task results
|
|
````````````
|
|
|
|
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
|
|
|
|
- debug:
|
|
msg: "it failed"
|
|
when: result is failed
|
|
|
|
# in most cases you'll want a handler, but if you want to do something right now, this is nice
|
|
- debug:
|
|
msg: "it changed"
|
|
when: result is changed
|
|
|
|
- debug:
|
|
msg: "it succeeded in Ansible >= 2.1"
|
|
when: result is succeeded
|
|
|
|
- debug:
|
|
msg: "it succeeded"
|
|
when: result is success
|
|
|
|
- debug:
|
|
msg: "it was skipped"
|
|
when: result is skipped
|
|
|
|
.. 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
|
|
:doc:`playbooks_reuse_roles`
|
|
Playbook organization by roles
|
|
:doc:`playbooks_best_practices`
|
|
Best practices in playbooks
|
|
`User Mailing List <https://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
|