Ansible 101 – Understanding Ansible Facts

ကျွန်တော်တို့ ansible မှာ automate လုပ်ချင်တဲ့ resources တွေရဲ့ variables တွေကိုသိချင်တဲ့အခါမှာ, ကျွန်တော်တို့ ansible facts ကိုသုံးပါတယ်.

resources တွေရဲ့ variables ဆိုတာက, တစ်ကယ်လို့ကျွန်တော်တို့ automate လုပ်မဲ့ resources က linux server ဖြစ်ရင် server ရဲ့ hostname, ip, network interface, cpu info, memory ..etc တို့ရဲ့ values တွေကိုပြောတာပါ.

ကျွန်တော်တို့ automate လုပ်မဲ့ resources တွေရဲ့ facts ( variables ) တွေပေါ်မူတည်ပြီး configuration management လုပ်လို့ရပါတယ်.

automate လုပ်မဲ့ resources တွေရဲ့ variables တွေကို ansible_facts variables နဲ့ ခေါ်သုံးလို့ရပါတယ်.

ansible_facts

ansible_facts ဆိုတာက ansible ကသတ်မှတ်ထားတဲ့ built-in variables name ကိုပြောတာပါ.

ansible_facts ကို JSON format နဲ့ ဖော်ပြပါတယ်.

ansible_fatcs variable ကို ansible ad-hoc or playbook မှာခေါ်သုံးလို့ရပါတယ်.

ansible ad-hoc command မှာ ansible_facts variable ကို setup module သုံးပြီးကြည့်လို့ရပါတယ်, file အနေနဲ့သိမ်းလို့ရပါတယ်.

 ansible localhost -m setup 
 ansible localhost -m setup > gather_facts.json

တစ်ကယ်လို့ automate လုပ်မဲ့ resources တွေရဲ့ variables တွေမှာ ကိုယ်လိုချင်တဲ့ အပိုင်းကို ansible ad-hoc နဲ့ အမြန် ခေါ်လို့ရပါတယ်. ansible playbook နဲ့လဲရပါတယ်, playbook ရေးဖို့လိုပါတယ်.

 ansible localhost -m setup -a "gather_subset=network"

ကျွန်တော်တို့ setup module ရဲ့ filter argument နဲ့ ကိုယ်လိုချင်တဲ့ အပိုင်းကို ခေါ်သုံးလို့ရပါတယ်. wildcard ( * ) သုံးလို့ရပါတယ်.

တစ်ကယ်လို့ ကိုယ်လိုချင်တဲ့ အပိုင်းကို ရှာချင်ရင် pipe and regular expression ကိုသုံးတာ ပိုမြန်ပါတယ်.

ကိုယ်ရှာချင်တဲ့ variables ကို playbook ထဲမှာ ထည့်ရေးဖို့ ရှာတဲ့ အခါမှာ ansible ad-hoc and setup module ကိုသုံးတာ ပိုမြန်ပါတယ်.

 ansible localhost -m setup -a "filter=ansible_*" | grep -i 'mount'
 ansible localhost -m setup -a "filter=ansible_mem*"
 ansible localhost -m setup -a "filter=ansible_interface*"

ကျွန်တော်တို့ localhost နေရာမှာ inventory file ထဲက group or hosts ကိုယ်ကြိုက်ရာကို သုံးလို့ရပါတယ်.

 ansible all -m setup -a "filter=ansible_*" | grep 'memory'

setup module ရဲ့ docs ကို reference လုပ်ပါ.

ansible-doc setup

ansible playbook တွေကို ကျွန်တော်တို့ run တဲ့အခါမှာ setup module ကို ansible က auto သုံးပါတယ်. ansible_facts ကိုသုံးဖို့ပါ.

ကျွန်တော်တို့ /etc/ansible/ansible.cfg file မှာ ပြင်လို့ရပါတယ်. ကျွန်တော်တို့ automate လုပ်မဲ့ resources တွေရဲ့ variables ကိုသိပြီးသာဆိုရင် gather_facts ကိုမသုံးလဲရပါတယ်.

ကျွန်တော်တို့ resources တွေအများကြီးကို automate လုပ်တဲ့အခါမှာ gather_facts ကိုမသုံးရင် performance ပိုကောင်းစေပါတယ်.

