Ansible 101 – Magic Variables Usage In Ansible

ကျွန်တော်တို့ magic variables ကိုသုံးပြီး automate လုပ်မဲ့ resources တွေရဲ့ facts ( variables ) တွေကို ansible playbook, jinja2 template file မှာ ခေါ်သုံးလို့ရပါတယ်.

magic variables က ansible special variables ထဲမှာပါ ပါတယ်.

အသုံးများတဲ့ magic variables တွေက hostvars, groups, group_names and inventory_hostname တို့ဖြစ်ပါတယ်.

hostvars

ကျွန်တော်တို့ hostvars ကိုသုံးပြီး ansible_facts, playbook variables တို့ကိုခေါ်သုံးလို့ရပါတယ်.

hostvars ကိုသုံးပြီး hosts တွေရဲ့ variables ကို ခေါ်သုံးလို့ရပါတယ်. jinja2 template file မှာသုံးတာများပါတယ်.

ကျွန်တော်တို့ hostvars ကို မသုံးခင်မှာ ansible gather_facts ကို အရင် run ထားဖို့လိုပါတယ်.

ကျွန်တော်တို့ hostvars ကို jinja2 template file မှာသုံးကြည့်ရအောင်.

 vim hostvars.j2
{% for host in groups['all'] %}
  {{ hostvars['worker1.example.com']['ansible_facts']['fqdn'] }}
{% endfor %}

hostvars က all group ထဲက worker1.example.com ( host ) ရဲ့ ansible_facts ကို ခေါ်သုံးထားတာပါ.

jinja2 template file ကို ansible playbook မှာ template module ကိုသုံးပြီးထည့်သုံးလို့ရပါတယ်.

ansible playbook နမူနာကို ကြည့်ပါ. ansible playbook က gather_facts ကို deafult run ပါတယ်.

 vim hostvars.yaml
---
- name: use hostvars
  hosts: all
  tasks:
    - name: use hostvars to check other host variables
      template:
        src: hostvars.j2
        dest: /home/ansible/test.txt
        newline_sequence: '\r\n'

ကျွန်တော်တို့ ansible playbook ရဲ့ hosts မှာ all group ကိုသုံးထားပါတယ်.

တစ်ကယ်လို့ host မှာ worker2.example.com ကိုဘဲ သတ်မှတ်ထားရင် hostvars မှာ error တက်ပါမယ်.

---
- name: use hostvars
  hosts: worker2.example.com
  tasks:
    - name: use hostvars to check other host variables
      template:
        src: hostvars.j2
        dest: /home/ansible/test.txt
        newline_sequence: '\r\n'

hostvars ကို မသုံးခင် ကိုယ်သုံးချင်တဲ့ host ရဲ့ variables က cached facts မှာ ရှိနေဖို့ လိုပါတယ်.

play တစ်ခုထည့်ပြီး gather_facts ကို all group မှာအရင် run ပြီးသုံးလဲရပါတယ်.

---
- name: for gather_facts only
  hosts: all

- name: use hostvars
  hosts: worker2.example.com
  tasks:
    - name: use hostvars to check other host variables
      template:
        src: hostvars.j2
        dest: /home/ansible/test.txt
        newline_sequence: '\r\n'

ansible playbook တစ်ခုထဲမှာ ဖြစ်လို့ all group ရဲ့ variables တွေက cache အနေနဲ့သိမ်းပြီးတာပါ.

နောက် play ရဲ့ host မှာ worker2.example.com ကို သုံးရင် variables က cache မှာရှိပြီးတာဖြစ်လို့ hostvars ကိုသုံးလိုရပါ ပြီး.

multiple play ကိုတော့ သုံးရမှာပါ.

groups

groups ( magic variables ) ကိုသုံးပြီး inventory ထဲမှာရှိတဲ့ groups, hosts list တွေကို Jinja2 Template file မှာသုံးလို့ရပါတယ်.

{% for host in groups['app_servers'] %}
   # something that applies to all app servers.
{% endfor %}

groups and hostvars ကိုတွဲသုံး လို့ရပါတယ်.

