Ansible when conditional explained - Part 9

preview_player
Показать описание
▬▬▬▬▬▬ 🚀 Courses ▬▬▬▬▬▬

▬▬▬▬▬▬ ⭐️ Guide and Repository ⭐️ ▬▬▬▬▬▬

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 ⭐️ ▬▬▬▬▬▬

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.
Рекомендации по теме
Комментарии
Автор

What is the syntax for evaluating when one variable equals another variable with concatentation? For example:
host1: "abc"
host2: "xyz"
tld1: "com"
tld2: "org"

- name: Cat a file when on host 1
ansible.builtin.shell: 'cat /opt/file'
when: inventory_hostname == {{ host1 }}.{{ tld1 }}

You can't use the {{ }} delimiters, because it fails as an unsafe conditional. But if you try host1.tld1, it assumes tld1 is an attribute of host1.

BatMite
Автор

when the state = absent, how it will install apahe2 server? even if the boolean is

shaikkhaja
Автор

The apache state should be latest correct?
Why absent

Ksailor
Автор

Great content for beginners like me, but a small miss sync between your explanation and the yaml file, with in the yaml file your when condition is True it shud uninstall the apache2 from the host, but in your lecture you were referring to installation of httpd if condition is true, which is not correct. [Please see line number 6 [state]]. an absent derivative will uninstall the appache2.

srk
Автор

I have a std output where it contain

Rahul 001
Hi Rahul
He is tall
He is the smart
Rahul 002
Hi Rahul
He is energetic

There will be number of lines like that with Rahul rollnumber and his description

I need to print Rahul rollnumber when the description of Rahul is having tall/energetic

I tried for single list using find but I am not sure how to print each Rahul when the condition match as there are number rollnumbers in the output

cpManikanta
join shbcf.ru