🚀 DevOps & SRE Certification Program 📅 Starting: 1st of Every Month 🤝 +91 8409492687 🔍 Contact@DevOpsSchool.com

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

How to set condition module executations in Ansible Playbook with Example?

Example – 1 For CENTOS / RHEL


---
- name: Update web servers
  hosts: web
  vars:
    myname: "Rajesh Kumar"

  tasks:
  - name: Install Apache in centos7
    ansible.builtin.yum:
      name: httpd
      state: latest
    when: ( ansible_os_family == "RedHat" and ansible_system == "Linux")
  - name: Copy index.html
    ansible.builtin.copy:
      src: index.html
      dest: /var/www/html/index.html
  - name: Starting a Apache Server
    ansible.builtin.service:
      name: httpd
      state: started
    when: 
      - ansible_os_family == "RedHat"
      - ansible_distribution_major_version == "7"
      - ansible_system == "Linux"
  - name: My Name is Equals to Rajesh Kumar
    debug:
      msg: "My Name is Equals to Rajesh Kumar"
    when: myname == "Rajesh Kumar"
  - name: Ansible print when ubuntu
    debug:
      msg: "I am ubuntu"
    when: ansible_os_family == "Debian"
  - name: My Name is not Equals to Rajesh Kumar
    debug:
      msg: "My Name is not Equals to Rajesh Kumar"
    when: myname != "Rajesh Kumar"

---
- name: Update web servers
hosts: web
vars:
myname: "Rajesh Kumar"
tasks:
- name: Install Apache in centos7
ansible.builtin.yum:
name: httpd
state: latest
when: ( ansible_os_family == "RedHat" and ansible_system == "Linux")
- name: Copy index.html
ansible.builtin.copy:
src: index.html
dest: /var/www/html/index.html
- name: Starting a Apache Server
ansible.builtin.service:
name: httpd
state: started
when:
- ansible_os_family == "RedHat"
- ansible_distribution_major_version == "7"
- ansible_system == "Linux"
- name: My Name is Equals to Rajesh Kumar
debug:
msg: "My Name is Equals to Rajesh Kumar"
when: myname == "Rajesh Kumar"
- name: Ansible print when ubuntu
debug:
msg: "I am ubuntu"
when: ansible_os_family == "Debian"
- name: My Name is not Equals to Rajesh Kumar
debug:
msg: "My Name is not Equals to Rajesh Kumar"
when: myname != "Rajesh Kumar"
# We can also check if a variable is empty using similar manner.
- hosts: all
tasks:
- shell: cat /etc/temp.txt
register: output
- name: Ansible when variable is empty example
debug:
msg: "empty"
when: output.stdout == ""
---
# this is a demo of conditional executions using 'when' statements, which can skip
# certain tasks on machines/platforms/etc where they do not apply.
- hosts: all
remote_user: root
vars:
favcolor: "red"
dog: "fido"
cat: "whiskers"
ssn: 8675309
tasks:
- name: "do this if my favcolor is blue, and my dog is named fido"
shell: /bin/false
when: favcolor == 'blue' and dog == 'fido'
- name: "do this if my favcolor is not blue, and my dog is named fido"
shell: /bin/true
when: favcolor != 'blue' and dog == 'fido'
- name: "do this if my SSN is over 9000"
shell: /bin/true
when: ssn > 9000
- name: "do this if I have one of these SSNs"
shell: /bin/true
when: ssn in [ 8675309, 8675310, 8675311 ]
- name: "do this if a variable named hippo is NOT defined"
shell: /bin/true
when: hippo is not defined
- name: "do this if a variable named hippo is defined"
shell: /bin/true
when: hippo is defined
# note that Ansible facts and vars like ansible_os_family can be used
# directly in conditionals without double curly braces
tasks:
- name: "shut down Debian flavored systems"
command: /sbin/shutdown -t now
when: ansible_os_family == "Debian"
# You can also use parentheses to group conditions:
tasks:
- name: "shut down CentOS 6 and Debian 7 systems"
command: /sbin/shutdown -t now
when: (ansible_distribution == "CentOS" and ansible_distribution_major_version == "6") or
(ansible_distribution == "Debian" and ansible_distribution_major_version == "7")
# Multiple conditions that all need to be true (a logical �and�) can also be specified as a list:
tasks:
- name: "shut down CentOS 6 systems"
command: touch /opt/verizon.txt
when:
- ansible_distribution == "RedHat"
- ansible_distribution_major_version == "7.5"
#If a required variable has not been set, you can skip or fail using Jinja2�s defined test. For example:
tasks:
- shell: echo "I've got '{{ foo }}' and am not afraid to use it!"
when: foo is defined
- fail: msg="Bailing out. this play requires 'bar'"
when: bar is undefined
# Following is the simplest example of checking whether the value of a variable. We have created a variable test1 and checking if the value is �Hello World� using the when statement.
- hosts: all
vars:
test1: "Hello World"
tasks:
- name: Ansible when variable equals example
debug:
msg: "Equals"
when: test1 == "Hello World"
# We can also set the reverse, i.e. if the variable is not equal to another value.
- hosts: all
vars:
test1: "Bye World"
tasks:
- name: Ansible when variable not equals example
debug:
msg: "Not Equals"
when: test1 != "Hello World"
# We can also make a conditional statement based on whether the variable contains a particular string.
- hosts: all
vars:
test1: "Bye World"
tasks:
- name: Ansible when variable contains string example example
debug:
msg: "Equals"
when: test1.find("World") != -1
#If the variable value is registered from a shell command you may have to use stdout.find to check the content.
- hosts: all
tasks:
- shell: cat /etc/temp.txt
register: output
- name: Ansible when variable contains string example example
debug:
msg: "Equals"
when: output.stdout.find("World") != -1

Example – 1 For Ubuntu

- name: Update web servers
  hosts: web
  vars:
    myname: "Rajesh Kumar"

  tasks:
  - name: Install Apache in ubuntu
    ansible.builtin.apt:
      name: apache2
      state: latest
    when: ( ansible_os_family == "Debian" and ansible_system == "Linux")
  - name: Copy index.html
    ansible.builtin.copy:
      src: index.html
      dest: /var/www/html/index.html
  - name: Starting a Apache Server
    ansible.builtin.service:
      name: apache2
      state: started
    when: 
      - ansible_os_family == "Debian"
      - ansible_distribution_major_version == "20"
      - ansible_system == "Linux"
  - name: My Name is Equals to Rajesh Kumar
    debug:
      msg: "My Name is Equals to Rajesh Kumar"
    when: myname == "Rajesh Kumar"
  - name: Ansible print when ubuntu
    debug:
      msg: "I am ubuntu"
    when: ansible_os_family == "Debian"
  - name: My Name is not Equals to Rajesh Kumar
    debug:
      msg: "My Name is not Equals to Rajesh Kumar"
    when: myname != "Rajesh Kumar"

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.