- Network Automation Cookbook
- Karim Okasha
- 157字
- 2025-04-04 13:10:13
How it works...
Ansible uses the when statement to provide conditional execution for the tasks. The when statement is applied at the task level and if the condition in the when statement evaluates to true, the task is executed for the given host. If false, the task is skipped for this host. The output as a result of running the preceding playbook is shown here:
The when statement can take a single condition as seen in the first task, or can take a list of conditions as seen in the second task. If when is a list of conditions, all the conditions need to be true in order for the task to be executed.
In the first task, the when statement is enclosed in "" since the statement starts with a string . However, in the second statement, we use a normal when statement with no "" since the when statement starts with a variable name.