Ansible 101 – Ansible Play Recap And Return Values

ကျွန်တော်တို့ ansible modules တွေကို run လို့ ရလာတဲ့ result တွေကို ansible playbook မှာပြန်သုံးဖို့ ansible engine, modules တွေရဲ return values တွေကိုနားလည်ဖို့ လိုပါတယ်.

ansibel engine, modules တွေကို run တဲ့အခါမှာ ရလာတဲ့ data တွေကို ကျွန်တော်တို့ variables အနေနဲ့ register လုပ်လို့ရပါတယ်, ansible playbook ရဲ့ output မှာလဲ tasks တွေရဲ့ state ကိုပြပါတယ်.

ansible modules တွေမှာ မတူညီတဲ့ return values တွေရှိပါတယ်.

ansible modules တွေရဲ့ return values ကို ansible-doc နဲ့ ကြည့်လိုရပါတယ်.

ansible copy module ရဲ့ return values နမူနာကိုကြည့်ပါ.

 ansible-doc copy

ansible modules တွေမှာ common ဖြစ်တဲ့ return values တွေကိုကြည့်ပါ.

Image by Red Hat

တစ်ချို့ return values keys တွေက ansible engine ကသတ်မှတ်တာပါ.

ansible playbook မှာ အတွေ့များတဲ့ return values တွေကိုကြည့်ပါ.

changed

changed က boolean တစ်ခုပါ, ကျွန်တော်တို့ ansible playbook မှာသတ်မှတ်ထားတဲ့ tasks က remote hosts မှာ changed လုပ်မလုပ်ကို ပြတာပါ.

    "changed": true

ansible playbook ကို run ပြီး ပြန်လာတဲ့ return status ကိုကြည့်ပါ.

တစ်ကယ်လို့ ကျွန်တော်တို့ ansible playbook မှာသတ်မှတ်ထားတဲ့ tasks က remote hosts မှာ changed မဖြစ်ရင်အောက်ပါအတိုင်းပြပါတယ်.

Play Recap

PLAY RECAP က ansible plays, tasks, modules arguments တွေရဲ့ results တွေကို ansible control node မှာ ansible engine က output အနေနဲ့ ဖော်ပြတာ ကိုပြောတာပါ.

PLAY RECAP တွေမှာ ansible engine က STDOUT အနေနဲ့ default ပြတဲ့ status တွေက,

ok, changed, unreachable, failed, skipped, rescued, ignored status တို့ဖြစ်ပါတယ်.

status ဆိုတာက ကျွန်တော်တို့ ansible playbook မှာ သတ်မှတ်ထားတဲ့ play, tasks တွေရဲ့ state ကို ပြောတာပါ.

ok status

ok က ကျွန်တော်တို့ playbook မှာသတ်မှတ်ထားတဲ့ tasks တွေက ok state မှာရှိတာကိုပြတာပါ.

  • ok = 4 ဆိုရင် tasks 4 ခုဟာ ok state မှာရှိတာကိုပြတာပါ.
  • playbook မှာသတ်မှတ်ထားတဲ့ tasks တွေက changed state ဖြစ်ပြီးလို့ playbook ကိုပြန် run ရင် ok ဘဲပြမှာပါ.
  • ansible gather_facts ကလဲ tasks တစ်ခုအနေနဲ့ ပြပါတယ်.

changed status

changed က ကျွန်တော်တို့ playbook ထဲက tasks တွေက automate လုပ်မဲ့ resources တွေမှာ changed state ဖြစ်မဖြစ်ကိုပြတာပါ.

unreachable status

unreachable က ကျွန်တော်တို့ automate လုပ်မဲ့ resources ေတွကို ( SSH, WinRM, DNS error ) ကြောင့် connect လုပ်လို့ရမရကို ပြတာပါ.

failed status

failed က ကျွန်တော်တို့ automate လုပ်ဖို့ playbook မှာသတ်မှတ်ထားတဲ့ tasks တွေက fail ဖြစ်တာကို ပြတာပါ.

skipped status

