1
+ ---
2
+ # # Boolean Conditional check for installing Apache
3
+ - name : Install Apache web server
4
+ apt :
5
+ name : apache2
6
+ state : absent
7
+ when : install_apache_flag
8
+
9
+ # # Conditionals based on ansible_facts
10
+ - name : How to use ansible_facts
11
+ debug :
12
+ var : ansible_facts['distribution']
13
+ # var: ansible_facts['distribution_major_version']
14
+ # var: ansible_facts['kernel_version']
15
+ # var: ansible_facts
16
+ when : ansible_facts['os_family'] == "Debian"
17
+
18
+ # # Putting multiple condition using ansible_facts
19
+ - name : Combine conditions using ansible facts
20
+ debug :
21
+ var : ansible_facts['kernel_version']
22
+ when :
23
+ - ansible_facts['os_family'] == "Debian"
24
+ - ansible_facts['distribution_major_version'] == "20"
25
+
26
+ # # Setting complex condition
27
+ - name : Setting complex condition
28
+ debug :
29
+ var : ansible_facts['kernel_version']
30
+ when : ansible_facts['os_family'] == "Debian" and ansible_facts['distribution_major_version'] | int >= 20
31
+
32
+ # # Register a variables and evaluate the value using when condition
33
+ - name : Register a variable
34
+ ansible.builtin.command : cat /home/ubuntu/test-file.txt
35
+ register : test_file_content
36
+
37
+ - name : Use the variable in conditional statement
38
+ debug :
39
+ var : test_file_content.stdout
40
+ when : test_file_content.stdout.find('hi') != -1 # and test_file_content is succeeded
41
+
42
+ # # When with Loop
43
+ - name : Run with items greater than 5
44
+ ansible.builtin.command : echo {{ item }}
45
+ loop : [ 0, 2, 4, 6, 8, 10 ]
46
+ when : item > 5
0 commit comments