Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Conditionals in Playbooks

Introduction to Conditionals in Playbooks

Conditionals in Ansible playbooks allow you to control the flow of tasks based on specific conditions. This can be particularly useful when you need to perform different actions depending on the state of your system or the values of variables.

Basic Conditional Syntax

The basic syntax for using conditionals in a playbook is to use the when directive. The when directive is followed by a Jinja2 expression that evaluates to either True or False. If the expression evaluates to True, the task is executed. If it evaluates to False, the task is skipped.

Here's a simple example:

- name: Install Apache
  apt:
    name: apache2
    state: present
  when: ansible_os_family == "Debian"
                

In this example, the task to install Apache will only run if the OS family is Debian.

Using Variables in Conditionals

Conditionals can also use variables. This allows for more dynamic and flexible playbooks. You can define variables in your inventory files, playbooks, or even gather facts using the setup module.

Example with a variable:

- name: Install Nginx
  apt:
    name: nginx
    state: present
  when: http_server == "nginx"
                

In this case, the task will only run if the http_server variable is set to "nginx".

Conditionals with Multiple Conditions

You can also combine multiple conditions using logical operators like and, or, and not. This allows for more complex conditional logic in your playbooks.

Example with multiple conditions:

- name: Install MariaDB
  apt:
    name: mariadb-server
    state: present
  when: ansible_os_family == "Debian" and ansible_distribution_version == "10"
                

In this example, the task will only run if the OS family is Debian and the OS version is 10.

Using Facts in Conditionals

Ansible facts are system properties that are automatically gathered by Ansible. You can use these facts in your conditionals to make decisions based on the state of your system.

Example using Ansible facts:

- name: Ensure the service is running
  service:
    name: apache2
    state: started
  when: ansible_facts['os_family'] == "Debian"
                

This task ensures that the Apache service is running, but only if the OS family is Debian.

Conditional Loops

You can also use conditionals within loops to control which items in a loop are processed. This is done by adding the when directive within the loop.

Example with a loop:

- name: Install multiple packages
  apt:
    name: "{{ item }}"
    state: present
  loop:
    - "nginx"
    - "mysql-server"
    - "php-fpm"
  when: item != "mysql-server" or install_mysql == true
                

In this example, the task will skip the installation of MySQL unless the install_mysql variable is set to true.

Conclusion

Using conditionals in Ansible playbooks allows for more granular and flexible control over your automation tasks. By leveraging variables, facts, and logical operators, you can create highly dynamic playbooks that adapt to various conditions and states.