part 2 completed
This commit is contained in:
43
README.md
43
README.md
@@ -24,7 +24,7 @@ Red Hat Certified Specialist in Ansible Automation (EX407) Preparation Course
|
|||||||
- [Basic Playbook Syntax Demonstration](#basic-playbook-syntax-demonstration)
|
- [Basic Playbook Syntax Demonstration](#basic-playbook-syntax-demonstration)
|
||||||
- [Use Variables to Retrieve the Results of Running Commands](#use-variables-to-retrieve-the-results-of-running-commands)
|
- [Use Variables to Retrieve the Results of Running Commands](#use-variables-to-retrieve-the-results-of-running-commands)
|
||||||
- [Use Conditionals to Control Play Execution Part 1](#use-conditionals-to-control-play-execution-part-1)
|
- [Use Conditionals to Control Play Execution Part 1](#use-conditionals-to-control-play-execution-part-1)
|
||||||
|
- [Use Conditionals to Control Play Execution Part 2](#use-conditionals-to-control-play-execution-part-2)
|
||||||
|
|
||||||
## Understanding Core Components of Ansible
|
## Understanding Core Components of Ansible
|
||||||
### Understanding Core Components of Ansible Part 1
|
### Understanding Core Components of Ansible Part 1
|
||||||
@@ -583,6 +583,8 @@ Let's write some simple playbook.
|
|||||||
|
|
||||||

|

|
||||||
|
|
||||||
|
Playbook with handler:
|
||||||
|
|
||||||
```
|
```
|
||||||
---
|
---
|
||||||
- hosts: labservers
|
- hosts: labservers
|
||||||
@@ -606,3 +608,42 @@ Explanation for playbook:
|
|||||||
- once notify message appeared it's going to trigger handler in the end of play
|
- once notify message appeared it's going to trigger handler in the end of play
|
||||||
- handler is not going to run again if there is no change made.
|
- handler is not going to run again if there is no change made.
|
||||||
- doesn't matter if we run playbook several times, handler will not be triggered (what is useful, to avoid downtime of the service)
|
- doesn't matter if we run playbook several times, handler will not be triggered (what is useful, to avoid downtime of the service)
|
||||||
|
|
||||||
|
### Use Conditionals to Control Play Execution Part 2
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
- **When** - if condition is true, it will execute. More similar to **if-else** condition
|
||||||
|
- **With_items** - will take each item in a list and loop through it
|
||||||
|
- **With_files** - really similar to **With_items**
|
||||||
|
|
||||||
|
Demonstration of how to use a **loop** in playbooks:
|
||||||
|
```
|
||||||
|
---
|
||||||
|
- hosts: labservers
|
||||||
|
become: yes
|
||||||
|
tasks:
|
||||||
|
- name: create users
|
||||||
|
user:
|
||||||
|
name: "{{item}}"
|
||||||
|
with_items:
|
||||||
|
- sam
|
||||||
|
- john
|
||||||
|
- bob
|
||||||
|
```
|
||||||
|
- Following playbook is going to run through `items` and create users **sam** **john** and **bob** listed in `with_items` block
|
||||||
|
|
||||||
|
Demonstration of how to use **when** condition in playbooks:
|
||||||
|
```
|
||||||
|
---
|
||||||
|
- hosts: labservers
|
||||||
|
become: yes
|
||||||
|
tasks:
|
||||||
|
- name: edit index
|
||||||
|
lineinfile:
|
||||||
|
path: /var/www/html/index.html
|
||||||
|
line: "I'm back!!!"
|
||||||
|
when:
|
||||||
|
- ansible_hostname == "innaghiyev1c"
|
||||||
|
```
|
||||||
|
- Condition is going to wait for proper hostname and apply your changes only on that node.
|
||||||
BIN
images/img14.png
Normal file
BIN
images/img14.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 224 KiB |
11
playbooks/loop_playbook.yml
Normal file
11
playbooks/loop_playbook.yml
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
---
|
||||||
|
- hosts: labservers
|
||||||
|
become: yes
|
||||||
|
tasks:
|
||||||
|
- name: create users
|
||||||
|
user:
|
||||||
|
name: "{{item}}"
|
||||||
|
with_items:
|
||||||
|
- sam
|
||||||
|
- john
|
||||||
|
- bob
|
||||||
10
playbooks/when_playbook.yml
Normal file
10
playbooks/when_playbook.yml
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
---
|
||||||
|
- hosts: labservers
|
||||||
|
become: yes
|
||||||
|
tasks:
|
||||||
|
- name: edit index
|
||||||
|
lineinfile:
|
||||||
|
path: /var/www/html/index.html
|
||||||
|
line: "I'm back!!!"
|
||||||
|
when:
|
||||||
|
- ansible_hostname == "innaghiyev1c"
|
||||||
Reference in New Issue
Block a user