Ansible 101 – Ansible Plugins

ကျွန်တော်တို့ ansible ကို ansible plugins တွေနဲ့တွဲသုံးလို့ရပါတယ်. ansible plugins က ansible core မှာ arguments အနေနဲ့သုံးလို့ရတဲ့ code တွေပါ.

ansible plugins ကို python နဲ့ရေးထားပါတယ်. ansible plugins ကိုကိုယ်တိုင်ရေးချင်လဲရပါတယ်.

ansible က plugins architecture ကိုသုံးထားလို့ ansible နဲ့လုပ်နိင်တဲ့ features တွေကိုကျွန်တော်တို့ က upgrade လုပ်လို့ရပါတယ်.

ansible မှာ built-in ပါတဲ့ plugins တွေကိုနမူနာကြည့်ပါ.

Image by www.ansible.com

ansible built-in plugins တွေကသက်ဆိုင်ရာအပိုင်းအလိုက်အသုံးဝင်ပါတယ်.

ကျွန်တော် sharing လုပ်ချင်တာက Jina2 ( templating language ) နဲ့တွဲသုံးရတဲ့ plugins ( Lookup, Filters, Test ) တို့အကြောင်းပါ.

Jinja2 နဲ့ပါတ်သက်ပြီး ကျွန်တော်အရင်က sharing လုပ်ထားတာရှိပါတယ်.

ansible plugins တွေရဲ့ document ကိုအောက်ပါ link မှာကြည့်ပါ.

ansible plugins တွေကို ရေးထားတဲ့ python code တွေကို control node ရဲ့ အောက်ပါ directory မှာသိမ်းထားပါတယ်.

cd /usr/lib/python3/dist-packages/ansible/plugins
ls -l

ကျွန်တော်တို့ Jinja2 နဲ့တွဲသုံးတဲ့ plugins တွေရဲ့အကြောင်းကိုကြည့်ရအောင်.

lookup plugins

lookup plugins က ansible မှာ Jinja2 နဲ့တွဲသုံးဖို့လုပ်ထားတဲ့ built-in plugins ပါ. lookup plugins ကိုရေးထားတဲ့ python code တွေကို အောက်ပါ link မှာကြည့်ပါ.

ကျွန်တော်တို့ lookup plugins ကိုသုံးပြီး outside data sources ( files, databases, key/value stores, APIs, etc…) တွေကို playbook and ansible ad-hoc command မှာ ခေါ်သုံးလို့ရပါတယ်.

lookup plugins ကို ansible မှာ Jinja2 templating နဲ့သုံးတဲ့နေရာတွေမှာသုံးလို့ရပါတယ်. ansible play, variables file, Jinja2 template တို့မှာသုံးလို့ရပါတယ်.

lookup plugins က control node မှာဘဲ run ပါတယ်, remote node မှာ မ run ပါဘူး.

lookup plugins ထဲမှာပါတဲ့ plugins တွေရဲ့ documentကို commandline ကိုသုံးပြီးကြည့်လို့ရပါတယ်.

ansible-doc -t lookup -l

ကျွန်တော်တို့ lookup plugins ရဲ့ file plugins ကိုကြည့်ရအောင်.

ansible-doc -t lookup file

lookup plugins ရဲ့ file plugins ကို ရေးထားတဲ့ python code ကို အောက်ပါ directory မှာလေ့လာလို့ရပါတယ်.

cd /usr/lib/python3/dist-packages/ansible/plugins/lookup
vim file.py

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

ansible authorized_key module နဲ့သုံးထားပါတယ်.

vim authorized_key.yaml
---
- name: use lookup plugins
  hosts: all
  tasks:
    - name: Set authorized key in alternate location
      authorized_key:
        user: devops
        state: present
        key: "{{ lookup('file', 'file/id_rsa.pub') }}"
        path: /etc/ssh/authorized_keys/devops
        manage_dir: False

lookup plugins ထဲက file plugin ကိုသုံးပြီး devops user ရဲ့ public key ကို managed node တွေကို copy လုပ်ထားတာပါ.

lookup plugins ထဲက file plugin ကိုသုံးပြီး ansible control node မှာရှိတဲ့ file တွေကိုခေါ်သုံးလို့ရပါတယ်.

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

ansible-playbook --syntax-check authorized_key.yaml
ansible-playbook authorized_key.yaml

ကျွန်တော်တို့ managed nodes တွေကို devops user ရဲ့ ssh public key ကို copy ကူးတာရောက်မရောက်ကိုစမ်းပါ.

su - devops
ssh worker1.example.com

lookup plugins ကိုသုံးပြီးလုပ်လို့ရတာတွေကအများကြီးပါ.

lookup plugins ရဲ့ document ကို အောက်ပါ link မှာကြည့်ပါ.

filters plugins

ကျွန်တော်တို့ filters plugins ကိုသုံးပြီး JSON data တွေကို အခြား data type ( YAML ) ကိုပြောင်းလို့ရပါတယ်.

