tags lesson completed

This commit is contained in:
Ilgar_Naghiyev
2020-03-04 12:02:36 +01:00
parent e621c5ec33
commit 66478e651e
4 changed files with 89 additions and 1 deletions

View File

@@ -27,6 +27,9 @@ Red Hat Certified Specialist in Ansible Automation (EX407) Preparation Course
- [Use Conditionals to Control Play Execution Part 2](#use-conditionals-to-control-play-execution-part-2)
- [Configure Error Handling](#configure-error-handling)
- [Demo: Error Handling Ignore Errors](#demo-error-handling-ignore-errors)
- [Demo: Error Handling Block Groups](#demo-error-handling-block-groups)
- [Selectively Run Specific Tasks In Playbooks Using Tags](#selectively-run-specific-tasks-in-playbooks-using-tags)
## Understanding Core Components of Ansible
### Understanding Core Components of Ansible Part 1
@@ -676,4 +679,58 @@ Demonstration of playbook:
- We're going to `ignore errors` and even if playbook failed it's going to execute
- Output will look like this:
![img](https://github.com/Bes0n/EX407-Ansible-Automation/blob/master/images/img15.png)
![img](https://github.com/Bes0n/EX407-Ansible-Automation/blob/master/images/img15.png)
### Demo: Error Handling Block Groups
Block groups and rescues - like a try and catch.
```
---
- hosts: labservers
tasks:
- name: get file
block:
- get_url:
url: "http://innaghiyev3c/index.html"
dest: "/tmp/index_file"
rescue:
- debug: msg="The file doesn't exists!"
always:
- debug: msg="Play done!"
```
- `rescue` - that part of block will run if playbook run failed
- `always` - that part of block will run any time. No matter playbook run fails or not.
Here how rescue block is working:
![img](https://github.com/Bes0n/EX407-Ansible-Automation/blob/master/images/img16.png)
### Selectively Run Specific Tasks In Playbooks Using Tags
How Ansible uses tags
- You can have several deployments in your cookbook. Like database and application deployment
- By using tags we can deploy only application stage or only database stage
```
---
- hosts: web
become: yes
tasks:
- name: deploy app binary
copy:
src: /home/cloud_user/apps/hello
dest: /var/www/html/hello
tags:
- webdeploy
- hosts: db
become: yes
tasks:
- name: deploy db script
copy:
src: /home/cloud_user/apps/script.sql
dest: /opt/deb/scripts/script.sql
tags:
- dbdeploy
```
- `ansible-playbook tags_playbook.yml` - running playbook without tags
- `ansible-playbook tags_playbook.yml --tags webdeploy` - running playbook only for `webdeploy` part
- `ansible-playbook tags_playbook.yml --tags dbdeploy` - running playbook only for `dbdeploy` part

BIN
images/img16.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 424 KiB

View File

@@ -0,0 +1,12 @@
---
- hosts: labservers
tasks:
- name: get file
block:
- get_url:
url: "http://innaghiyev2c/index.html"
dest: "/tmp/index_file"
rescue:
- debug: msg="The file doesn't exists!"
always:
- debug: msg="Play done!"

View File

@@ -0,0 +1,19 @@
---
- hosts: web
become: yes
tasks:
- name: deploy app binary
copy:
src: /home/user/apps/hello
dest: /var/www/html/hello
tags:
- webdeploy
- hosts: db
become: yes
tasks:
- name: deploy db script
copy:
src: /home/user/apps/script.sql
dest: /opt/deb/scripts/script.sql
tags:
- dbdeploy