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"

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"
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.