ansible_facts ကို သုံးထားတဲ့ tasks တွေက gather_facts ကိုလိုပါတယ်.

/etc/ansible/ansible.cfg မှာ gather_facts ကို default on ထားပါတယ်.

တစ်ကယ်လို့ gather_facts ကို ပိတ်ချင်ရင် ansible playbook မှာ define လုပ်လို့ရပါတယ်.

- hosts: localhost
  gather_facts: no

gather_facts ကိုပိတ်ထားရင် ansible_facts variable ကိုသုံးလို့မရပါဘူး.

gather_facts ပိတ်ပြီး ansible_facts variables ကိုသုံးချင်ရင် setup or gather_facts module ကို အရင်သုံးဖို့ လိုပါတယ်.

---
- name: query ansible facts
  hosts: localhost
  gather_facts: no
  tasks:
    - name: use setup module
      setup:

    - name: use debug module
      debug:
        var: ansible_facts

ansible ad-hoc command ကို setup module နဲ့သုံးရင် ansible_facts variable က ansible_ prefix နဲ့ reply ပြန်ပါတယ်.

ansible playbook မှာ setup module ကိုသုံးရင် ansible_facts variable က ansible_ prefix နဲ့ reply မပြန်ပါဘူး.

ansible_facts ထဲက variables တွေကို ansible playbook ထဲမှာသုံးချင်ရင် ansible_ prefix ကိုသုံးဖို့မလိုပါဘူး.

---
- name: query ipv4 addresses
  hosts: localhost
  gather_facts: no
  tasks:
    - name: use setup module
      setup:

    - name: use debug module 
      debug:
        var: ansible_facts['all_ipv4_addresses']

ကျွန်တော်တို့ system hostname ကို ansible_facts ကိုသုံးပြီး အောက်ပါ syntax နဲ့ ခေါ်သုံးလို့ရပါတယ်.

var: ansible_facts['fqdn']

ansible_facts ရဲ့နောက်မှာ ” ansible_ ” prefix ကိုထည့်ဖိုမလိုပါဘူး.

ansible module တွေက python ansible package ကိုသုံးပြီးရေးထားတဲ့ python script file တွေပါ.

setup module ကိုလေ့လာလိုလျှင် အောက်ပါ directory မှာကြည့်ပါ.

 vim /home/invoker/.local/lib/python3.8/site-packages/ansible/modules/setup.py

python ansible package ကိုလဲလေ့လာလို့ရပါတယ်.

 python
>>> import ansible
>>> help(ansible)

ansible_facts ကို json format နဲ့သိမ်းထားပြီး, ansible facts ထဲမှာdata type သုံးမျိုးလောက်ပါပါတယ်.

List, Dictionary and Ansible Unsafe Text တို့ဖြစ်ပါတယ်.

ansible_facts ရဲ့ data type ကို type_debug filter နဲ့ check လုပ်လို့ရပါတယ်.

---
- name: query ansible_facts 
  hosts: worker1.example.com 
  tasks:
    - name: check ansible_facts data type 
      debug:
        var: ansible_facts['all_ipv4_addresses'] | type_debug
    - name: check ansible_facts data type
      debug:
        var: ansible_facts['memory_mb'] | type_debug
    - name: check ansible_facts data type
      debug:
        var: ansible_facts['machine'] | type_debug

ansible-playbook check_datatype.yaml

caching and disabling facts

facts ( variables ) တွေကို memory မှာ default အနေနဲ့ သိမ်းထားပါတယ်. cached facts ကို multiple play တွေမှာသုံးတာများပါတယ်. ကျွန်တော်တို့ cached facts ကိုသုံးပြီး system တစ်ခုရဲ့ facts ( variables ) ကို အခြား system တစ်ခုမှာ သုံးလို့ရပါတယ်.

magic variables တွေကိုသုံးတဲ့အခါမှာ cached facts ကအရေးပါ ပါတယ်.ansible က caching အနေနဲ့ memory cache plugin ကို default အနေနဲ့သုံးထားပါတယ်.

ansible playbook တစ်ခုကို run တဲ့အချိန်မှာ ansible က memory cache plugin ကို သုံးပြီး ansible playbook ပြီးတဲ့အချိန်ထိ facts ( variables ) ကို memory ထဲမှာ ခနသိမ်းထားပါတယ်. အခြား cache plugin တွေကိုလဲ သုံးလို့ရပါတယ်.

