Skip to main content

assert

assert module

Assert that given expressions are true. This module evaluates a list of conditions and fails if any of them are false, making it useful for validation and testing.

Examples

- name: Assert variable is defined
assert:
that:
- my_var is defined
msg: "Variable my_var must be defined"

- name: Multiple assertions
assert:
that:
- ansible_os_family == "RedHat"
- ansible_distribution_major_version >= "7"
msg: "This playbook requires RHEL/CentOS 7 or later"

- name: Assert with complex conditions
assert:
that:
- inventory_hostname in groups['webservers']
- port | int > 0 and port | int < 65536
- config_file is file

- name: Assert service is running
assert:
that:
- service_status.stdout == "running"
msg: "Service must be running before proceeding"

Note: All conditions in the 'that' list must be true for the assertion to pass. If any condition fails, the task will fail with the specified message.

Module Capabilities

  • Has Revert: false
  • Provides Variables: None

Inputs

ParameterTypeDescriptionRequiredDefaultChoices
msgstringCustom message to display when assertion fails. If not provided, a default message will be shown.falseAssertion failed
that[string]List of Jinja2 expressions that must evaluate to true. All expressions must pass for the assertion to succeed.false

Outputs

No outputs.