Conditional Statements in Ansible

Last Updated : 18 Aug, 2025

Ansible Conditional Statements allow the user to manage the execution of tasks based on specific conditions. The primary mechanism by which conditional logic is added to tasks in Ansible is by using the when keyword.

These allow tasks to run only when specific criteria are met such as variable values, file presence, or system facts. This ensures selective task execution, improves playbook flexibility, and supports robust automation.

Syntax

IF expressions are introduced by the keyword when followed by an expression whose value is either true or false; if the value is true, then an action is performed, and otherwise, it isn't done.

Example:

yaml
- name: Print message when condition is met
  debug:
    msg: "This task runs only if the condition is true"
  when: some_condition

Expressions

The condition in a when statement can be a simple variable, a comparison, or a more complex expression involving logical operators such as and, or, and not.

Example:

yaml
- name: Print message if variable is true
  debug:
    msg: "The variable is true"
  when: example_var
- name: Print message if both conditions are true
  debug:
    msg: "Both conditions are true"
  when: var1 and var
- name: Print message if either condition is true
  debug:
    msg: "At least one condition is true"
  when: var1 or var2

Combining Conditions

  • Logical operators will allow you to combine multiple conditions in order to form a very complicated conditional statement.

Example:

yaml
- name: Complex condition
  debug:
    msg: "Complex condition met"
  when: var1 and (var2 or var3)

Registered Variables

Tasks can register variables that store the results of their execution. The registered variables can then be used within conditional statements, in which decisions are taken based on the output of previous tasks.

Example:

yaml
- name: Check if a file exists
  stat:
    path: /path/to/file
  register: file_status
- name: Print message if file exists
  debug:
    msg: "The file exists"
  when: file_status.stat.exists

Process for using Conditional Statements

Step 1: Launch EC2 Instance

 Launch EC2 Instance

Step 2: Install Ansible

Now install ansible in our local machine by using following command.

 sudo amazon-linux-extras install ansible2
Install Ansible

Step 3: Define Inventory file or Host

Define Inventory file or Host

Step 4: Create Playbook

Define Variables

  • Variables are essential for storing values that can be reused throughout your playbook. You can define variables in several places, including inventory files, playbooks, roles, and extra variables passed from the command line.

- name: Example Playbook with Variables

hosts: localhost

vars:

example_var: true

tasks:

- name: Print variable value

debug:

var: example_var

Use when Keyword

  • When keyword is used to specify the condition that must be true for the task to run. Conditions can be based on variable values or expressions.

- name: Example Playbook with Conditional Statements

hosts: localhost

vars:

is_debug: true

tasks:

- name: Print debug message

debug:

msg: "Debug mode is enabled"

when: is_debug

Use Logical Operators

  • You can use logical operators (and, or, not) to combine multiple conditions in a single when statement, creating more complex conditions.

- name: Example Playbook with Complex Conditions

hosts: localhost

vars:

var1: true

var2: false

tasks:

- name: Print message if both conditions are true

debug:

msg: "Both conditions are true"

when: var1 and var2

- name: Print message if either condition is true

debug:

msg: "At least one condition is true"

when: var1 or var2

Use Registered Variables

  • Registered variables capture the results of tasks and can be used in subsequent tasks. They are created using the register keyword.

- name: Example Playbook with Registered Variables

hosts: localhost

tasks:

- name: Check if a file exists

stat:

path: /path/to/file

register: file_status

- name: Print message if file exists

debug:

msg: "The file exists"

when: file_status.stat.exists

Combine All Concepts

  • Combine variables, when keyword, logical operators, and registered variables to create a comprehensive playbook with dynamic task execution.

Example:

- name: Comprehensive Playbook with Conditional Statements

hosts: localhost

vars:

var1: true

var2: false

file_path: "/path/to/file"

tasks:

- name: Check if the file exists

stat:

path: "{{ file_path }}"

register: file_status

- name: Print message if both conditions are true

debug:

msg: "Both conditions are true"

when: var1 and var2

- name: Print message if file exists

debug:

msg: "The file exists"

when: file_status.stat.exists

- name: Print message if either condition is true

debug:

msg: "At least one condition is true or the file exists"

when: var1 or var2 or file_status.stat.exists

Combine All Concepts

Step 5: Run Playbook

  • Now run playbook by using ansible-playbook commands
Run Playbook
  • Two tasks were skipped because their conditions evaluated to false
  • The path specified in your variable file was "/path/to/file", which likely does not exist.

Example 2

  • Now change file path to see different result

var1: true

var2: true

file_path: "/etc/hosts" # Assuming this file exists on your system

Example 2
  • Run the playbook again to see different results.
Run the playbook again to see different results.

Here we see all tasks are executed.

Comment