Ansible #4 ตอน Inventory 101
พื้นฐานสำคัญของ Ansible ประเด็นแรก จะเป็นการเข้าใจถึง inventory และการปรับแต่งข้อมูลใน inventory เพราะว่า Ansible ทำงานกับ host จำนวนมาก ๆ ในการสั่งงานเพียงครั้งเดียว ซึ่งในชีวิตจริง host แต่ละเครื่องก็มีคุณสมบัติไม่เหมือนกันเลย หรือในการสั่งให้ Ansible ทำงานในแต่ละครั้ง การระบุ host ที่จะบริหารจัดการ ก็อาจจะไม่ซ้ำกันเลย ดังนั้นการทำความรู้จัก inventory ของ Ansible ให้ดี ในความเห็นผม เป็นเรื่องที่ควรให้ความสำคัญเรื่องหนึ่งเลย
Inventory คืออะไร
Inventory เป็นที่ที่ใช้นิยาม host ที่ต้องการให้ Ansible เข้าไปบริหารจัดการ สามารถเก็บได้ในหลายรูปแบบ สามารถกำหนดรูปแบบที่ให้ Ansible สามารถอ่านข้อมูลจาก inventory ได้ โดยการกำหนดรูปแบบ สามารถกำหนดค่าใน ansible.cfg ในส่วนของ [inventory] กำหนดที่ค่า enable_plugins ซึ่งมีค่า default เป็น ‘host_list’, ‘script’, ‘yaml’, ‘ini’
ในส่วนของการกำหนด plugins ของ inventory สามารถเรียกดูรูปแบบของ inventory ได้จากคำสั่ง ansible-doc -t inventory -l และหากต้องการทราบรายละเอียดของ plugins สามารถศึกษาเพิ่มเติมได้จาก ansible-doc -t inventory <plugins name>
[drs@tyche ~]$ ansible-doc -t inventory -l
advanced_host_list Parses a 'host list' with ranges
constructed Uses Jinja2 to construct vars and groups based on existing inventory.
host_list Parses a 'host list' string
ini Uses an Ansible INI file as inventory source.
openstack OpenStack inventory source
script Executes an inventory script that returns JSON
virtualbox virtualbox inventory source
yaml Uses a specifically YAML file as inventory source. [drs@tyche ~]$ ansible-doc -t inventory host_list
> HOST_LIST (/usr/lib/python2.7/site-packages/ansible/plugins/inventory/host_list.py)Parses a host list string as a comma separated values of hosts This plugin only applies to inventory strings that are not paths and contain a comma.EXAMPLES:
# define 2 hosts in command line
# ansible -i '10.10.2.6, 10.10.2.4' -m ping all# DNS resolvable names
# ansible -i 'host1.example.com, host2' -m user -a 'name=me state=absent' all# just use localhost
# ansible-playbook -i 'localhost,' play.yml -c local
แต่โดยปกติ inventory จะเป็นอยู่ในรูปแบบของ text file โครงสร้างการจัดเก็บในรูปแบบ INI
alpha.example.com
beta.example.com[web]
gamma.example.com
delta.example.com [db]
zeta.example.com
theta.example.com
lambda.example.com
[app:children]
web
db
คำอธิบาย
- นิยามกลุ่มของ host (group) ได้ด้วยการระบุชื่ออยู่ระหว่างเครื่องหมาย [] (เรียกเครื่องหมายนี้เป็นภาษาอังกฤษว่า square brackets) แต่ละบรรทัดหลังจากบรรทัดที่นิยามกลุ่มไว้ก็จะเป็นการนิยาม host ท่ีเป็นสมาชิกในกลุ่ม เช่นในตัวอย่างจะมีการนิยาม group อยู่ 2 กลุ่มคือกลุ่ม web และ db ในการเรียกถึงกลุ่ม web ก็จะระบุถึงสมาชิกในกลุ่มได้ 2 host คือ gamma.example.com และ delta.example.com
- นิยาม group of group จากเรื่องของการนิยามกลุ่ม บรรทัดที่อยู่หลังเครื่องหมาย [] จะเป็นการระบุ host ที่เป็นสมาชิกของกลุ่ม แต่ถ้าต้องการให้แต่บรรทัดเป็นการระบุ กลุ่ม เป็นสมาชิก เช่นในตัวอย่างเป็นการนิยามกลุ่มที่ชื่อว่า app ที่มีสมาชิกเป็นสมาชิกของกลุ่ม web และ db นั่นหมายถึง การเรียกถึงกลุ่ม app หมายถึงการระบุถึง 5 host คือ gamma.example.com, delta.example.com, zeta.example.com, theta.example.com และ lambda.example.com ซึ่งการนิยาม group of group ทำได้โดย
[group_of_group_name:children]
หลังจากที่สร้าง inventory ได้ในเบื้องต้นแล้ว สามารถนำไปใช้งานในเบื้องต้นได้ โดยการระบุชื่อ group หรือ group of group ใน pattern (สงสัยเรื่อง pattern)
$ ansible -i myinventory web --list-host
hosts (2):
gamma.example.com
delta.example.com$ ansible -i myinventory app --list-host
hosts (5):
gamma.example.com
delta.example.com
zeta.example.com
theta.example.com
lambda.example.com
คำอธิบาย
--list-host
เป็นการให้ ansible แสดงรายชื่อ host ที่เป็นไปตาม pattern ที่กำหนด