ကျွန်တော်တို့ ansible မှာ playbook တစ်ခုမှာ သတ်မှတ်ထားတဲ့ play, tasks, vars, file, template တွေကို အခြား playbook တွေမှာ ပြန်သုံးချင်တဲ့အခါ, sharing လုပ်ချင်တဲ့အခါမှာ ansible Roles ကိုသုံးပါတယ်.
playbook တစ်ခုထဲကို အခြား playbook ထဲမှာ ပြန်သုံးတဲ့အခါမှာ import_tasks နဲ့က အဆင်ပြေပါတယ်, ဒါပေမဲ့ multiple playbook, vars file, template file တွေကို အခြား playbook တွေမှာ ပြန်သုံးချင်ရင်တော့ ansible role ကပို အဆင်ပြေပါတယ်.
ကျွန်တော်တို့ ansible roles တစ်ခု create လုပ်ပြီး တူညီတဲ့ resources တွေကို automate လုပ်ချင်တဲ့ အခါမှာ create လုပ်ထားတဲ့ role ကိုပြန်သုံးလို့ရပါတယ်.
ansible roles ကို သုံးပြီး ကျွန်တော်တို့ automate လုပ်ထားတဲ့ configuration management procedures တွေကို အခြားလူတွေကို sharing လုပ်ရတာလွယ်ပါတယ်.
ansible role ကို နေရာနှစ်ခု မှာသတ်မှတ်လို့ရပါတယ်.
project directory မှာ roles/ ဆိုပြီး directory တစ်ခု create လုပ်ပြီး role ကိုသတ်မှတ်လို့ရပါတယ်.
mkdir roles
ဒါမှမဟုတ်ရင် /etc/ansible/roles မှာ role ကို သတ်မှတ်လို့ ရပါတယ်.
mkdir /etc/ansible/roles
ansible role ကို အခြားနေရာမှာ သတ်မှတ််ထားရင်လဲ ansible.cfg file မှာ roles_path နဲ့ role လမ်းကြောင်းကိုသတ်မှတ်လို့ရပါတယ်.
vim ansible.cfg
roles_path=./roles
Role Directory Structure
ansible role မှာ ကြိုသတ်မှတ်ထားတဲ့ directory structure ရှိပါတယ်.

ansible က ansible role directory တွေထဲမှာရှိတဲ့ main.yml file တွေကို default အနေနဲ့ သုံးပါတယ်.
ansible role ထဲမှာပါတယ် directory တွေရဲ့ တည်ဆောက်ပုံကိုကြည့်ပါ.
- tasks directory အောက်မှာ role ရဲ့ main tasks တွေကို သတ်မှတ်ထားပါတယ်, ansible playbook မှာ role ကိုသုံးတဲ့အခါမှာ tasks/main.yml မှာ သတ်မှတ်ထားတဲ့ tasks တွေကို run မှာပါ.
- handlers directory အောက်မှာ roles ရဲ့ handlers ( inactive tasks ) တွေကိုသတ်မှတ်ထားပါတယ်.
- library directory အောက်မှာ ကိုယ်သတ်မှတ်လိုတဲ့ modules ( library ) တွေကိုသတ်မှတ်လို့ရပါတယ်.
- defaults directory အောက်မှာ roles မှာသုံးတဲ့ variables တွေကိုdefault အနေနဲ့သတ်မှတ်ထားပါတယ်, default directory မှာသတ်မှတ်ထားတဲ့ variables တွေကို ပြောင်းလဲရတာလွယ်ပါတယ်, variables priority ကိုကြည့်ပါ.
- vars directory အောက်မှာ roles မှာသုံးတဲ့ variables တွေကို သတ်မှတ်လို့ရပါတယ်, vars directory မှာသတ်မှတ်ထားတဲ့ variables တွေကိုပြောင်းလဲရတာ defaults directory မှာလောက်မလွယ်ပါဘူး, variables priority ကိုကြည့်ပါ.
- files directory အောက်မှာ roles မှာသုံးတဲ့ files ( configuration, text files ) တွေကိုသတ်မှတ်လို့ရပါတယ်.
- templates directory အောက်မှာ roles မှာသုံးတဲ့ Jinja2 Template files တွေကို သတ်မှတ်လို့ရပါတယ်.
- meta directory အောက်မှာ roles ကိုသုံးတဲ့အခါမှာလိုအပ်တဲ့ dependencies တွေကိုသတ်မှတ်လို့ရပါတယ်.
tasks, vars directory အောက်မှာ YAML file တွေကို resources ရဲ့ platform အလိုက်သတ်မှတ်လို့ရပါတယ်.
ကျွန်တော်တို့ automate လုပ်မဲ့ resources က linux server ဆိုရင် tasks directory အောက်မှာ linux distro ပေါ်မူတည်ပြီး playbook တွေကိုခွဲပြီးသတ်မှတ်လို့ရပါတယ်.

ကိုယ် automate လုပ်မဲ့ resources က redhat ဆိုရင် redhat.yml ကို run မှာပါ, debian ဆိုရင်တော့ debian.yml ကို run မှာပါ.
tasks directory အောက်မှာ resources တွေရဲ့ platform အလိုက်ခွဲထားတဲ့ roles တွေကို ansible playbook မှာသုံးတဲ့ အခါမှာ ansible conditionals နဲ့ တွဲသုံးလို့ရပါတယ်.
ansible roles ကို conditionals နဲ့ တွဲသုံးတာကို နမူနာကြည့်ပါ.

ansible conditionals နဲ့ပါတ်သတ်ပြီး ကျွန်တော် sharing လုပ်ထားတဲ့ post ရှိပါတယ်.
ansible role ကို ansible-galaxy command သုံးပြီး create လုပ်လို့ရပါတယ်.
mkdir roles
cd roles
ansible-galaxy role init (role_name)
ansible-galaxy role init webservers


ansible role ကို playbook မှာသုံးတာကိုကြည့်ပါ.ကျွန်တော်တို့ create လုပ်ထားတဲ့ webservers role ကိုသုံးထားပါတယ်.
vim ansible_role.yml
---
- name: install webserver
hosts: dev
roles:
- webservers
ansible-playbook -syntax-check ansible_role.yml
ansible-playbook ansible_role.yml

ansible galaxy က ansible roles, collections တွေကို sharing or distribute လုပ်တဲ့ platform ပါ.
ansible galaxy command ကို သုံးပြီး sharing လုပ်ထားတဲ့ roles တွေကို download လုပ်လို့ရပါတယ်.
ansible galaxy website မှာကိုယ်လိုချင်တဲ့ role တွေကို category ( cloud, network, security ) အလိုက်ရှာလို့ရပါတယ်.

တစ်ကယ်လို့ ကျွန်တော်တို့က aws နဲ့ပါတ်သတ်တဲ့ ansible role ကို ရှာချင်ရင် galaxy website ရဲ့ search မှာ ရှာပြီး local control node ကို download ဆွဲပြီးသုံးလို့ပါတယ်.

ansible role ကို ansible version 2.10 ကစပြီး ansible collections ထဲမှာထည့်ထားပါတယ်.
ansible version 2.10 မတိုင်ခင်က တော့ ansible role ကို တိုက်ရိုက် install လုပ်လို့ရပါတယ်.
ansible role ကို install လုပ်ချင်ရင် ansible-galaxy collection ကိုသုံးရမှာပါ.
ansible-galaxy collection install amazon.aws
ansible collections
ကျွန်တော်တို့ ansible version 2.10 ကစပြီး ansible playbook, roles, modules and plugins တွေကို ansible collections ကိုသုံးပြီး sharing or distribute လုပ်ပါတယ်, ansible version 2.10 မတိုင်ခင်ကတော့ ansible role ကိုသုံးပါတယ်.
ansible collections က distribution format တစ်ခုပါ. ansible collections ကို ansible galaxy command ကိုသုံးပြီး install လုပ်လို့ရပါတယ်.
ကျွန်တော်တို့ ansible မှာ ansible version 2.10 ကစပြီး ansible roles, collections, modules တွေရဲ့ distribute or maintain လုပ်ပုံကို အပြောင်းအလဲလုပ်ထားပါတယ်.
ansible version 2.10 မတိုင်ခင်က ansible modules တွေကို maintain လုပ်တဲ့ developers တွေက modules တွေမှာ သူတို့ bug fixes လုပ်ထားတာတွေကို ansible မှာ ရဖို့က ansible ရဲ့ release and manitenance cycle ကိုစောင့်ရပါတယ်.
ansible version 2.10 ကစပြီး ansible ကို ansible, ansible-core and collections ဆိုပြီး ခွဲလိုက်ပါတယ်.
- ansible package မှာ ansible-core နဲ့ core modules and plugins, collections အနည်းငယ်ပါပါတယ်.
- ansible-core မှာ ansible-playbook, ansible-docs စသည် ansible ရဲ့ အဓိက CLI tools တွေပါပါတယ်.
- collections မှာ သက်ဆိုင်ရာ category ( cloud, virtualization, network, storage, security ) အလိုက် collections တွေပါပါတယ်.
ansible-core ကို ansible core team က maintain လုပ်ပါတယ်.
https://docs.ansible.com/core.html

collections ကိုတော့ သက်ဆိုင်ရာ contributes လုပ်တဲ့ developers တွေက maintain လုပ်ပါတယ်.
https://docs.ansible.com/collections.html

ansible galaxy
ansible galaxy က ansible roles, collections တွေကို sharing or distribute လုပ်တဲ့ platform ပါ.
ansible galaxy website ကို အောက်ပါ link မှာကြည့်ပါ.
ansible galaxy command ကို သုံးပြီး sharing လုပ်ထားတဲ့ collections တွေကို download လုပ်လို့ရပါတယ်.
ansible galaxy website မှာကိုယ်လိုချင်တဲ့ collections တွေကို category ( cloud, network, security ) အလိုက်ရှာလို့ရပါတယ်.

ansible collections ကို install လုပ်ချင်ရင် ansible-galaxy collection ကိုသုံးရမှာပါ.
ansible-galaxy collection install community.aws
ansible collections ကို install လုပ်တဲ့အခါမှာ local project directory မှာ install မလုပ်ရင် ” ~/.ansible/collections “ directory ကို ansible က default သုံးမှာပါ.
ansible.cfg file မှာလဲ collections_paths ဆိုပြီး collections ကို install လုပ်မဲ့ directory ကိုသတ်မှတ်လို့ရပါတယ်.
vim ansible.cfg

ansible collections ကို local project directory မှာ install လုပ်တာကို နမူနာကြည့်ပါ.
mkdir collections
ansible-galaxy collection install amazon.aws

amazon collections ကို install လုပ်ထားတာကို နမူနာကြည့်ပါ.
ansible collections ကို install လုပ်ရင် collections directory အောက်မှာ ansible_collections ( global_namespace ) ကို ansible က auto-create လုပ်မှာပါ.

ansible collections ကို tarball အနေနဲ့လဲ install လုပ်လို့ရပါတယ်. ansible galaxy website ကကိုယ် install လုပ်ချင်တဲ့ collections ကို tarball အနေနဲ့ download လုပ်ပြီး install လုပ်ရမှာပါ.

ansible-galaxy collection install community-aws-1.5.0.tar.gz -p ./collections
ansible collections ကို requirements.yml ကိုသုံးပြီး install လုပ်လို့ရပါတယ်.
python မှာ requirements.txt ကို pip နဲ့ install လုပ်သလိုပါဘဲ.
pip3 install -r requirements.txt

ansible collections ကို requirements.yml နဲ့ install လုပ်တာနမူနာကြည့်ပါ.
vim requirements.yml
---
collections:
# aws collection offical
- amazon.aws
# aws collection community
- community.aws
ansible-galaxy collections install -r requirements.yml


ansible collections ကို ansible playbook မှာခေါ်သုံးတဲ့ အခါမှာ FQCN ( fully qualified collection name ) ကိုသုံးရပါတယ်.
FQCN ( fully qualified collection name ) မှာ အပိုင်းသုံးခုပါပါတယ်.
- namespace ( collection ကို maintain လုပ်တဲ့ developers or organization ရဲ့ name ပါ )
- name of the collection ( collection ရဲ့ name ပါ )
- name of the module, role, plugin ( collection ထဲမှာ ပါတဲ့ module, role, plugin တွေရဲ့ name ပါ )
amazon.aws collection ကိုနမူနာကြည့်ပါ.
amazon.aws.aws_caller_info
- amazon ( namespace )
- aws ( name of the collection )
- aws_caller_info ( name of the module inside collection )
ansible collections တွေရဲ့ docs ကို အောက်ပါ links မှာကြည့်ပါ.
https://docs.ansible.com/ansible/latest/collections/index.html
amazon collections ကို ansible playbook မှာသုံးပုံကို docs မှာကြည့်ပါ.
https://docs.ansible.com/ansible/latest/collections/amazon/aws/index.html#plugins-in-amazon-aws
ansible collections ကို ansible playbook မှာသုံးတာကို နမူနာကြည့်ပါ. aws collections ကိုသုံးထားပါတယ်.
aws collections ကိုသုံးဖို့ control node မှာ aws cli and aws programmatic access ကိုသတ်မှတ်ထားဖို့လိုပါတယ်.
aws ကို ansible နဲ့ automate လုပ်တာကို ကျွန်တော်အရင်က sharing လုပ်ထားတာ ရှိပါတယ်.
vim aws_collections.yml
---
- name: use ansible collections
hosts: localhost
collections:
- amazon.aws
tasks:
- name: Get the current caller identity information
amazon.aws.aws_caller_info:
register: caller_info
- name: use debug module
debug:
var: caller_info

ansible collections ကိုကိုယ်တိုင် create လုပ်ပြီး sharing လုပ်ချင်လဲရပါတယ်.
ansible galaxy command ကိုသုံးပြီး create လုပ်ရမှာပါ.
collection ကို create လုပ်တဲ့အခါမှာ ” <namespace>.<collection_name> ” format ကိုသုံးရမှာပါ.
local.webserver
local က namespace ပါ, webserver က collection ရဲ့ name ပါ.
webserver collection ထဲမှာ ကျွန်တော်တို့ sharing လုပ်ချင်တဲ့ playbooks modules, roles, plugins တွေကိုသတ်မှတ်ရမှာပါ.
ansible-galaxy collection init local.webserver --init-path ./collections/ansible_collections

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