ကျွန်တော်တို့ ansible ကို သုံးပြီး resources ကို automate လုပ်တဲ့ အခါမှာ ansible ad-hoc and playbook တို့က အရေးပါပါတယ်.
ကျွန်တော်တို့ resources တွေကို automate လုပ်တဲ့ အခါ မှာ automate လုပ်မဲ့ tasks က single task ဖြစ်ပြီး ထပ်ခါထပ်ခါ run ဖို့မလိုရင် ansible ad-hoc command ကိုသုံးလို့ရပါတယ်.
ansible playbook ကိုတော့ ကျွန်တော်တို့ automate လုပ်မဲ့ tasks တွေက multiple and complex tasks တွေဖြစ်တဲ့အခါ မှာသုံးပါတယ်.
ansible ad-hoc
ansible ad-hoc command က /usr/bin/ansible binary ကို သုံးပါတယ်. ansible ad-hoc command ကို server တွေကို reboot လုပ်တာ, server တွေရဲ့ variables တွေကို gather facts လုပ်တာ, file, packages, users, services တွေကို single tasks အနေနဲ့ automate လုပ်တဲ့ အခါမှာ သုံးပါတယ်.
ansible ad-hoc command နဲ့ repeated tasks တွေကို automate လုပ်ရင်အဆင်မပြေပါဘူး.
ကျွန်တော်တို့ ansible ad-hoc command ကိုကြည့်ရအောင်.
ansible [pattern] -m [module] -a "[module options]"
[pattern] ကကျွန်တော်တို့ inventory မှာ define လုပ်ထားတဲ့ hosts, groups တွေကို ရည်ညွှန်းပါတယ်.
[module] က ကျွန်တော်တို့ ansible module တွေကို ပြောတာပါ.
[module options] က ansible module တွေရဲ့ arguments တွေကို ရည်ညွှန်းပါတယ်.
ansible ad-hoc ရဲ့ default module က command moudule ပါ. ansible command ကို အခြား ansible module မပါဘဲ run ရင် command module ကို default အနေနဲ့ သုံးပါတယ်.
ansible worker1.example.com -a "reboot"
ansible ad-hoc command နမူနာ တစ်ခု run ကြည့်ရအောင်. apt module ကို သုံးပြီး apt upgrade လုပ်တာပါ.
ansible worker1.example.com -m apt -a "name='*' state=latest"

worker1.example.com က inventory pattern မှာ host pattern တစ်ခု ထဲကို ခေါ်ထားပါတယ်. group တစ်ခုလုံးကို သုံးလဲအဆင်ပြေပါတယ်.
-m apt က ansible apt module ကို သုံးထားပါတယ်.
-a က ansible module arguments တွေပါ.
တစ်ကယ်လို့ group တစ်ခုလုံကို ansible ad-hoc နဲ့ automate လုပ်ချင်ရင် default group တွေကို သုံးလို့ ရပါတယ်. အခြား inventory file မှာသတ်မှတ်ထားတဲ့ groups တွေကိုသုံးလဲရပါတယ်.
ansible all -m apt -a "name='*' state=latest"
all and ungrouped က inventory default group တွေပါ.

default အနေနဲ့ ansible က processes 5 ခုကို တစ်ပြိုင်ထဲ automate လုပ်ပါတယ်. ကျွန်တော်တို့ ansible ad-hoc and ansible.cfg မှာ ပြင်လို့ရပါတယ်.
ansible all -a "/sbin/reboot" -f 10
-f = fork ( parallel process )
-f 10 က parallel process ကို ansible ad-hoc မှာ 10 ပြောင်းထားတာပါ.
ansible.cfg မှာ ပြောင်းလဲရပါတယ်.
vim ansible.cfg

ansible ad-hoc command တွေကိုစုပြီး script အနေနဲ့သုံးလဲရပါတယ်.
ကျွန်တော်တို့ managed hosts တွေမှာ test user account ကို ansible ad-hoc သုံးပြီး create လုပ်ကြည့်ရအောင်.
vim create.sh
chmod +x create.sh
./create.sh

ansible setup module ကိုသုံးပြီး control or managed hosts တွေရဲ့ variables ကို ယူသုံးလိုရပါတယ်.
ansible localhost -m setup ( for localhost variables query )
ansible all -m setup ( for managed hosts variables query )
ansible localhost -m setup > gather_facts.json ( variables output )


