ansible playbook when
时间: 2023-08-13 09:08:00 浏览: 118
Ansible之Playbook使用
`ansible playbook when` is a conditional statement used in Ansible playbooks to define when a particular task or role should be executed. It allows you to specify conditions based on variables or facts gathered from the target hosts.
Here's an example of how `when` can be used in an Ansible playbook:
```yaml
- name: Example playbook
hosts: web_servers
tasks:
- name: Install Apache web server
package:
name: apache2
state: present
when: ansible_distribution == "Ubuntu"
```
In this example, the task to install Apache web server will only be executed if the target host's distribution is Ubuntu. The condition `ansible_distribution == "Ubuntu"` is evaluated, and if it's true, the task is executed; otherwise, it is skipped.
You can use various operators and comparison expressions in the `when` statement to define more complex conditions. Ansible provides a rich set of built-in variables and facts that can be used for condition evaluation.
阅读全文