Ansible when conditional explained - Part 9

Опубликовано: 27 Декабрь 2024
на канале: Rahul Wagh
4,316
92

▬▬▬▬▬▬ 🚀 Top resource to learn Cloud Computing ▬▬▬▬▬▬
Link - https://datacamp.pxf.io/4GaEq3

▬▬▬▬▬▬ 🗓️ Book Me for Consultation ▬▬▬▬▬▬
Calender - https://topmate.io/rahul_wagh17 ▬▬▬▬▬▬ 🚀 Courses ▬▬▬▬▬▬

1. Terraform -    • HashiCorp Terraform Associate Certifi...  
2. Ansible -    • Ansible for Beginners: Learn the Fund...  
3. Prometheus & Grafana -    • Getting started with Prometheus Grafa...  
4. Helm Chart -    • Complete Helm Chart Tutorial: From Be...  
5. Hashi Corp vault -    • HashiCorp Vault Installation - Part 1...  

▬▬▬▬▬▬ ⭐️ Guide and Repository ⭐️ ▬▬▬▬▬▬
Github Repository : https://github.com/rahulwagh/ansible-...


The usage of conditionals, which are used to regulate the playbook's flow based on the values of variables or facts, is one of Ansible's key features. We'll talk about Ansible conditionals and how to utilize them in playbooks in this blog article.

Similar to conditional statements in programming languages, Ansible uses conditionals. They enable us to declare one set of activities to be carried out in the event of a true condition, and a another set of actions in the event of a false condition. Conditions can be used in tasks, blocks, and loops in Ansible.

The "when" phrase is one of the most utilized conditionals in Ansible. We can provide a prerequisite that must be satisfied before a task is executed using the "when" expression. The "when" statement can be used, for instance, to determine whether a package is installed on a remote server before executing a process that depends on it. Here's an illustration:

---
name: Install Apache web server
apt:
name: apache2
state: present
when: ansible_distribution == 'Ubuntu'

n this example, the task to install the Apache web server will only be executed if the remote server is running Ubuntu.

Conditionals can also be used in loops in Ansible. For example, we can use the "when" statement to filter the items in a loop based on a condition. Here's an example:

---
name: Install multiple packages
apt:
name: "{{ item }}"
state: present
when: item in packages_to_install
loop: "{{ all_packages }}"

In this example, the task will only be executed for packages that are in the "packages_to_install" list.

▬▬▬▬▬▬ ⭐️ Time Stamps ⭐️ ▬▬▬▬▬▬
0:00 Intro
0:07 What is Ansible Conditional?
0:55 How to implement Ansible Conditional?
02:15 Multiple Conditional
03:44 Conditional on variable
05:41 Conditional with Loop
06:58 Ansible facts with Conditional
08:43 Demo

▬▬▬▬▬▬ ⭐️ Follow me ⭐️ ▬▬▬▬▬▬

Linkedin -   / rahul-wagh  
Twitter -   / wagh_rahul  

To learn more on DevOps visit - https://jhooq.com/
Learn more onto Anisble - https://www.ansible.com/

Disclaimer/Policy: All the content/instructions are solely mine. The source is completely open-source.

Video is copyrighted and can not be re-distributed on any platform.