ansible when
时间: 2023-09-26 17:04:36 浏览: 46
在Ansible中,"when"语句用于条件判断,只有当满足特定条件时,才会执行相应的任务。可以使用逻辑与("and")和逻辑或("or")来组合多个条件。当使用"when"语句时,不需要将变量用{{ }}括起来。
在Ansible Playbook中,除了条件判断,还可以像其他编程语言一样进行循环和其他流程控制操作,同时还可以控制任务的执行结果。
总结起来,Ansible中的"when"语句可以用于条件判断,并且可以进行逻辑组合,而在Ansible Playbook中,还可以进行更多的流程控制和任务结果控制操作。
相关问题
ansible when
Ansible中的when语句用于在任务执行之前判断条件是否成立,只有条件成立时才会执行任务。when语句可以使用逻辑运算符and、or和not来组合多个条件,也可以使用括号来改变条件的优先级。以下是几个when语句的示例:
1. 逻辑与
```yaml
when: ansible_distribution == "CentOS" and ansible_distribution_major_version == "7"
```
2. 逻辑或
```yaml
when: ansible_distribution == "RedHat" or ansible_distribution == "Fedora"
```
3. 组合使用
```yaml
when: ( ansible_distribution == "RedHat" and ansible_distribution_major_version == "7" ) or ( ansible_distribution == "Fedora" and ansible_distribution_major_version == "28" )
```
4. not运算符
```yaml
when: not ansible_distribution == "Ubuntu"
```
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.