ansible.builtin.assert 模块 – 断言给定的表达式为真

注意

此模块是 ansible-core 的一部分,并包含在所有 Ansible 安装中。在大多数情况下,即使不指定 collections 关键字,您也可以使用简短的模块名称 assert。但是,我们建议您使用完全限定的集合名称 (FQCN) ansible.builtin.assert,以便轻松链接到模块文档并避免与可能具有相同模块名称的其他集合冲突。

概要

  • 此模块断言给定的表达式为真,并带有可选的自定义消息。

  • Windows 目标也支持此模块。

注意

此模块具有相应的 action 插件

参数

参数

注释

fail_msg

别名: msg

字符串

在 Ansible 2.7 中添加

用于断言失败的自定义消息。

此参数在 Ansible 2.7 之前称为“msg”,现在已重命名为“fail_msg”,别名为“msg”。

quiet

布尔值

在 Ansible 2.8 中添加

将其设置为 true 以避免冗长的输出。

选项

  • false ← (默认)

  • true

success_msg

字符串

在 Ansible 2.7 中添加

用于成功断言的自定义消息。

that

列表 / 元素=字符串 / 必需

可以传递给“when”语句的相同形式的字符串表达式列表。

属性

属性

支持

描述

action

支持: 完全

表示此项具有相应的 action 插件,因此部分选项可以在控制器上执行

async

支持:

支持与 async 关键字一起使用

become

支持:

可以与 become 关键字一起使用

bypass_host_loop

支持:

强制执行不按主机执行的“全局”任务,这会绕过按主机模板化以及串行、节流和其他循环注意事项

条件语句的工作方式与使用 run_once 时相同,使用的变量将来自第一个可用的主机

此操作在锁步策略之外无法正常工作

check_mode

支持: 完全

可以在 check_mode 中运行,并返回更改状态预测,而无需修改目标,如果不支持,则会跳过该操作。

connection

支持:

使用目标配置的连接信息在其上执行代码

delegation

支持:

除了 register 和/或与 delegate_facts 结合使用外,几乎没有效果。

可以与 delegate_to 及相关关键字一起使用

diff_mode

支持:

在差异模式下,将返回有关已更改内容(或在 check_mode 中可能需要更改的内容)的详细信息

platform

平台: 全部

可以操作的目标操作系统/系列

另请参阅

另请参阅

ansible.builtin.debug

在执行期间打印语句。

ansible.builtin.fail

使用自定义消息失败。

ansible.builtin.meta

执行 Ansible “操作”。

示例

- name: A single condition can be supplied as string instead of list
  ansible.builtin.assert:
    that: "ansible_os_family != 'RedHat'"

- name: Use yaml multiline strings to ease escaping
  ansible.builtin.assert:
    that:
      - "'foo' in some_command_result.stdout"
      - number_of_the_counting == 3
      - >
        "reject" not in some_command_result.stderr

- name: After version 2.7 both 'msg' and 'fail_msg' can customize failing assertion message
  ansible.builtin.assert:
    that:
      - my_param <= 100
      - my_param >= 0
    fail_msg: "'my_param' must be between 0 and 100"
    success_msg: "'my_param' is between 0 and 100"

- name: Please use 'msg' when ansible version is smaller than 2.7
  ansible.builtin.assert:
    that:
      - my_param <= 100
      - my_param >= 0
    msg: "'my_param' must be between 0 and 100"

- name: Use quiet to avoid verbose output
  ansible.builtin.assert:
    that:
      - my_param <= 100
      - my_param >= 0
    quiet: true

作者

  • Ansible 核心团队

  • Michael DeHaan