ansible failed_when
时间: 2023-11-20 08:07:47 浏览: 37
`failed_when` 是 Ansible 中的一个关键字,用于在任务执行失败时控制任务的行为。可以使用 `failed_when` 来定义一个条件,当这个条件成立时,任务会被标记为失败。例如:
```
- name: Check if file exists
stat:
path: /path/to/file
register: file_stat
failed_when: file_stat.stat.exists == False
```
在上面的例子中,如果文件不存在,任务会被标记为失败。
相关问题
- name: run rping ipv4 cli_command: command: ping {{ vrf }} {{ item }} -c 1 register: pingresult4 ignore_errors: True failed_when: "'100% packet loss' not in pingresult4.stdout" loop: "{{ dest.split(',') }}" when: params is not defined and item | ipv4
这是一个使用 Ansible 进行网络设备管理的 playbook 示例。该 playbook 中包含了一个名为 run rping ipv4 的任务,该任务使用 cli_command 模块执行 ping 命令,并将执行结果存储在 pingresult4 变量中。
具体而言,该任务使用了 loop 参数来循环执行 ping 命令,其中 dest 变量使用 split 方法将多个 IP 地址拆分为列表,并逐个执行 ping 命令。同时,使用了 register 参数将 ping 命令的执行结果存储在 pingresult4 变量中,以便后续处理和分析。
在 ping 命令执行过程中,使用了 ignore_errors 参数来忽略 ping 命令执行时可能遇到的错误。同时,使用了 failed_when 参数来判断 ping 命令执行结果中是否包含 "100% packet loss" 字符串,如果不包含则认为 ping 命令执行成功,否则认为 ping 命令执行失败。
最后,在 when 参数中使用了条件判断,当 params 未定义且 item 为 ipv4 地址时才执行该任务,以避免在不需要执行时浪费资源。
该 playbook 的目的是测试多个 IP 地址的连通性,并为网络管理员提供一个快速诊断和定位故障的工具。通过执行 ping 命令,管理员可以了解网络设备的可达性和响应时间,并及时进行故障排查和修复。
ansible when: lineinfile.faild
`lineinfile.failed` is a boolean value that indicates whether the `lineinfile` module failed or not. You can use the `when` keyword to conditionally execute a task based on the value of this variable.
For example, if you want to run a task only if the `lineinfile` module failed, you can use the following syntax:
```
- name: Example task
some_module:
some_parameter: some_value
when: lineinfile.failed
```
This will only run the `Example task` if the `lineinfile` module failed.