Skip to main content
In this article, we explore how to use conditionals in Ansible to control task execution based on the operating system and other dynamic factors. Instead of maintaining separate playbooks for different OS distributions, you can consolidate them into a single playbook that intelligently executes tasks based on conditions.

Installing NGINX on Multiple Operating Systems

Different operating systems require different package managers. For example, Debian uses APT while Red Hat uses yum. Traditionally, you might have separate playbooks for each OS, but conditionals allow you to handle both in one file.

Example: Installing NGINX on a Debian System

Use the following playbook snippet to install NGINX on a Debian system using the apt module:

Example: Installing NGINX on Debian and Red Hat Systems

Ansible provides the built-in variable ansible_os_family that lets you determine the OS flavor. This example demonstrates installing NGINX on both Debian and Red Hat systems in a single playbook:
Notice the use of the double equal sign (==) when comparing values of ansible_os_family.
You can combine conditions using logical operators. Use the or operator to execute a task if any condition is true, and and if all conditions must be met.

Example: Combining Conditions

The playbook below shows how to install NGINX on Debian when the OS version is “16.04” and on either Red Hat or SUSE systems:

Using Conditionals in Loops

You can also apply conditionals within loops. Suppose you have an array of packages to install, each with a required property. The following playbook installs only the packages marked as required:
This condition ensures that even though the loop iterates over all items, only the packages with required set to true are installed.

Using Conditionals with Registered Variables

Often, you may want to perform tasks based on the output of previous commands. For example, you might check if a service is down and send an email alert if it is.

Example: Notifying When a Service is Down

The playbook below checks the status of the “httpd” service and registers the output. If the service is found to be down, it sends an email notification:
In this example, the find() method searches for the string “down” in result.stdout. If found (i.e., the return value is not -1), the email alert is triggered.

Conclusion

Using conditionals in Ansible playbooks allows you to create versatile tasks that respond to the specific characteristics of your hosts. By leveraging built-in variables like ansible_os_family and using logical operators, you can streamline your automation workflows and maintain clean, efficient playbooks. Continue exploring and practicing these techniques to build robust Ansible automation strategies that adapt dynamically to your infrastructure’s needs.

Watch Video

Practice Lab