In a playbook, you may want to execute different tasks, or have different goals, depending on the value of a fact (data about the remote system), a variable, or the result of a previous task. You may want the value of some variables to depend on the value of other variables. Or you may want to create additional groups of hosts based on whether the hosts match other criteria. You can do all of these things with conditionals. Ansible uses Jinja2 tests and filters in conditionals. Ansible supports all the standard tests and filters and adds some unique ones as well.
Basic conditionals with when
The simplest conditional statement applies to a single task. Create the task, then add a when statement that applies a test. The when the clause is a raw Jinja2 expression without double curly braces (see group_by – Create Ansible groups based on facts). When you run the task or playbook, Ansible evaluates the test for all hosts. On any host where the test passes (returns a value of True), Ansible runs that task. For example, if you are installing mysql on multiple machines, some of which have SELinux enabled, you might have a task to configure SELinux to allow mysql to run. You would only want that task to run on machines that have SELinux enabled:
Conditionals based on ansible_facts
Often you want to execute or skip a task based on facts. Facts are attributes of individual hosts, including IP address, operating system, the status of a file system, and many more. With conditionals based on facts:
You can install a certain package only when the operating system is a particular version.
You can skip configuring a firewall on hosts with internal IP addresses.
You can perform clean-up tasks only when a file system is getting full.
Conditions based on registered variables
Often in a playbook, you want to execute or skip a task based on the outcome of an earlier task. For example, you might want to configure a service after it is upgraded by an earlier task. To create a conditional based on a registered variable:
Register the outcome of the earlier task as a variable.
Create a conditional test based on the registered variable.
Conditionals with roles
There are three ways to apply conditions to roles:
- Add the same condition or conditions to all tasks in the role by placing your when statement under the roles keyword.
- Add the same condition or conditions to all tasks in the role by placing your when statement on a static import_roles in your playbook.
Add a condition or conditions to individual tasks or blocks within the role itself. This is the only approach that allows you to select or skip some tasks within the role based on your when statement. To select or skip tasks within the role, you must have conditions set on individual tasks or blocks, use the dynamic include_role in your playbook, and add the condition or conditions to the include. When you use this approach, Ansible applies the condition to the include itself plus any tasks in the role that also have that when statement.
Example:
when: ansible_os_family == “RedHat”
TASK BY TRAINER:
By using when condition statement install the package “httpd” with custom webpage showing messege “This is Ansible workshop in Unnati ” when ansible_os_family is “RedHat”.