ansible ad-hoc ကိုသုံးပြီး single tasks တွေကို automate လုပ်တဲ့ အခါမှာ ansible modules argument တွေကို ” name=value ” format နဲ့ ရေးပါ. ” = “ ကိုသုံးပါ , “name:value” format ကိုသုံးလို့ မရပါဘူး. ansible playbook မှာသုံးလို့ရပါတယ်.
ansible all -m user -a "name='test' state=absent remove=yes"
ansible modules document တွေကို online or offline ကြည့်လို့ရပါတယ်.
ansible-doc -l

ansible modules တွေကို ansible version 2.10 ကစပြီး collections အောက်မှာ ကိုယ်သက်ဆိုင်ရာ အလိုက်ခွဲ သိမ်းပါတယ်.
https://docs.ansible.com/ansible/latest/collections/index.html
aws အတွက် ansible modules ကို amazon collections ထဲမှာ ထဲ့ထားပါတယ်.
https://docs.ansible.com/ansible/latest/collections/amazon/aws/index.html#plugins-in-amazon-aws
ansible ad-hoc အတွက် docs ကို အောက်ပါ link မှာကြည့်ပါ.
https://docs.ansible.com/ansible/latest/user_guide/intro_adhoc.html
ansible playbook
ansible playbook ရေးဖို့ အတွက် ကျွန်တော်တို့ YAML syntax ကို နားလည်ဖို့ လိုပါတယ်. YAML ကို configuration management language အဖြစ်အသုံးများပါတယ်. XML, JSON တို့ထက် ရေးရတာ လွယ်ပါတယ်.
Ansible ကိုနားလည်ဖို့ JSON syntax ကိုလဲ နားလည်ဖို့လိုပါတယ်.
အရင်ဆုံး ကျွန်တော်တို့ YAML syntax ကို အရင်ကြည့်ရအောင်.
Ansible playbook တွေမှာ အသုံးများတဲ့ YAML data types တွေက list နဲ့ dictionary ပါ.
YAML file ကိုစရေးရင် — စပြီး … နဲ့ အဆုံးသတ်ပါတယ်, … ကိုမထည့်လဲရပါတယ်.

YAML file တွေက list data types နဲ့စရေးတာများပါတယ်. list ထဲမှာပါတာတွေက key=value pairs (DICTIONARY) တွေများပါတယ်.
yaml list and dictionary
yaml list တွေကို ansible playbook မှာရေးရင် ” – ” နဲ့စရေးပြီး Identation level လဲတူပါတယ်. YAML က Identation character အဖြစ် space character ကိုသုံးပါတယ်. tab to space ကို ကိုယ်ကြိုက်ရာ IDE မှာ ပြောင်းလို့ရပါတယ်.
တစ်ကယ်လို့ YAML playbook ကို VIM Editor မှာ သုံးချင်ရင် အောက်ပါအတိုင်းပြောင်းလို့ရပါတယ်.
vimrc file ကို user home directory မှာ create လုပ်ပါ. vimrc file မှာ 1 tab = 2 space အနေနဲ့ ပြောင်းလို့ရပါတယ်.
echo "autocmd FileType yaml setlocal ai ts=2 sw=2 et" > ~/.vimrc
YAML list ရေးပုံကို နမူနာ ကြည့်ပါ.

YAML Dictionary ရေးပုံကို နမူနာကြည့်ပါ. YAML Dictionary က key: value ပုံစံနဲ့ရေးလို့ရပါတယ်. “:” နောက်မှာ space character လိုပါတယ်.

YAML List ထဲမှာ YAML Dictionary ကို ထည့်ရေးလဲရပါတယ်. YAML Dictionary ထဲမှာလဲ YAML list ကိုထည့်ရေးလို့ရပါတယ်.

ansible playbook မှာ Boolean values ( true/false ) ကိုလဲသုံးပါတယ်. Boolean values ကို yes, no, true, false ကိုယ်ကြိုက်သလိုရေးလို့ရပါတယ်.

ansible playbook ရေးရာမှာ dictionary value ကရေးရမှာများနေရင် ” | ” and ” > ” character ကိုသုံးလို့ရပါတယ်.
” | “ ကို ” Literal Block Scalar” လို့ ခေါ်ပြီး, ” | “ ကိုသုံးပြီးရေးရင် newlines and trailing spaces character ပါ ပါတယ်.

