Conditions in ANSIBLE

What Are Conditions?

Using when Statements in Ansible

Conditional Playbook Example:

--- # CONDITIONAL PLAYBOOK
- hosts: demo  # Target host group
  user: ansible  # Running tasks as the 'ansible' user
  become: yes  # Running with sudo privileges
  connection: ssh  # Connecting via SSH

  tasks:
    - name: Install Apache Server for Debian Family
      command: apt-get -y install apache2
      when: ansible_os_family == "Debian"

    - name: Install Apache Server for RedHat Family
      command: yum -y install httpd
      when: ansible_os_family == "RedHat"

Lab: Implementing Conditions in Ansible

Step 1: Prepare the Environment

1️⃣ Continue from the previous LAB (4th Lab)

If you have stopped your instances after the last lab, start all instances and log in via PuTTY.

2️⃣ Log into the Ansible Server