ကျွန်တော်တို့ resources အများကြီးကို automate လုပ်တဲ့အခါ မှာ facts caching ကြောင့် performance ပိုကောင်းစေ ပါတယ်.

ကျွန်တော်တို့ facts caching ကို ပိတ်လို့ရပါတယ်. ansible playbook မှာ gather_facts: no ဆိုပြီးပိတ်လို့ရပါတယ်.

- hosts: localhost
  gather_facts: no

custom facts

ကျွန်တော်တို့က ansible setup module ကိုသုံးပြီး standard သတ်မှတ်ထားတဲ့ facts ( variables ) တွေကို resources တွေစီ က ခေါ်သုံးလို့ရပါတယ်. standard မသတ်မှတ်ထားတဲ့ custom facts ( variables ) တွေကိုလဲ automate လုပ်မဲ့ resources တွေမှာ သတ်မှတ်လို့ရ ပါတယ်.

custom facts ကို သတ်မှတ်ဖို့ နှစ်နည်း ရှိပါတယ်. tempoaray facts အတွက် ansible set_fact module ကိုသုံးပြီး, permanent facts အတွက် /etc/ansible/facts.d directory မှာ သတ်မှတ်လို့ရပါတယ်.

set_fact module ကိုသုံးပြီး temporary custom facts ကို managed hosts မှာသတ်မှတ်ကြည့်ရအောင်.

---
- name: set set_fact on managed host
  hosts: worker1.example.com
  tasks:
    - name: use set_fact module
      set_fact:
        webserver:
          - package: apache2
            installed: True
        cacheable: yes

    - name: use debug module to check facts
      debug:
       var: webserver
 ansible-playbook set_fact.yaml

permanent custom facts ကို သုံးဖို့ /etc/ansible/facts.d directory ကို managed hosts မှာ create လုပ်ပါ. facts.d directory ထဲမှာ ကိုယ်သတ်မှတ် လိုတဲ့ custom facts file ကိုသိမ်းပါ.

custom facts file ကို JSON, INI, executable file တို့နဲ့ရေး လို့ရပါတယ်.

custom fact file ကို .fact နဲ့သိမ်းဖို့လို ပါတယ်.

custom facts file တစ်ခုကို INI Style နဲ့ရေးကြည့်ရအောင်.

 vim webserver.fact
[general]
package=apache2
installed=True

facts.d directory ထဲ့က custom facts file ကို ansible_local နဲ့ခေါ်သုံးလို့ရပါတယ်.

custom facts file ကို /etc/ansible/facts.d directory မှာ သတ်မှတ်ပြီးရင် setup module ကို tasks တစ်ခုအနေနဲ့ ပြန် run ပေးရပါတယ်.

ဒါမှ ကျွန်တော်တို့ custom facts ကိုသုံးလို့ရမှာပါ.

sample playbook ကို ကြည့်ပါ.

 vim custom_facts.yaml
---
- name: set custom facts on remote hosts
  hosts: all
  tasks:

    - name: Create directory for ansible custom facts
      ansible.builtin.file:
        state: directory
        recurse: yes
        path: /etc/ansible/facts.d

    - name: Install custom fact
      ansible.builtin.copy:
        src: webserver.fact
        dest: /etc/ansible/facts.d

    - name: Re-read facts after adding custom fact
      ansible.builtin.setup:
        filter: ansible_local

    - name: check custom facts
      debug:
        var: ansible_local['webserver']['general']['package']

custom facts ကို ခေါ်သုံးတဲ့အခါမှာ custom facts file name and label ကို ansible_local နောက်မှာ ထည့်ရေးရပါတယ်.

var: ansible_local[‘webserver’][‘general’][‘package’]

[‘webserver’] က custom facts file name ပါ.

[‘general’] က custom facts file ရဲ့ label ပါ.

[‘package’] က custom facts value ပါ.

ကျွန်တော်တို့ sample playbook ကို run ကြည့်ပါ.

 ansible-playbook --syntax-check custom_facts.yaml
 ansible-playbook custom_facts.yaml

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 *