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 filesystem, and many more. With conditionals based on facts:
SeeCommonly-used factsfor a list of facts that frequently appear in conditional statements. Not all facts exist for all hosts. For example, the ‘lsb_major_release’ fact used in the example below only exists when thelsb_releasepackageis installed on the target host. To see what facts are available on your systems, add a debug task to your playbook:
Here is a sample conditional based on a fact:
If you have multiple conditions, you can group them with parentheses:
You can uselogical operatorsto combine conditions. When you have multiple conditions that all need to be true (that is, a logicaland), you can specify them as a list:
If a fact or variable is a string, and you need to run a mathematical comparison on it, use a filter to ensure that Ansible reads the value as an integer: