ansible.builtin.assert 模块 – 断言给定的表达式为真
注意
此模块是 ansible-core
的一部分,并包含在所有 Ansible 安装中。在大多数情况下,即使不指定 collections 关键字,您也可以使用简短的模块名称 assert
。但是,我们建议您使用完全限定的集合名称 (FQCN) ansible.builtin.assert
,以便轻松链接到模块文档并避免与可能具有相同模块名称的其他集合冲突。
概要
此模块断言给定的表达式为真,并带有可选的自定义消息。
Windows 目标也支持此模块。
注意
此模块具有相应的 action 插件。
参数
参数 |
注释 |
---|---|
用于断言失败的自定义消息。 此参数在 Ansible 2.7 之前称为“msg”,现在已重命名为“fail_msg”,别名为“msg”。 |
|
将其设置为 选项
|
|
用于成功断言的自定义消息。 |
|
可以传递给“when”语句的相同形式的字符串表达式列表。 |
属性
属性 |
支持 |
描述 |
---|---|---|
支持: 完全 |
表示此项具有相应的 action 插件,因此部分选项可以在控制器上执行 |
|
支持: 无 |
支持与 |
|
支持: 无 |
可以与 become 关键字一起使用 |
|
支持: 无 |
强制执行不按主机执行的“全局”任务,这会绕过按主机模板化以及串行、节流和其他循环注意事项 条件语句的工作方式与使用 此操作在锁步策略之外无法正常工作 |
|
支持: 完全 |
可以在 check_mode 中运行,并返回更改状态预测,而无需修改目标,如果不支持,则会跳过该操作。 |
|
支持: 无 |
使用目标配置的连接信息在其上执行代码 |
|
支持: 无 除了 |
可以与 delegate_to 及相关关键字一起使用 |
|
支持: 无 |
在差异模式下,将返回有关已更改内容(或在 check_mode 中可能需要更改的内容)的详细信息 |
|
平台: 全部 |
可以操作的目标操作系统/系列 |
另请参阅
另请参阅
- 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