Ansible 101 – JSON Usage In Ansible

ကျွန်တော်တို့ Ansible ကိုလေ့လာတဲ့အခါမှာ JSON Syntax and Data Types တွေကို နားလည်ဖို့လိုပါတယ်. Ansible က Ansible ad-hoc and playbook တွေရဲ့ results တွေကို JSON Syntax နဲ့ဖော်ပြပါတယ်. Ansible variables file ကိုလဲ JSON နဲ့ရေးလို့ရပါတယ်.

JavaScript Object Notation ( JSON ) က lightweight data-interchange format ဖြစ်ပြီး language-independent data format ဖြစ်လို့ အသုံးများပါတယ်. programming languages တွေမှာ JSON နဲ့ intergrate လုပ်ဖို့ library များပါတယ်.

JSON ကို data တွေကိုသိမ်းပြီး network ကတဆင့်ပြန်ပို့တဲ့အခါမှာသုံးရတာအဆင်ပြေပါတယ်. JSON က data serialization and deserialization မှာအဆင်ပြေပါတယ်.

Ansible Modules တွေကို python နဲ့ရေးတာများပါတယ်. python code တွေကို automate လုပ်မဲ့ resources တွေစီကို transfer လုပ်တဲ့အခါမှာ python data serialization and deserialization ကိုသုံးပါတယ်.python data types ကို JSON data types အနေနဲ့ပြောင်းတာကိုပြောတာပါ.

Python Serialization & Deserialization
Image by www.i2tutorials.com
Image by www.pythontic.com

JSON ကို server, web application, API, and database တွေမှာ data exchange လုပ်တဲ့နေရာမှာသုံးတာများပါတယ်. XML ကိုသုံးလဲရပါတယ်. JSON ကဖတ်ရတာပိုလွယ်ပါတယ်.

JSON Syntax တွေက JavaScript Syntax နဲ့တော်တော်တူပါတယ်.

ကျွန်တော်တို့ JSON Data Types တွေကိုအရင်ကြည့်ရအောင်.

json data types

json data တွေကို key,value, key/value pair syntax နဲ့ရေးတာများပါတယ်.

json data types တွေက array, boolean, number,null, object and string တို့ဖြစ်ပါတယ်.

json syntax နမူနာကိုကြည့်ပါ.

"foo" : "bar"

“foo” က json key ဖြစ်ပြီး အမြဲတမ်း string data type ပါ, quotation mark ( ” ” ) နဲ့ရေးရပါတယ်.

“bar” က json value ဖြစ်ပြီး, data types အနေနဲ့ ” string, number, boolean, null, array, and object” တို့ကိုသုံးလို့ရပါတယ်, quotation mark ( ” ” )လိုပါတယ်.

“foo” : “bar” တစ်ခုလုံးကို json key/value pair အနေနဲ့ရေးလို့ရပါတယ်. key/value pair က တစ်ခုထက်ပိုရင် comma ( , ) ထည့်ရေးရပါတယ်.

ကျွန်တော်တို့ ansible မှာ အများဆုံးတွေ့ရတဲ့ json data type တွေက json object and array ဖြစ်ပါတယ်.

json object

json object က python dictionary နဲ့ တူပါတယ်.json object တွေကို key/value pairs နဲ့ရေးပြီး, curly bracket ( { } ) ထဲထည့်ရေးရပါတယ်. တစ်ကယ်လို့ key/value pairs က တစ်ခုထက်ပိုရင် comma ( , ) ထည့်ပေးရပါတယ်.

json object ကိုနမူနာကြည့်ရအောင်.

{ "name": "ansible", "group": "sudo" }

json object values ထဲမှာ nested object တွေထည့်ရေးလို့ရပါတယ်.

"foo" : {
  "bar" : "Hello",
  "club" : "World"
  }

nested object values တွေကို နည်းနှစ်မျိုးနဲ့ access လုပ်လို့ရပါတယ်.

dot(.) notation and bracket ( [] ) notation တို့ဖြစ်ပါတယ်.

foo.bar            ( dot notation )
foo["bar"]         ( bracket notation )

json array

json array data type က javaScript array, python list တို့နဲ့တူပါတယ်.

array values တွေမှာ string, number, object, array, boolean, null တို့သုံးနိင်ပါတယ်.

json array ကို square bracket ( [] ) ထဲထည့်ရေးရပါတယ်.

json array ကို json object ရဲ့ values နေရာမှာသုံးတာများပါတယ်.

json array နမူနာကိုကြည့်ပါ.

[ "ansible", "101", "series" ]
"ansible_facts": {
           "name": "ansible",
           "id": "1003",
           "groups": [ "ansible", "sudo" ]
}

json array values ကို index number သုံးပြီး access လုပ်လို့ရပါတယ်.

json array values ကိုလဲ dot(.) and bracket notation နဲ့ access လုပ်လို့ရပါတယ်.

ansible_facts.groups[0]       ( dot notation )
ansible_facts['groups'][0]    ( bracket notation )

ansible ad-hoc and playbook တွေရဲ့ result ကို json syntax နဲ့ ဖော်ပြတဲ့အခါမှာ nested object and array ကို သုံးတာများပါတယ်.

ကျွန်တော်တို့ ansible setup module ကို သုံးပြီး managed hosts ရဲ့ ansible_facts ( variables ) ကို access လုပ်တာကိုလေ့လာကြည့်ပါ.

ansible worker1.example.com -m setup -a "filter=ansible_*">ansible_facts.json
vim ansible_facts.json

ansible ad-hoc and playbook တွေရဲ့ results တွေကို နားလည်ဖို့ JSON object and array ရဲ့ nested data structure ကိုနားလည်ဖို့လိုပါတယ်.

nested data structure ဆိုတာက json object values ထဲမှာ json array ရှိပြီး, json array ထဲမှာ json object or array ကိုသုံးတာကိုပြောတာပါ.

ansible ad-hoc or playbook တွေရဲ့ result ကို json syntax နဲ့ ပြတာကိုကြည့်ပါ.

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 *