Data types ( string, list, dictionary ) တွေကိုပြောင်းတဲ့နေရမှာလဲသုံးပါတယ်. list data type ကနေ dictionary data type ကို filters plugins ကိုသုံးပြီးပြောင်းလို့ရပါတယ်.

URL link ထဲက hostname တွေကို ဆွဲထုတ်တဲ့နေရာမှာလဲသုံးလို့ရပါတယ်. ကျွန်တော်တို့ string တွေကို SHA1 hash အနေနဲ့ပြောင်းတဲ့နေရာမှာလဲသုံးလို့ရပါတယ်.

filters plugins က pipe character ( ” | ” ) ကိုသုံးပါတယ်.

Image by www.ansible.com

ကျွန်တော်တို့ filters plugins ကို ansible playbook မှာသုံးကြည့်ရအောင်.

vim filter_plugins.yaml
---
- name: use filters plugins
  hosts: dev
  tasks:
    - name: Add the user 'htunn' with a bash shell, appending the group 'sudo'  to the user's groups
      user:
        name: htunn
        shell: /bin/bash
        password: "{{ 'password' | password_hash('sha512') }}"
        groups: sudo
        append: yes

ကျွန်တော်တို့ htunn ဆိုရဲ့ user ရဲ့ password ( string data type ) ကို filter plugins ကိုသုံးပြီး sha512 hash အနေနဲ့ပြောင်းပြီး သတ်မှတ်ထားပါတယ်.

user ရဲ့ password နေရာမှာ variables ကိုသုံးလို့ရပါတယ်.

vim filter_plugins.yaml
---
- name: use filters plugins
  hosts: prod
  vars:
    - password: "htunn"
  tasks:
    - name: Add the user 'htunn' with a bash shell, appending the group 'sudo' to the user's groups
      user:
        name: thuthu
        shell: /bin/bash
        password: "{{ '${password}' | password_hash('sha512') }}"
        groups: sudo
        append: yes

တကယ်လို့ password နေရာက sensitive ဖြစ်ရင် ansible vault နဲ့သုံးလဲရပါတယ်.

ansible vault နဲ့ပါတ်သက်ပြီး ကျွန်တော် အရင်က sharing လုပ်ထားတာရှိပါတယ်.

filter plugins ကိုသုံးပြီးကျွန်တော်တို့ လုပ်လို့ရတာတွေကများပါတယ်.

filter plugins ရဲ့ document ကိုအောက်ပါ link မှာကြည့်ပါ.

test plugins

ကျွန်တော်တို့ test plugins ကို ansible မှာ Jinja2 template expressions တွေရဲ့ True or False results တွေကို check လုပ်တဲ့နေရမှာသုံးပါတယ်.

ansible test and filters plugins ရဲ့ မတူညီတဲ့အချက်က test ကို comparisons အတွက်သုံးပါတယ်, filters plugins ကို data manipulation ( JSON -> YAML ) အတွက်သုံးပါတယ်.

Jinja2 expressions တွေက python expressions တွေကိုနားလည်ရင် အသုံးပြုရတာ အဆင်ပြေပါတယ်.

ကျွန်တော်တို့ Jinja2 expressions တွေကိုနားလည်မှ test plugins ကိုသုံးရတာအဆင်ပြေမှာပါ.

Jinja2 template expressions တွေကိုနမူနာကြည့်ပါ.

  • Math expressions
Image by www.exponea.com
  • Comparison expressions
Image by www.exponea.com
  • Logical expressions
Image by www.exponea.com
  • Other Operators expressions
Image by www.exponea.com

ကျွန်တော်တို့ test plugins ကို ansible မှာသုံးတဲ့ syntax ကိုကြည့်ပါ.

Image by www.ansible.com

is က jinja2 other operators expressions ထဲက operator expressions တစ်ခုပါ.

ကျွန်တော်တို့ ansible conditional ( when ) မှာ condition တွေကို check လုပ်တဲ့နေရာမှာလဲ test plugins ကိုသုံးပါတယ်.

when: results is failed

task တစ်ခုကရလာတဲ့ results ရဲ့ failed or success ကို check လုပ်တဲ့နေရာမှာ test plugins ကိုသုံးထားတာပါ.

ကျွန်တော်တို့ test plugins ကိုရေးထားတဲ့ python code ကိုအောက်ပါ directory မှာကြည့်လို့ရပါတယ်.

cd /usr/lib/python3/dist-packages/ansible/plugins/test
ls -l

ကျွန်တော် တို့ ansible playbook မှာ test plugins ကိုသုံးပြီး tasks တွေရဲ့ results ကို check လုပ်တာကိုနမူနာကြည့်ပါ.

Image by www.ansible.com

test plugins ကိုသုံးပြီးလုပ်လို့ရတာတွေကများပါတယ်.

test plugins ရဲ့ document ကိုအောက်ပါ link မှာကြည့်ပါ.

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 *