” > ” ကို ” Folded Block Scalar” လို့ခေါ်ပြီ, ” > “ ကိုသုံးပြီးရေးရင် newlines ကို spaces အနေနဲ့ ပြောင်းပေပါတယ်.

ansible playbook မှာ ” # ” sign ကို comment အနေနဲ့ သုံးပါတယ်.

playbook တွေရဲ့ dictionary value တွေမှာ single ( ” ) or double quote ( ” ” ) သုံးလို့ရပါတယ်.

double quote ( ” ” ) မှာ escape character သုံးလို့ရပါတယ်.

ansible playbook မှာ variables ကိုခေါ်သုံးဖို့ “{{ variable }}” format ကို သုံးပါတယ်. variables ကို inventory or external var file, directory ကခေါ်သုံးလို့ရပါတယ်.

အခြား special character ( [] {} > | * & ! % # ` @
) တွေကို သုံးချင်ရင် quotation mark နဲ့သုံးပါ.
boolean values , floating-point values တွေမဟုတ်ဘဲ သာမန် ဘဲသုံးချင်ရင် quotation mark နဲ့သုံးပါ.


ကျွန်တော်တို့ ansible sample playbook တစ်ခုရေးကြည့်ရအောင်. playbook file ကို .yaml or .yml နဲ့ save ပါ.
vim test.yaml
---
- name: query ansible_facts
hosts: all
tasks:
- name: use debug module
debug:
msg: "{{ ansible_facts['fqdn'] }}"

ansible playbook ရဲ့ name ကို သတ်မှတ်တာကောင်းပါတယ်, မသတ်မှတ်လဲရပါတယ်.
hosts: all က ansible က managed လုပ်မဲ့ hosts or groups ကိုသတ်မှတ်တာပါ. ansible inventory file မှာ hosts or groups ကို ကြည့်ပါ.
tasks: က ကိုယ် automate လုပ်ချင်တာတွေကို ansible module သုံးပြီးရေးရပါတယ်.
ansible playbook ထဲမှာ multiple play ရေးလို့ရပါတယ်.

desired state and idempotency
ansible modules တွေက automate လုပ်မဲ့ resources တွေကို အရင် check လုပ်ပါတယ်, တစ်ကယ်လို့ လိုချင်တဲ့ desired state ဖြစ်ပြီးတာဆိုရင် action မယူပါဘူး.
အဲလို ansible modules တွေကို idempotent ဖြစ်တယ်လို့ သတ်မှတ်လို့ရပါတယ်.
ansible modules တိုင်တော့ idempotent မဖြစ်ပါဘူး, modules တွေကို idempotent ဖြစ်မဖြစ်အရင်းစမ်းတာပိုကောင်းပါတယ်.
ansible playbook ကို run ကြည့်ရအောင်.
ansible playbook ရဲ့ syntax ကို မ run ခင်အရင် check လုပ်ပါ.
ansible-playbook --syntax-check test.yaml
ansible-playbook test.yaml

ansible playbook ကို localhost မှာ local connection နဲ့ test run လုပ်လို့ရပါတယ်.
ansible-playbook test.yaml -c local -i "localhost,"

ansible engine နောက်ကွယ်က run တာကိုကြည့်ချင်ရင် verbosity ထည့် run ပါ. debugging အနေနဲ့သုံးလို့ရပါတယ်.
- v = the output data is displayed
- vv = both output and input data are displayed
- vvv = includes information about connections to managed hosts
- vvvv = include additional info such scripts that are executed on each
remote host, and the user that is executing each script
တစ်ကယ်လို့ ansible engine နောက်ကွယ်က run တာကိုအသေးစိတ်သိချင်ရင် verbosity မြင့်ပါ.
ansible-playbook test.yaml -v
ansible-playbook test.yaml -vvvv ( increase verbosity for details )
YAML Syntax အတွက် docs ကို အောက်ပါ link မှာကြည့်ပါ.
https://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html
Ansible Playbook အတွက် docs ကို အောက်ပါ link မှာကြည့်ပါ.
https://docs.ansible.com/ansible/latest/user_guide/playbooks_intro.html
Ansible ကို ဆက်လေ့လာလိုလျှင်.
https://www.ansiblefordevops.com/
https://www.jeffgeerling.com/blog/2020/ansible-101-jeff-geerling-youtube-streaming-series