ကျွန်တော်တို့ ansible block ကို ansible playbook မှာ tasks တွေကို groups လိုက်စုပြီး run တဲ့နေရာမှာ သုံးတာများပါတယ်.
ansible block ကို error handling အတွက်လဲသုံးလို့ရပါတယ်, programming language တွေမှာ exception handling သဘောမျိူးပါ.
ansible block ရဲ့ error handling က python ရဲ့ { try/except/finally } နဲ့ သဘောတရား တူပါတယ်.
python ရဲ့ error and exception handling { try/except/finally } concept ကို နမူနာကြည့်ပါ.
{try/except/finally}
vim try_except.py
x = 'testing'
def try_except(*args):
try:
print(x)
except:
print("variable x is not defined")
finally:
print("testing exception handling in python")
test = try_except()

x = variable ကို သတ်မှတ်ထားရင် try အောက်မှာရှိတဲ့ print(x) statement က run မှာပါ.
x = variable ကို မသတ်မှတ်ထားရင် except အောက်မှာရှိတဲ့ print(“variable is not defined”) က run မှာပါ.
finally က x = variable သတ်မှတ်တဲ့အပေါ်မမူတည်ပါဘူး, finally က အမြဲတန် run မှာပါ.
#x = 'testing'
def try_except(*args):
try:
print(x)
except:
print("variable x is not defined")
finally:
print("testing exception handling in python")
test = try_except()
တစ်ကယ်လို့ try_except.py မှာ variable ကို မသတ်မှတ်ဘဲ run ရင် except က run မှာပါ.
finally ကတော့အမြဲတန်း run မှာပါ.

ansible block
ansible block ကို playbook မှာ tasks တွေကို group ဖွဲပြီး run တဲ့နေရာမှာ သုံးတာကို ကြည့်ပါ.
vim block.yaml
---
- name: check when statement
hosts: all
tasks:
- name: install lamp stack on ubunut
block:
- name: install apache server
apt:
name: apache2
state: latest
- name: install mysql
apt:
name: mysql-server
state: latest
- name: install php
apt:
name: php7.4
state: latest
when: ansible_distribution == "Ubuntu"
ansible block ကို conditionals နဲ့တွဲ သုံးထားပါတယ်. တစ်ကယ်လို့ ansible_distribution က ubuntu ဖြစ်ရင် ansible block ကို run မှာပါ.
ansible conditionals ( when ) နဲ့ပါတ်သတ်ပြီး ကျွန်တော် အရင်က sharing လုပ်ထားတာရှိပါတယ်.
ansible block ကို error handling မှာသုံးတာကို နမူနာကြည့်ပါ.
error handling ကို python error handling ( try/except/finally ) အတိုင်းဘဲ ansible block မှာ ( block/rescue/always ) ပုံစံနဲ့ သုံးလို့ရပါတယ်.
python exception handling and ansible block ရဲ့ comparison chart ကိုကြည့်ပါ.
Python | equal to ( = ) | Ansible Block |
try | –> | block |
except | –> | rescue |
finally | –> | always |
ansible playbook မှာ ansible block ကို error handling အတွက်သုံးတာကိုကြည့်ပါ.
vim try_except.yaml
---
- name: use ansible block
hosts: all
tasks:
- name: use ansible block to install webserver
block:
- name: block - install apache server on CentOS
yum:
name: httpd
state: latest
rescue:
- name: rescue - install apache server on ubuntu
apt:
name: apache2
state: absent
always:
- name: always - check webserver status
shell: echo "successfully install webserver"
တစ်ကယ်လို့ block အပိုင်းက tasks တွေက fail ဖြစ်ရင် rescue ပိုင်းက effect ဖြစ်မှာပါ.
block ပိုင်းက tasks က success ဖြစ်ရင် rescue ပိုင်းကို skipped လုပ်မှာပါ.
always ကတော့ block, rescue tasks တွေရဲ့ fail, success ဖြစ်တဲ့အပေါ်မမူတည်ပါဘူး.
always ကအမြဲတန်း run မှာပါ.


ansible handlers ( inactive tasks )
ကျွန်တော်တို့ ansible tasks တွေကို remote hosts တွေရဲ့ changed state ပေါ်မူတည်ပြီး run စေချင်တဲ့ အခါမှာ ansible handlers ကိုသုံးပါတယ်.
ansible handlers က tasks ပါဘဲ, inactive ဖြစ်နေတဲ့ tasks ပါ, remote hosts မှာ ကိုယ်သတ်မှတ်ထားတဲ့ tasks က changed state ဖြစ်မှ handlers က effect ဖြစ်မှာပါ.
ansible playbook မှာ ansible handlers ( inactive tasks ) ကို notify keyword သုံးပြီး သုံးလို့ရပါတယ်.
ansible handlers က tasks ဖြစ်တဲ့ အတွက် ansible modules တွေကိုသုံးလို့ရပါတယ်.
ansible playbook မှာသုံးတဲ့ handlers တွေရဲ့ name က unique ဖြစ်ရပါမယ်.
ansible playbook မှာကို သုံးချင်တဲ့ handlers tasks ကို handlers keyword အောက်မှာ ရေးရမှာပါ.
handlers ကို ansible playbook မှာသုံးတဲ့အခါမှာ tasks keyword နဲ့ indentation level တူရပါမယ်.
handlers ကို ansible playbook မှာသုံးတာကို နမူနာ ကြည့်ပါ.
---
- name: use ansible block
hosts: dev
tasks:
- name: use ansible block to install webserver
block:
- name: block - install apache server on CentOS
yum:
name: httpd
state: latest
rescue:
- name: rescue - install apache server on ubuntu
apt:
name: apache2
state: latest
notify: restart apache
always:
- name: always - check webserver status
shell: echo "successfully install webserver"
handlers:
- name: restart apache
service:
name: apache2
state: restarted

ansible playbook ကို run ကြည့်ရအောင်.
ansible-playbook try_except.yaml

ansible ကို ဆက်လေ့လာလိုလျှင်.
https://www.ansiblefordevops.com/
https://www.jeffgeerling.com/blog/2020/ansible-101-jeff-geerling-youtube-streaming-series