skipped က ကျွန်တော်တို့ automate လုပ်မဲ့ tasks တွေက remote hosts တွေမှာ changed ဖြစ်ပြီးတာ, ဒါမှမဟုတ် tasks မှာ condition နဲ့ check တာ မကိုက်လို့ skipped ဖြစ်တာကိုပြတာပါ.

rescued status

rescued က ansible playbook ကို error handling အတွက် block rescue clause နဲ့ run တဲ့ အခါမှာပြပါတယ်.

  • ansible block က python ရဲ့ { try / except / finally ) statement နဲ့ concept တူပါတယ်.

ကျွန်တော်တို့ ansible block ကို playbook မှာသုံးတာကို ကြည့်ပါ.

တစ်ကယ်လို့ ansible block မှာ rescue block ကို run ရင် ansible playbook ရဲ့ play recap မှာ rescued state ကိုပြမှာပါ.

---
- name: use ansible block 
  hosts: all
  tasks:
  - name: use ansible block and rescue clause
    block:
     - name: Print a message
       ansible.builtin.debug:
         msg: 'I execute normally'

     - name: Force a failure
       ansible.builtin.command: 
        cmd: /usr/bin/false

     - name: Never print this
       ansible.builtin.debug:
         msg: 'I never execute, due to the above task failing, :-('
    rescue:
     - name: Print when errors
       ansible.builtin.debug:
         msg: 'I caught an error, can do stuff here to fix it, :-)'

ကျွန်တော်တို့ ansible playbook ကို run ကြည့်ရအောင်.

 ansible-playbook block.yaml

ignored status

ignored ကို ansible playbook ရဲ့ tasks တွေမှာ errors ဖြစ်လဲ playbook ဆက် run လို့ရအောင် ရေးတဲ့ အခါမှာ error tasks တွေ ignore ဖြစ်တာကို ပြတာပါ.

  • ansible playbook ကို procedural-style နဲ့ ရေးတဲ့ အခါမှာ task တစ်ခုက fail ဖြစ်ရင် next task ကို ဆက်မ run ပါဘူး
  • error ဖြစ်တဲ့ task ကို ကျွန်တော်တို့ ignore လုပ်လို့ရပါတယ်.
  • error ဖြစ်နိင်တဲ့ task မှာ ( ignore_errors: True ) keyword ကို ထည့်ပေးရပါတယ်.
  • error ဖြစ်တဲ့ task က ansible playbook run တဲ့အချိန်မှာ ignore ဖြစ်ရင် ansible က ignored state ကို output မှာပြပါတယ်.

return values

ansible modules တွေရဲ့ return values တွေက မတူပါဘူး. retun values တွေမှာ common ဖြစ်တာတွေတော့ရှိပါတယ်.

common ဖြစ်တဲ့ return values တွေကိုကြည့်ပါ.

ကျွန်တော်တို့ ansible modules တွေရဲ့ return values တွေကို ansible-doc နဲ့ကြည့်လိုရပါတယ်.

တစ်ကယ်လို့ ansible modules ရဲ့ return values ကို ansible playbook မှာပြန်သုံးချင်ရင် register keyword ကိုသုံးပြီး variables အနေနဲ့ သုံးလို့ရပါတယ်.

ansible playbook မှာ return values ကိုသုံးတာကို နမူနာကြည့်ပါ.

 vim return_values.yaml
---
- name: check return values
  hosts: all
  tasks:
    - name: use command module to check return values
      command:
        argv: 
         - /usr/bin/true
      register: return_values

    - name: use debug module to check return values
      debug:
        msg: "{{ return_values.rc }}"

ကျွန်တော်တို့ ansible playbook ကို run ကြည့်ရအောင်.

 ansible-playbook return_values.yaml

ကျွန်တော်တို့ ansible modules တွေမှာ common တွေ့ရတဲ့ return values တွေကို အောက်ပါ links မှာကြည့်ပါ.

https://docs.ansible.com/ansible/latest/reference_appendices/common_return_values.html#results

ansible ကိုဆက်လေ့လာလိုလျှင်.

https://www.ansiblefordevops.com/

https://www.jeffgeerling.com/blog/2020/ansible-101-jeff-geerling-youtube-streaming-series

Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *