Ansible Basic

--

Ansible သည် IT Infrastructure tasks များကို simple text files ဖြင့် automate လုပ်ပေးနိုင်သည့် Infrastructure ‌as code tool တစ်ခုဖြစ်သည်။ Ansible သည် open source platform ဖြစ်ပြီး၊ agentless configuration management system ဖြစ်သည်။ System configuration များရေးရန်အတွက် ကိုယ်ပိုင်ဟန် Declarative Language ပုံစံကိုအသုံးပြုထားပြီး Python base ဖြစ်ကာ၊ powershell, shell, ruby များကိုလည်းသုံးထားသည်။

Ansible သည် သိပ္ပံစိတ်ကူးယဥ် ဇာတ်လမ်းတစ်ခုတွင် အသုံးပြုသည့် communication devide တစ်ခု၏ အမည်ဖြစ်သည်။ ထို device သည် information များကို အလင်းထက်မြန်သော အမြန်နှုန်းဖြင့် ပို့ပေးနိုင်သည်။ စာရေးဆရာမ Ursula K. Le Guin သည် သူ၏စာအုပ်ဖြစ်သော Rocannon’s World တွင် ထို concept ကိုစတင်ရေးခဲ့သည်။ တခြား သိပ္ပံစိတ်ကူးယဥ် ဇာတ်လမ်းများတွင်လည်း ထို idea ကို ထည့်သုံးလာကြသည်။

Ansible tools ကို 2012 တင်စတင် developed လုပ်ခဲ့သည့် Michael DeHaan သည် Ansible ဟူသော အမည်ကို Ender’s Game by Orson Scott Card စာအုပ်မှယူထားခြင်ဖြစ်နိုင်သည်။ ထိုစာအုပ်တွင် ansible သည် များစွာသော remote ships များကို အလွန်ဝေးသော နေရာမှ တစ်ချိန်တည်းထိန်းချုပ်နိုင်သည်။ Ansible သည်လည်းထိုကဲ့သို့ system များကို orchestration လုပ်ပေးနိုင်သည် engine တစ်ခုဖြစ်သည်။ Ansible ကို 2015 တွင် Red Hat ကလွဲပြောင်းယူခဲ့ပြီး၊ Properietary/GNU General Public License အောက်တွင်ရှိသည်။

Ansible’s Architecture

Ansible architecture တွင် control nodes နဲ့ managed hosts ဟူ၍ နှစ်မျိုးရှိသည်။ Control node သည် administrator သုံးသည့် laptop (သို့) Red Hat Ansible Tower တစ်ခုခုဖြစ်နိုင်ပြီး၊ ၄င်းတွင် Ansible ကို installed လုပ်ထားသည်။ managed hosts များသည် ကဏ္ဏအလိုက်စုပေါင်းထားသော server groups များကို inventory တွင် စာရင်းသွင်းထားခြင်းဖြင့် servers များကို လွယ်ကူစွာစီမံ manage နိုင်သည်။ Inventory ၏ configuration များအတွက် YAML, INI format များကိုသုံးထားပြီး၊ configuration များကို ASCII text file ဖြင့်သိမ်းပေးနိုင်ခြင်းနဲ့ external sources များအတွက် information များကိုလည်း dynamically သတ်မှတ်နိုင်သည်။

Ansible သည် လုပ်ငန်းများကိုလုပ်ဆောင်ရန် ရှုပ်ထွေးသော scripts များကို ရေးမည့်အစား၊ modules များ run ရန်အတွက်လိုအပ်သော task များကို hosts များပေါ်တွင် လုပ်ကိုင်ဆောင်ရွက်ပေးနိုင်သည့် high-level plays များကို playbook တွင်‌ရေးနိုင်သည်။ Module သည် system files များ၊ software installation များ (သို့) API calls များ အတွက်အသုံးပြုနိုင်သည့် Python, Perl, Ruby, Bash များဖြင့် ရေးနိုင်သော small programs များဖြစ်သည်။ Ansible တွင်များစွာသော modules များပါ၀င်သည်။

Ansible သည် agentless architecture ဖြစ်ပြီး၊ Cross platform ကိုလည်း support လုပ်သည်။ Ansible control node နဲ့ hosts များအကြား manage လုပ်ရန် OpenSSH (သို့) WinRM ကို အသုံးပြုသည်။ Control node သည် orchestration task များကို လုပ်ဆောင်စဥ်အတွင်း modules များနဲ့ communicates များတွင် input/output များအတွက် JSON-base protocol ကိုသုံးသည်။ Ansible သည် node များကို manage ပြုလုပ်ခြင်းများအတွက် အသုံးမပြုသည့်အခါ resource စာသုံးမှုလည်းနည်းသည်။

Hosts များပေါ်တွင် desired state များအတွက် အသုံးပြုမည့် run tasks များဖြစ်သော moudules များကို ansible မှတဆင့် hosts များပေါ်သို့ push လုပ်ပေးသည်။ ထို run tasks များ ပြီးဆုံးသည့်အခါတွင် pushed လုပ်ထားသော modules များလည်း တခါတည်းဖယ်ရှားပေးသည်။ Agentless ဖြစ်ခြင်းကြောင့် တခြား automate tools များထက် လုပ်ငန်းတွင်ကျယ်ခြင်းနဲ့ ‌agent အယောင်ဆောင် attacking များကိုလျှော့နည်းစေပြီးပိုsecure ဖြစ်စေသည်။

Ansible Tower သည် Enterprise များအတွက် control, secure နဲ့ manage စသော automation များကို scalely လုပ်ဆောင်ပေးနိုင်သည့် framework ဖြစ်သည်။ Playbooks များကို control လုပ်နိုင်ခြင်း၊ SSH credentials များကို အသုံးပြုခြင်း၊ logs များကို သိမ်းပေးခြင်းတို့ကို ပြုလုပ်ပေးသည်။ Ansible Tower ကို အသုံးပြုနိုင်ရန်အတွက် Web based User Interface ကိုပေးထားပြီး၊ API အတွက် RESTful API ကိုသုံးသည်။

Installation and Configuration

Installation အတွက်မိမိ၏ system နဲ့ကိုက်ညီသော installation နည်းများကို သုံးနိုင်သည်။

Fedora, CentOS, RHEL and compatible
# If you're on RHEL/CentOS 7:
$ yum install epel-release
$ yum install ansible
Ubuntu, Debian and compatible
$ sudo apt-add-repository -y ppa:ansible/ansible
$ sudo apt-get update
$ apt-get install ansible
MAC တွင် ansible ကို pip မှ Installing လုပ်နိုင်သည်။
$ Install Homebrew (Homebrew website မှinstallation command ကိုရယူပါ။)
$ brew install python3
$ pip install ansible
Installation from the source code
$ git clone git://github.com/ansible/ansible.git
$ cd ansible
$ sudo make install
Install ပြီးသွားလျှင် version ကိုစစ်ကြည့်ပြီး၊ system တွင်အလုပ်လုပ်ခြင်းကို verify လုပ်ပါ။
$ ansible --version
Python မရှိပါက install လုပ်ရန်
$ yum install python3
Ansible တွင် python version ကိုစစ်ရန်
$ python3 --version
$ ansible -m setup localhost | grep ansible_python_version

Preparations

ဤ Lab အတွက် Control server တစ်လုံး, Host — နှစ်လုံး, ssh connection အတွက် ssh-key, ip address — သုံးခု, VIM editor ကို adjust များကိုပြင်ဆင်ခြင်းတို့ကို ပြုလုပ်သွားပါမည်။

Control server နဲ managed hosts များအကြား ssh connect အတွက် control server တွင် ssh-keygen ကိုထုတ်ပြီး host1 နဲ့ host2 ဆီကို ssh-public ကိုပို့ပေးသည်။ 
ssh-keygen
ssh-copy-id host1 or ipaddress

INVENTORY

Ansible ၏ default Inventory သည် /etc/ansible/hosts တွင်ရှိသည်။ Ansible သည် hosts များကို manage လုပ်နိုင်ရန် Inventory သည် hosts များကိုသတ်မှတ်ပေးသည်။

Inventory တွင် ကဏ္ဏအလိုက်စုပေါင်းထားသော systems groups များရှိသည်။ Groups များတွင် child groups များရှိနိုင်ပြီး၊ hosts များသည်လည်း multiple groups များတွင် members အဖြစ်ရှိနိုင်သည်။

Inventory တွင် variables များကိုလည်းသုံးနိုင်သည်။ Host inventory များကို နှစ်မျိုးရေးနိုင်သည်။ Text files ဖြင့်ရေးရသော static host inventory နဲ့ script ဖြင့် ရေးရသော dynamic host inventory ဖြင့်လည်းရေးနိုင်သည်။

A Static Host Inventory

Simplest Form

Inventory ကို INI format file (သို့) yaml file ဖြင့်ရေးနိုင်သည်။ Static inventory file တွင် managed hosts များကို Listing စီထားသော ip address (သို့) Host names များဖြင့်ရေးနိုင်သည်။ လေးထာင့်ကွင်း[]ကိုအသုံးပြု၍ host များကို Group ဖွဲ့နိုင်သည်။

192.168.1.1
web1.example.com
[Prod]
192.168.1.2

Nested Groups

Ansible host inventories များတွင် groups of host groups များကိုရေးရန် :children suffix ကိုအသုံးပြုနိုင်သည်။ အောက်ပါ ဥပမာ၌ yangon group တွင် sule နဲ့ mingaladon groups များရှိ သည်။

[sule]
192.168.1.1
[mingaladon]
192.168.1.1
[yangon:children]
sule
mingaladon

Simplifying Host Specifications with Ranges

Ansible host inventories ကို ip address နဲ့ hostnames များအတွက် ranges ဖြင့်ရေးလိုပါက square brack အတွင်း full colon [start:end]syntax ကိုသုံး၍ ရေးနိုင်သည်။ အောက်ပါ ဥပမာ၌ sule group တွင် sule1.example.com , sule2.example.com စသဖြင့်ရှိပြီး၊ mingaladon group တွင် 192.168.4.3, 192.168.4.4 ဟူ၍ နံပါတ် (သို့) ‌alphabetic များကို range အတွင်းတွဲပေးသွားမည်။

[sule]
192.168.1.1
web[1:2].example.com
[mingaladon]
192.168.1.2
192.168.1.[3:4]

Inventory ကိုပြန်စစ်ရန်အတွက်

$ ansible all --list-hosts
$ ansible sule --list-hosts
$ ansible sule1.example.com --listhosts

Customize Inventory

mkdir /home/username/deploy-inventory
cd /home/username/deploy-inventory
vim inventory
[Dev]
192.168.1.1
web1.example.com
[Prod]
192.168.1.2

Customize inventory file ကို verify လုပ်ရန်အတွင် -i option ကို အသုံးပြုရမည်။ ထို option သည် default inventory file အစား ‌ansible ကို custom inventory file ကိုအသုံးပြုစေသည်။

$ ansible all -i inventory --list-hosts

CONFIGURATION FILE

Ansible configuration file ကို modify လုပ်ခြင်းအားဖြင့် Ansible ကို customized ပြုလုပ်ကာ အသုံးပြုနိုင်သည်။ Ansible တွင် default configuration file မှာ/etc/ansible/ansible.cfg တွင်ရှိသည်။ Ansible တွင် multiple configuration file များကိုအသုံးပြုနိုင်သည်။ Ansible သည် configuration file ကိုရွေးချယ်ရာ၌ အရင်ဆုံးတွေ့သည့် file ကိုသုံးသည်။ Ansible တွင် config file ကို သတ်မှတ်ရာတွင် — (1) Environment variable ကိုအသုံးပြု၍ ANSIBLE_CONFIG ကို သတ်မှတ်နိုင်သည်။ (2) Current Working Directory တွင် ansible.cfg ကို ./ansible.cfg သတ်မှတ်ကာ Playbook နဲ့ adhoc command များဖြင့်တိုက်ရိုက်အသုံးပြုနိုင်သည်။ (3) ထိုအပြင် Home directory တွင်ရှိသော ansible.cfg ကို ~/.ansible.cfg ‌အသုံးပြု၍ ansible command ဖြင့် အသုံးပြုနိုင်သည်။ (4) တခြား config file များကိုမသတ်မှတ်ထားပါက default ကိုသုံးသည်။

Ansible configuration file တွင် inventory location များ၊ အသုံးပြုမည့် method များနဲ့ အသုံးပြုမည့် user များကို ဖော်ပြသည့် secctions များပါ၀င်သည်။ ထို section များကို key-value pairs များဖြင့်သတ်မှတ်သည်။ Section title များကို square brackets [] ထဲတွင်ထည့်သည်။ configuration file သည် INI format ဖြစ်ပြီး comment marker အတွက် "#" or ";" ကိုသုံးသည်။ Basic operation တွင် အသုံးများသည့် sections နှစ်ခုမှာ — [defaults] နဲ့ [privilege_escalation] ဖြစ်သည်။

default သည် Ansible operation ၏ default လုပ်ငန်းစဥ်ကိုသတ်မှတ်သည်။ privilege_escalation သည် managed hosts များ‌အပေါ်တွင် အသုံးပြုမည့် remote user သည် root ကိုအသုံးပြုနိုင်သည်။ root user မဟုတ်သော unprivileged user များအသုံးပြုပါက၊ privilege escalation များလုပ်ဆောင်ရန်အတွက်သုံးသည်။ အောက်ပါ config သည် ပုံမှန်အသုံးပြုနိုင်သော ansible.cfg တစ်ခုဖြစ်သည်။

[defaults]
inventory = ./inventory
remote_user = user
ask_pass = false
[privilege_escalation]
become = true
become_method = sudo
become_user = root
become_ask_pass = false

Privilege_escalation အတွက် နောက်တနည်းမှာ — Red Hat Enterprise Linux 7တွင် default configuration ဖြစ်သော /etc/sudoers ၏ wheel groups အောက်တွင်ရှိသော users များသည် sudo privileges ကို automatically granted ရကြသည်။ အောက်တွင် ansible ဟူသော user ကို sudo privileges ကို ရစေရန် /etc/sudoers.d တွင်သတ်မှတ်နိုင်သည်။

Ansible control ၏ /etc/sudores.d တွင်သတ်မှတ်ပြီး၊ scp ဖြင့် host1 နဲ့ host2 ဆီသို့ပို့ပေးသည်။ 
vim /etc/sudoers.d/ansible
ansible ALL=(ALL) NOPASSWD:ALL
scp /etc/sudoers.d/ansible host1:/etc/sudoers.d/
scp /etc/sudoers.d/ansible host1:/etc/sudoers.d/

Manage Configuration Files

mkdir ကိုသုံးပြီး home/user အောက်တွင် deploy-manage အမည်ဖြင့် folder တစ်ခုကိုဆောက်ပြီး၊ cd ဖြင့် ထို folder ထဲ့သို့၀င်ထားသည်။
mkdir /home/username/deploy-manage
cd /home/username/deploy-manage
Deploy-manage ထဲတွင် touch ကိုသုံးပြီး ansible.cfg ကိုဆောက်ကာ၊ configuration များကိုသတ်မှတ်သည်။
touch ansible.cfg
vim andible.cfg
[defaults]
inventory = ./inventory
[privilege_escalation]
become = true
become_method = sudo
become_user = root
become_ask_pass = true
Deploy-manage folder အတွင်း inventory တစ်ခုကို ပြုလုပ်ပြီး host group နဲ့ host list မျးကိုထည့်သွင်းထားသည်။
touch /home/username/deploy-manage/inventory
vim inventory
[sule]
192.168.1.1
[mingaladon]
192.168.1.2
ad-hoc command တွင် -i ‌inventory ကို ကြေငြာစရာမလိုတော့ဘဲ inventory အတွင်းသတ်မှတ်ထားသော hosts မျးကိုကြည့်နိုင်သည်။
ansible all --lists-hosts
ansible sule --list-hosts
ansible mingaladon --list-hosts

AD HOC COMMANDS

Ad hoc commands များသည် single ansible task များဖြစ်သော ပေါ့ပါးသည့် လုပ်ငန်းများ quick tests , changes အချို့ကို မြန်ဆန်စွာလုပ်ဆောင်ပေးနိုင်သည့် tools ဖြစ်သည်။ Ad Hoc commands ပုံစံမှာ —

$ ansible [host-pattern] -m [module] [-a 'module arguments'] [-i inventory]

host-pattern သည် ad-hoc command ကို သက်ရောက်စေလိုသည့် managed-host များအတွက် argument ဖြစ်သည်။ eg. inventory file အတွင်းရှိ host တစ်ခု (သို့) host group များကို ညွှန်းလိုသည့်အခါသုံးသည်။ all, host-name (or) groupname, --list-hosts

-ioption သည် configuration အတွင်းရှိ default inventory ကိုမသုံးလိုသည့်အခါ၊ သုံးလိုသော inventory file ကိုညွှန်းရန်သုံးသည်။

-m option သည် targeted hosts များအပေါ်တွင်သုံးမည့် argument များ၏ module name များဖြစ်သည်။ ping, user, command,

-a option သည် string quoted ကဲ့သို စီထားသည့် နောက်ထပ်အသုံးပြုမည့် arguments list များဖြစ်သည်။

Ad Hoc Commands အချို့မှာ —

$ ansible all -m ping
$ ansible localhost -m command -a 'id'
$ ansible mymanagedhosts -m command -a /usr/bin/hostname
$ ansible -m user -a "name=aungaung uid=4000 state=present" web1.example.com

PLAYBOOKS

Ad hoc command များသည် single task များအတွက်တစ်ကြိမ်သာအသုံးပြုနိုင်ပြီး၊ playbook သည် multiple complex tasks များအတွက် သာမက ၄င်း playbook ကိုလည်းထပ်ကာ ထပ်ကာ အသုံးပြုနိုင်သည်။

---
- name: Configure important user consistently
hosts: web1.example.com
tasks:
- name: aungaung exists with UID 4000
user:
name: aungaung
uid: 4000
state: present

Playbook သည် YAML format ‌ဖြင့်ရေးထားသော text file ဖြစ်ပြီး၊ file extension ကို y‌aml/yml အဖြစ်သုံးသည်။ Playbook တွင် data များကို ပိုင်းခြားရန်နဲ့ညွှန်းရန်အတွက် indentation ကို space characters ကိုသုံးသည်။ YAML format ဖြင့်ရေးရန် basic rule နှစ်ခုမှာ — same level ရှိသော data element များသည် same indentation တွင်ရှိရမည်။ — Child item များကို parents items များထက် ပိုသော indentation တွင်ထားရသည်။

Indentation အတွက် space character ကိုသုံးသည်။ သို့သော် tab key ကိုအသုံးပြုခွင့်မပေးပါ။ ထို့ကြောင့် Yaml format ကို Vim ကိုအသုံးပြုပြီး ရေးရန်အတွက်၊ 1 tab key ကို 2-space indentation အဖြစ်သုံးနိုင်အောင် လိုအပ်သလို Adjust လုပ်နိုင်သည်။
vim ~/.vimrc
autocmd filetype yaml setlocal ai ts=2 sw=2 et

YAML ရေးနည်း အခြေခံကို ဤ link တွင် လေ့လာနိုင်ပါသည်။ Playbook များတွင် three dashes--- ကို document အစအတွက်သုံးပြီး၊ three dots ... ကို document အဆုံးသတ်အတွက်သုံးသည်။ Yaml တွင် Dictionary နဲ့ Lists ဟူ၍အခြေခံ ရေးနည်းနှစ်မျိုးရှိသည်။

Yaml တွင် strings အကြား spaces ပါသော strings များကို double quotes (သို့) single quotes များဖြင့်ရေးနိုင်သည်။

this is a string 
'this is another string'
"this is yet another a string"
Multiline strings များကို နှစ်မျိုးဖြင့်ရေးနိုင်သည်။ ပထမနည်းမှာ vertical bar (|) character ကိုအသုံးပြု၍ string များကို new line ခွဲများအဖြစ်ရေးနိုင်သည်။
include_newlines: |
Example Company
123 Main Street
Atlanta, GA 30303
ဒုတိယနည်းမှာ greater-than (>) character ကိုအသုံးပြု၍ စာကြောင်းရှည်များရေးရန်သုံးသည်။
fold_newlines: > This is an example of a long string, that will become a single sentence once folded.

List ကိုရေးရာတွင် single dash - ၏နောက်တွင် space ကိုအသုံးပြုပြီး lists များကို ရေးနိုင်သည်။

- apple
- orange
- grape

List ကို inline format ဖြင့်ရေးရာတွင် square braces အတွင်းရေးနိုင်သည်။

hosts: [servera, serverb, serverc]

Play များအတွက် collection များကို key-value pairs များဖြင့်ရေးနိုင်သည်။YAML တွင် indented block များအတိုင်းရေးထားသော key-paires Collection များကို Dictionary ဟုလည်းခေါ်သည်။ Key များသည် တူညီသော indentaiton တွင်ရှိရမည်။

 name: just an example
hosts: webservers
tasks:
- first
- second
- third

Dictionaries ကို inline block ဖြင့်ရေးလျှင် curly braces အတွင်းရေးရသည်။

{name: just an example, hosts: webservers,tasks:[first, second, third]}

Play အတွက်အခြေခံအားဖြင့် key သုံးမျိုးမှာ name, host, tasks များဖြစ်သည်။ သူတို့သည် တူညီသော indentation တွင်ရှိကြသည်။ Playbook တစ်ခုတွင် အသုံးပြုသည့် Key attribute များ၏အသုံးချမှုများမှာ —

name attribute သည် မဖြစ်မနေအသုံးပြုရန်မလိုသော်လည်း၊ play တစ်ခုအတွက် label အမှတ်အသားတစ်ခုအဖြစ် အသုံးပြုရသည်။ multiple play များစွာရှိသော playbook တစ်ခုအတွက် အလွန်အသုံး၀င်သည်။

- name: Configure important user consistently

hosts attrbute သည် play tasks များကို run ရန် အသုံးပြုမည့် hosts များကို သတ်မှတ် ရာတွင်သုံးသည်။

hosts: web1.example.com

Tasks attribute သည် play တစ်ခုအတွက်အသုံးပြုမည့် taks list များနဲ့ ၄င်းတို့အတွက်သတ်မှတ်ထားသော value များဖြစ်သည်။

tasks:
- name: newbie exists with UID 4000
user:
name: newbie
uid: 4000
state: present

Playbook တစ်ခု၏ task အတွက် အသုံးပြုနိုင်သည့် module များမှာ —

ad-hoc module
ping,yum, file, lineinfile
playbook moudule
copy, service, get_url, archive, unarchive, setup, user, shell, command, replace

Playbook များကို run ရန်အတွက် $ ansible-playbookcommand ကိုသုံးသည်။

vim ကိုအသုံးပြု၍ webserver.yml အမည်ရှိသော file တစ်ခုကိုပြုလုပ်ပြီး play တစ်ခုကိုရေးပါ။ Play task များသည် -name ဖြင့် document ခေါင်းစဥ်ကိုရေးသည်။ hosts တွင် tasks များကို run မည် server ကိုထည့်သည်။ tasks တွင် -name ဖြင့် tasks ၏ခေါင်းစဥ်ကိုရေးသည်။ yum module ကိုသုံးပြီး နောက်ဆုံး latest version ဖြစ်သော http ကို install လုပ်သည်။ 
vim webserver.yml
---
- name: play to setup web server
hosts: web1.example.com
tasks:
- name: latest httpd version installed
yum:
name: httpd
state: latest
ထို playbook ကို run ရန်အတွက် ansible-playbook command ကိုအသုံးပြုကာ webserver.yml ကို run သည်။
$ ansible-playbook webserver.yml
PLAY [play to setup web server] ************************************************
TASK [Gathering Facts] *********************************************************
ok: [web1.example.com]
TASK [latest httpd version installed] ******************************************
changed: [web1.example.com]
PLAY RECAP *********************************************************************
web1.example.com : ok=2 changed=1 unreachable=0 failed=0
ဤ result တွင် Changed ဆိုသည်မျာ host အတွင်း httpd သည် version အ‌ဟောင်းတစ်ခု installed ရှိခဲ့သောကြောင့်၊ task တွင် version အသစ်နဲ့ changed ပေးလိုက်ခြင်း စသည့် task အတွင်း Change ဖြစ်ကြောင်းကိုပြသည်။ Change ဖြစ်သွားသောကြောင့် နောက်တစ်ခါ task ကိုပြန် run လျှင် task ၏လိုခြင်သည့် state အတိုင်းဖြစ်သောကြောင့် ပြောင်းလဲမှုမရှိတော့ဘဲ no change ဖြစ်သည်။ $ ansible-playbook webserver.ymlPLAY [play to setup web server] ************************************************
TASK [Gathering Facts] *********************************************************
ok: [web1.example.com]
TASK [latest httpd version installed] ******************************************
ok: [web1.example.com]
PLAY RECAP *********************************************************************
web1.example.com : ok=2 changed=0 unreachable=0 failed=0

Syntax စစ်ဆေးရန်အတွက် --syntax-checkcommand ကိုသုံးနိုင်သည်။

$ ansible-playbook --syntax-check webserver.yml

Playbook ကို run လိုက်သည့်အခါ output များကို details ကြည့်ရန်အတွက် command တွင် -voption ဖြစ်သောverbosity ကိုသုံးနိုင်သည်။

-v သည် task result ကို ဖော်ပြပေးသည်။
-vv သည် task result နဲ့ task configuration ကိုဖော်ပြသည်။
-vvv သည် managed hosts နဲ့ connection information များကိုကြည့်နိုင်သည်။
-vvvv သည် connection အပြင် scripts များကို အသုံးပြုထားလျှင် scripts များအလုပ်လုပ်သွားသည်ပုံနဲ့ သုံးသွားသည့် scripts များကိုဖော်ပြပေးသည်။

Check Mode /Dry Run ကိုအသုံးပြုခြင်း။

Playbook ကို -c option ကိုအသုံးပြု၍ host များပေါ်တွင် မည်သည့် change များဖြစ်သွားမည်ကို အစမ်းအနေဖြင့် dry run ကိုအသုံးပြု၍ report များကို ရနိုင်သည်။ 
$ ansible-playbook -C webserver.yml
--dff option ဖြင့်လည်းတွဲ၍အသုံးပြုနိုင်သည်။ $ ansible-playbook -C webserver.yml --diff web1.example.com

VARIABLES

Variables များသည် values များကို storeလုပ်ပေးနိုင်ပြီး၊ project အတွင်း ၄င်း file များကို ပြန်သုံးနိုင်သည်။ Variables များကိုသုံးခြင်းဖြင့် project အတွင်း errors များကို နည်းစေပြီး၊ creation ပိုင်းနဲ့ maintenance များအတွက်လည်း ရိုးရှင်းလွယ်ကူစေသည်။

Variables အမည်များကိုသတ်မှတ်ရာတွင် variable အစစာလုံးများသည် letter ဖြစ်ရမည်၊ ၄င်းနောက်တွင် letters, numbers, and underscores များလိုက်နိုင်သည်။

web_server, remote_file, file_1, file1, remote_server_1, remote_server1

Variables များကို ansible project တွင် အမျိုးမျိုးသော ‌ကဏ္ဏများအတွက်အသုံးချနိုင်သည်။ အဓိကအားဖြင့် သုံးမျိုးခွဲနိုင်သည်။

  • Global scope: အတွက် variables များကို command line (သို့) Ansible configuratoin များမှသတ်မှတ်နိုင်သည်။
  • Play scope: အတွက် variables များကို play အတွင်းနဲ့ ၄င်းနဲ့ဆက်စပ်သော strucutre များတွင်သတ်မှတ်နိုင်သည်။
  • Host scope: အတွက် variables များကို host groups နဲ့ host တစ်ခုစီအတွက် inventory, fact-gathering (သို့) registered tasks များဖြင့်သတ်မှတ်နိုင်သည်။

VARIABLES IN PLAYBOOKS

Playbook variables မျးကို နည်းအမျိုးမျိုးဖြင့်သတ်မှတ်နိုင်သည်။ ပထမဆုံးနည်းမှာ — playbook အစတွင် vars block အတွင်း variables များထားခြင်းဖြစ်သည်။

- hosts: all
vars:
user: joe
home: /home/joe
tasks:
# Creates the user joe ဟူသော ခေါင်းစဥ်ကိုတပ်ပေးသွားမည်။
- name: Creates the user {{ user }}
user:
# Joe နာမည်နဲ့ user တစ်ယောက်ကို create လုပ်ပေးသွားမည်။
name: “{{ user }}”

Playbooks အတွင်းသတ်မှတ်ထားသော variables များကို ယူသုံးရန် double curly braces {{}} ကိုအသုံးပြု၍ ၄င်း {{}} ထဲတွင် varialbe name ကိုထည့်သွင်းခြင်းဖြင့်၊ tasks ကို run လိုက်သောအခါ ansible သည် value ကိုထည့်သွင်းပေးသွားမည်။

နောက်တစ်ခုမှာ external variables files ကို vars_files directive ဖြင့်ရယူအသုံးပြုခြင်းဖြစ်သည်။ အောက်တွင်ဖော်ပြထားသည် play တွင် var folder အတွင်းရှိ users.yml ကိုယူသုံးထားသည်။

- hosts: all
vars_files:
- vars/users.yml

vars folder အတွင်း users.yml file တွင်အောက်ပါ value များကိုသတ်မှတ်ထားရမည်။
user: joe
home: /home/joe

HOST VARIABLES AND GROUP VARIABLES

Inventory variables များကို Host variables နဲ့ Group variables ဟူ၍ အပိုင်းနှစ်ပိုင်းခွဲနိုင်သည်။ Host variables များသည် host တစ်ခုအတွက်သာ ရည်ညွှန်ပြီး၊ Group variables များသည် group အတွင်းရှိ host များအားလုံးအတွက် သက်‌ရောက်သည်။ Inventory အတွင်း host variable များသည် group variables များအပေါ် ဦးစားပေးသက်‌ရောက်မှုရှိသော်လည်း၊ Plackbook အတွင်းသတ်မှတ်ထားသော variables များသည် inventory အပေါ် လွမ်းမိုးသက်ရောက်သည်။

Host variables သတ်မှတ်ခြင်း။
[servers]
web1.example.com ansible_user=joe
Group variables သတ်မှတ်ခြင်း။
[servers]
web1.example.com
web2.example.com
[servers:vars]
user=joe
Variables များကို inventory အတွင်းတိုက်ရိုက်သတ်မှတ်ခြင်းထက် host_vars နဲ့ group_vars ဟူ၍အသုံးပြုခြင်းသင့်သည်။
group_var folder အတွင်း group name နဲ့ file တစ်ခုဆောက်ပြီး ထိုfile အတွင်း အသုံးပြုလိုသော variable များသတ်မှတ်နိုင်သည်။
group_vars/servers
user: joe

OVERRIDING VARIABLES FROM THE COMMAND LINE

Inventory variables များအပေါ်ကို playbook များဖြင့် variables များက overridden ရှိသည်။ ထို့အပြင် ထို inventroy နဲ့ playbook များအပေါ် command line ကိုအသုံးပြု၍ variables များကိုသတ်မှတ်နိုင်သည်၊ ၄င်းကို extra variables များဟုခေါ်သည်။

$ ansible-playbook main.yml -e "package=apache"

USING ARRAYS AS VARIABLES

Configurations များကိုသတ်မှတ်ရာတွင် ဥပမာ a list of users ကဲ့သို့သော တူညီသော user element တစ်ခုနဲ့ဆက်စပ်လျက်ရှိသော multiple variables များအတွက် arrays များကိုအသုံးပြုနိုင်သည်။

user1_first_name: Bob
user1_last_name: Jones
user1_home_dir: /users/bjones
user2_first_name: Anne
user2_last_name: Cook
user2_home_dir: /users/acook
Array အတွင်းထည့်သွင်းအသုံးပြုခြင်း။
users:
bjones:
first_name: Bob
last_name: Jones
home_dir: /users/bjones
acook:
first_name: Anne
last_name: Cook
home_dir: /users/acook
user data များကို access ပြန်လုပ်ရာတွင် အောက်ပါအတိုင်း variable တန်ဖိုးများကို ရယူနိုင်သည်။- # Returns 'Bob'
users.bjones.first_name
# Returns '/users/acook'
users.acook.home_dir
ထို့အပြင် variable များသည် python dictionary ကဲ့သို့အသုံပြုထားသဖြင့်၊ ‌အောက်ပါအတိုင်း variable တန်ဖိုးများကိုနောက်တစ်နည်းဖြင့်ရယူနိုင်သည်။ # Returns 'Bob'
users['bjones']['first_name']
# Returns '/users/acook'
users['acook']['home_dir']
dot notation ကိုအသုံးပြုခြင်းသည် key name များတူသောအခါ conflicts errors များရှိနိုင်သည်။ Variable များကိုရယူရန် အထက်ပါ နည်းနှစ်မျိုးလုံးကိုအသုံးပြုနိုင်သော်လည်း၊ troubleshooting များတွင် ရှုပ်ထွေးမှုမရှိစေရန် နည်းတစ်မျိုးတည်းကိုသာ သုံးသင့်သည်။

CAPTURING COMMAND OUTPUT WITH REGISTERED VARIABLES

Command ၏ output များကို capture လုပ်နိုင်ရန် register ကိုအသုံးပြုနိုင်သည်။ ထို output များသည် temporary variables များအဖြစ်သိုလှောင်ထားနိုင်ပြီး၊ debugging (သို့) command output များတွင် configuration များကို အသေးစိတ် ဖော်ပြစေလိုသည့်အခါများအတွက်သုံးသည်။

အောက်ပါ playbook တွင် command ၏ output တွင် debugging အတွက် capture လုပ်နိုင်ရန် register ပြုလုပ်ပုံကိုဖော်ပြထားသည်။ 
---
- name: Installs a package and prints the result
hosts: all
tasks:
- name: Install the package
yum:
name: httpd
state: installed
register: install_result
- debug: var=install_result
$ ansible-playbook playbook.yml
PLAY [Installs a package and prints the result] ****************************
TASK [setup] ***************************************************************
ok: [web1.example.com]
TASK [Install the package] *************************************************
ok: [web1.example.com]
TASK [debug] ***************************************************************
ok: [web1.example.com] => {
"install_result": {
"changed": false,
"msg": "",
"rc": 0,
"results": [
"httpd-2.4.6-40.el7.x86_64 providing httpd is already installed"
]
}
}
PLAY RECAP *****************************************************************
web1.example.com : ok=3 changed=0 unreachable=0 failed=0

VAULT

Ansible files များကို ပုံမှန်အားဖြင့် plain text ဖြင့်သိမ်းသည်။ ထို့ကြောင့် အရေးကြီးသော sensitive data များအတွက် ansible ၏ vault ကိုအသုံးပြုနိုင်သည်။ File များ၏ encryption အတွက် AES256 နဲ့ secret key အတွက် password ကိုအသုံးပြုသည်။ Ansible vault ကိုအသုံပြုခြင်းဖြင့် data file များကို encypt နဲ့ decrypt ပြလုပ်နိုင်သည်။ Ansible vault ကိုအသုံးပြုရန် command-line tool ဖြစ်သော် ansible-vault ကိုအသုံးပြု၍ files များကို create, edit, encrypt, decrypt နဲ့ view တို့ကိုပြုလုပ်နိုင်သည်။

Creating an Encrypted File 
File များကို Encrypted ပြုလုပ်ရန် ansible-vault create filename ကဲ့သို့သော command ကိုအသုံးပြုနိုင်သည်။
$ ansible-vault create secret.yml
New Vault password: redhat
Confirm New Vault password: redhat
Vault password ကို (valut-pass)ကဲ့သို့ file အတွင်းသိမ်းထားခြင်းဖြင့်လည်း file ကို encrypted လုပ်နိုင်သည်။
$ ansible-vault create --vault-password-file=vault-pass secret.yml
Viewing an Encrypted File
$ ansible-vault view secret.yml
Editing an Existing Encrypted File
$ ansible-vault edit secret.yml
Encrypting an Existing File
$ ansible-vault encrypt secret1.yml
Decrypting an Existing File
$ ansible-vault decrypt secret1.yml --output=secret1-decrypted.yml
Changing the Password of an Encrypted File
$ ansible-vault reky secret.yml
Changing the Password of using a vault password File
$ ansible-vault reky --new-vault-password-file=NEW_VAULT_PASSWORD_FILE secret.yml

PLAYBOOKS AND ANSIBLE VAULT

$ ansible-playbook --vault-id @prompt site.ymlvault-pass
password (one)
ansible (two)
toor (three)
Ansible 2.4 ၏အရင် version များတွင် --ask-vault-pass ဖြင့်အသုံးပြုရသည်။ တူညီသော password တစ်ခုတည်းဖြင့် encrypted လုပ်ထားသော file များဖြစ်ပါက၊ ၄င် option ကို ဆက်လက်အသုံးပြုနိုင်သည်။
$ ansible-playbook --ask-vault-pass site.yml
ထို့အပြင် --vault-password-file option ကို plain text အတွင်း excrypted လုပ်ထားသော password file တစ်ခုကို ညွှန်းရန်သုံးနိုင်သည်။
$ ansible-playbook --vault-pasword-file=vault-pw-file site.yml

FACT

Ansible facts များသည် managed hosts များပေါ်ရှိ ‌automatically ရှာဖွေနိုင်သော ariables များဖြစ်ကြသည်။

Facts များတွင် host ၏ specific information များပါ၀င်သည်။ ၄င်း information များကို plays များအတွင် regular variables များ၊ conditionals များ၊ loops များနဲ့ အခြားသောအခြေအနေများဖြစ်သော manage host များမှ value များကို စုစည်းခြင်း collected အပေါ်တွင်မူတည်ပြီး အမျိုးမျိုးအသုံးပြုနိုင်သည်။

Managed host ‌များအတွက် facts gathered အချို့မှာ - 
• The host name
• The kernel version
• The network interfaces
• The IP addresses
• The version of the operating system
• Various environment variables
• The number of CPUs
• The available or free memory
• The available disk space

Facts များသည် managed host များ၏ state များကိုရယူရန်အသုံးပြုနိုင်သလို၊ ၄င်း state များအပေါ်မူတည်ပြီး action များကိုလည်းသတ်မှတ်နိုင်သည်။

Ansible Facts variable အချို့မှာ - 
ansible_facts['hostname']
ansible_facts['fqdn']
ansible_facts['default_ipv4']['address']
ansible_facts['interfaces']
ansible_facts['devices']['vda']['partitions'][vda1']['size']
ansible_facts['dns']['nameserves']
ansible_facts['kernel']
variable များ၏ value များသည် has/dictionary ဖြစ်သော် value များကို dot notation ဖြင့်လည်းရနိုင်သည်။
ansible_facts.default_ipv4.address
ansible_facts.dns.nameservers

Playbook အတွင်းfact များကိုအသုံးပြုသောအခါ၊ ansible သည် variable name များ၏ ၄င်းတို့နဲ့သက်ဆိုင်သော value များကို dynamically အစားထိုးပေးသွားသည်။

---
- hosts: all
tasks:
- name: Prints various Ansible facts
debug:
msg: >
The default IPv4 address of {{ ansible_facts.fqdn }}
is {{ ansible_facts.default_ipv4.address }}
$ ansible-playbook playbook.yml
PLAY ***********************************************************************
TASK [Gathering Facts] *****************************************************
ok: [demo1.example.com]
TASK [Prints various Ansible facts] ****************************************
ok: [demo1.example.com] => {
"msg": "The default IPv4 address of demo1.example.com is
172.25.250.10"
}
PLAY RECAP *****************************************************************
demo1.example.com : ok=2 changed=0 unreachable=0 failed=0

Ansible Facts Injected as variables
အောက်ပါ command ကိုအသုံးပြု၍ facts အချက်အလက်များကို ရယူနိုင်သည်။
$ ansible web1.example.com -m setup
Ansible ၏ facts old naming system များကို config file ၏ [default] အောက်ရှိ inject_facts_as_vars ကို true မှ false သို့ပြောင်းခြင်ဖြင့် turn off လုပ်နိုင်သည်။

Turning off fact gathering
---
- name: This play gathers no facts automatically
hosts: large_farm
gather_facts: no
tasks:
- name: Manually gather facts
setup:

Creating Custom Facts
Custom facts များကို INI သို့ JSON format များအသုံးပြု၍ static file များတွင် သတ်မှတ်နိုင်ပါသည်။ managed hosts ၏ Custom facts files ကို /etc/ansible/facts.d folder အောက်တွင် .fact extension ဖြင့်သတ်မှတ်နိုင်သည်။


[packages]
web_package = httpd
db_package = mariadb-server
[users]
user1 = joe
user2 = jane

ထို့နောက် ‌adhoc command အသုံးပြု၍ $ ansible web1.example.com -m setup အောက်ပါ result ကို ရရှိမည်ဖြစ်သည်။ playbooks ကိုလည်းအသုံးပြုကာ custom facts ကိုအသုံးပြုနိုင်သည်။

"ansible_local": {
"custom": {
"packages": {
"db_package": "mariadb-server",
"web_package": "httpd"
},
"users": {
"user1": "joe",
"user2": "jane"
}
}
},

Using Magic variables

တစ်ချို့ variables များသည် ansible မှ automatically ဖြင့်သတ်မှတ်ခြင်းဖြစ်ပြီး၊ facts များ (သို့) setup module ၏ configured မဟုတ်ကြပါ။ ထို variables များကို magic variable များဟုခေါ်ပြီး၊ managehost ထံမှ တိကျသော information များရယူရန်အသုံး၀င်သည်။

Magic variables အချို့မှာ - 
hostvars
group_names
groups
inventory_hostname
$ansible localhost -m debug -a 'var=hostvars'["localhost"]'

ယခုဖော်ပြထားသော ansible ကို install လုပ်ခြင်း၊ host file နဲ့ inventory အကြောင်း၊ config file အကြောင်း၊ playbook ရေးခြင်း၊ variable, facts, vault များအသုံးပြုခြင်းများသည် Ansible စတင်လေ့လာသူများအတွက် အခြေခံများဖြစ်ပါသည်။ နားလည်မည်ဟုလည်းယူဆပါသည်။ နောက်ထပ်အနေဖြင့် TASK CONTROL, DYNAMIC INVENTORIES, SIMPLIFYING PLAYBOOKS WITH ROLES, DEPLOYING ROLES WITH ANSIBLE GALAXY များကို ဆက်လက်လေ့လာရပါမည်။

နိဂုံးချုပ်

နောက်ဆုံးအထိဖတ်ပေးတဲ့အတွက် ကျေးဇူးတင်ပါတယ်။ အ‌ကြောင်းအရာအများစုကို တိုက်ရိုက်ဘာသာပြန်ထားသော်လည်း၊တချို့ကိုသင့်တော်သလိုပြန်ရေးထားပါသည်။ အကျိုးအမြတ်အတွက်မဟုတ်ဘဲ ‌ပညာရေးအတွက်အထောက်အကူဖြစ်စေရန်သာရည်ရွယ်ပါသည်။ အမှားများပါပါက ထောက်ပြပေးဖို့ တောင်းဆိုလိုပါတယ်။

ယခုဖော်ပြထားသော Ansible အကြောင်းအရာများသည် Ansible ကို စတင်လေ့လာလိုသူများအတွက်အထောက်အကူပြုနိုင်မည်ဟု ယုံကြည်ပါသည်။ Ansible ကိုမိတ်ဆက်ရုံသာဖော်ပြထားသဖြင့် ကျန်ရှိသော လုပ်ဆောင်မှုများကို အ‌သေးစိတ်လေ့လာစရာများစွာကျန်ရှိနေပါသည်။

ထပ်မံလေ့လာနိုင်ရန်လည်း docs.ansible.com ၏ စာများနှာတွင် Documents များ၀င်ဖတ်ခြင်း၊ တခြား tutorials site များမှ tutorial များကိုလိုက်လုပ်ခြင်းဖြင့် အသိပညာနှင့် ကျွမ်းကျင်မှုများတိုးတက်ပြီး၊ ရည်မှန်းထားသော အောင်မြင်မှုများကိုရနိုင်ကြပါစေ။

Online Source:

Wikipedia — www.wikipedia.org

Ansible Docs — docs.ansible.com

--

--

suum (RHCE| CC-ISC2| NSE3| AZ-900)

စာရေးသားခြင်းဟာ အသိမြင်ကိုဖလှယ်ခြင်းနဲ့ အတွေးအမြင်ကို ဖွင့်တဲ့တံခါးလို့ခံယူပါတယ်။ စာပေတွေဟာ တူညီတဲ့ခရီးကိုလာမယ့်သူတွေအတွက် ပိုပြီးခရီးတွင်စေမယ်လို့ ယုံကြည်ပါတယ်