ansible when
时间: 2023-11-17 11:04:24 浏览: 67
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.
ansible 的when
Ansible的when语句用于在执行任务之前进行条件判断,根据条件的真假来决定是否执行该任务。当条件为真时,任务会被执行;当条件为假时,任务会被跳过。
以下是两个使用when语句的Ansible任务的例子:
1. 使用自定义参数进行条件判断:
```yaml
- hosts: all
tasks:
- name: "Install package on specific host"
yum:
name: httpd
state: installed
when: ansible_hostname == 'specific_host'
```
2. 根据主机名进行条件判断:
```yaml
- hosts: tomcat
vars:
tomca: 1
tasks:
- name: "Install httpd on nod1"
yum:
name: httpd
state: installed
when: ansible_hostname == 'nod1'
- name: "Install mysql on nod2"
yum:
name: mariadb,mariadb-server
state: installed
when: ansible_hostname == 'nod2'
```
这两个例子展示了如何使用when语句根据不同的条件来执行不同的任务。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)