Skip to content

Commit 369ad21

Browse files
committed
ansible conditionals
1 parent 303cc46 commit 369ad21

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

part-9-ansible-conditionals/README.md

+14
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,17 @@
33
```bash
44
ansible-playbook --inventory inventory/ansible-conditionals-playbook/hosts ansible-conditionals-playbook.yml
55
```
6+
7+
8+
## How to find apache is installed or not
9+
10+
```
11+
type -a apache2
12+
```
13+
14+
15+
## SSH to EC2 instance
16+
17+
```
18+
ssh -i /Users/rahulwagh/.ssh/aws_ec2_terraform [email protected]
19+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
var_file_variable: "Hello, world in main var.yml!"
2+
install_apache_flag: false

0 commit comments

Comments
 (0)