{% for host in groups['app_servers'] %}
   {{ hostvars[host]['ansible_facts']['eth0']['ipv4']['address'] }}
{% endfor %}

( “app_servers” ) groups ထဲ့က hosts တွေကို for loop ကိုသုံးပြီး hostvars နဲ့ IPv4 address ကို ansible_facts ကခေါ်သုံးတာပါ.

group_names

group_names ( magic variables ) ကို သုံးပြီး host တစ်ခုဟာ list of groups ထဲမှာပါ မပါကို check လုပ်လို့ရပါတယ်.

ကျွန်တော်တို့ group_names ကို jinja2 မှာ သုံးကြည့်ရအောင်.

 vim group_names.j2
{% if 'dev' in group_names %}
   This host is a member of dev group
{% endif %}

ကျွန်တော်တို့ playbook run တဲ့ hosts တွေက ( dev ) group ထဲမှာ ပါရင် Jinja2 Template file မှာသတ်မှတ်ထားတာတွေ ကိုအသုံးပြုမှာပါ.

group_names က inventory file ထဲက groups တွေကို check လုပ်ပါတယ်.

Inventory file ကို ကြည့်ပါ.

ansible playbook မှာ jinja2 template file ကို သုံးကြည့်ပါ.

 vim group_names.yaml
---
- name: use group_names
  hosts: all
  tasks:
    - name: use group_names to check host membership
      template:
        src: group_names.j2
        dest: /home/ansible/group_names.txt
        newline_sequence: '\r\n'

ကျွန်တော်တို့ hosts နေရာမှာ all ( group ) ကိုသုံးထားပါတယ်.

Jinja2 template file မှာ group_names နဲ့ ” dev ” ( group ) ကိုဘဲ condition check လုပ်ထားလို့ ” dev ” ( group ) မှာပါတဲ့ hosts တွေမှာဘဲ သုံးလို့ရမှာပါ.

” dev ” ( group ) မှာပါတဲ့ hosts တွေမှာဘဲ jinja2 template file မှာ သတ်မှတ်ထားတာ တွေက effect ဖြစ်တာကိုကြည့်ပါ.

inventory_hostname

ကျွန်တော်တို့ gather_facts ကို disable လုပ်ထားတဲ့အခါမှာ inventory_hostname ကို ansible_hostname အစားသုံးလို့ရပါတယ်.

inventory file ထဲမှာ သတ်မှတ်ထားတဲ့ host names တွေကို inventory_hostname ( magic variables ) ကခေါ်သုံးလို့ရပါတယ်.

inventory_hostname ကို jinja2 မှာသုံးတာကိုကြည့်ပါ.

{% for host in groups['all'] %}
  {{ hostvars[host]['inventory_hostname'] }}
{% endfor %}

ကျွန်တော်တို့ hostvars အနောက်က host နေရာမှာ ( ‘ ‘ ) မထည့်ထားပါဘူး, ( ‘ ‘ ) ထည့်ပြီး သုံးရင် error တက်ပါတယ်.

host တစ်ခုချင်စီ သုံးရင် ( ‘ ‘ ) ကိုသုံးဖို့လိုပါတယ်.

{{ hostvars['worker1.example.com']['inventory_hostname'] }}

Jinja2 Template file ကို ansible playbook မှာသုံးတာကိုကြည့်ပါ.

 vim inventory_hostname.yaml
---
- name: use inventory hostname
  hosts: all
  gather_facts: False
  tasks:
    - name: use inventory_hostname to check other host variables
      template:
        src: inventory_hostname.j2
        dest: /home/ansible/inventory_hostname.txt
        newline_sequence: '\r\n'

ကျွန်တော်တို့ gather_facts ကို False လုပ်ပြီးပိတ်ထားပါတယ်.

inventory_hostname.txt ကို အခြား hosts တွေမှာ ansible ad-hoc command သုံးပြီး check လုပ်ပါ.

 ansible all -m shell -a "cat /home/ansible/inventory_hostname.txt"

အခြား magic variables တွေကိုကြည့်ပါ.

Image by Red Hat

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 *