Ansible Configuration: ansible.cfg อธิบายทุก Settings ที่สำคัญ

ไฟล์ ansible.cfg คือหัวใจของการกำหนด default behavior ที่ใช้ทุกครั้งที่รัน playbook หรือ ad-hoc command การตั้งค่าในไฟล์นี้ช่วยลดการพิมพ์ flag ซ้ำ ๆ และทำให้ทีมทุกคนใช้ค่าเดียวกันโดยไม่ต้องจำ

บทความนี้อธิบาย settings สำคัญใน ansible.cfg แต่ละ section พร้อมตัวอย่างการใช้งานจริง และลำดับความสำคัญของไฟล์ config เมื่อมีหลายไฟล์ในระบบ

ลำดับการค้นหา ansible.cfg

เครื่องมือ configuration management นี้ค้นหา config file ตามลำดับดังนี้ (หยุดที่ไฟล์แรกที่พบ)

  • ANSIBLE_CONFIG — environment variable ที่ระบุ path ของไฟล์ (ความสำคัญสูงสุด)
  • ./ansible.cfg — ไฟล์ในโฟลเดอร์ปัจจุบัน (แนะนำสำหรับ per-project config)
  • ~/.ansible.cfg — ไฟล์ใน home directory ของ user
  • /etc/ansible/ansible.cfg — system-wide config (ค่า default)

แนะนำให้สร้าง ansible.cfg ในโฟลเดอร์โปรเจกต์เสมอ เพราะช่วยให้ทีมทุกคนใช้ค่าเดียวกันและเก็บใน Git ได้

Section [defaults] — ค่าพื้นฐานที่ใช้บ่อยที่สุด

Section นี้ควบคุม behavior หลักของระบบ automation

[defaults]
# ระบุ inventory file หลัก
inventory = ./inventory.ini

# user ที่ใช้ login บน managed node
remote_user = ubuntu

# private key สำหรับ SSH
private_key_file = ~/.ssh/control_key

# ปิดการตรวจสอบ host key (สะดวกใน lab แต่ปิดใน production)
host_key_checking = False

# จำนวน host ที่รันพร้อมกัน (default = 5)
forks = 10

# โฟลเดอร์เก็บ roles
roles_path = ./roles

# ปิด cow output ที่ไม่จำเป็น
nocows = 1

# แสดง diff เมื่อไฟล์เปลี่ยนแปลง
diff_always = False

# timeout ของ SSH connection (วินาที)
timeout = 30

Section [ssh_connection] — ปรับแต่ง SSH

Section นี้ควบคุมการเชื่อมต่อ SSH โดยตรง ส่งผลต่อความเร็วและเสถียรภาพของการรัน playbook บนหลาย host

[ssh_connection]
# เปิด pipelining เพื่อลด SSH round-trip (เพิ่มความเร็วมาก)
# ต้องปิด requiretty ใน sudoers บน managed node ก่อน
pipelining = True

# ใช้ SSH ControlMaster เพื่อ reuse connection (ลด overhead)
ssh_args = -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no

# ปิด ControlMaster ถ้าพบปัญหาการเชื่อมต่อ
# control_path = /tmp/ansible-ssh-%%h-%%p-%%r

pipelining = True เป็นการตั้งค่าที่ส่งผลต่อความเร็วมากที่สุด โดยลด SSH round-trip จาก 3 ครั้งเหลือ 1 ครั้งต่อ task แต่ต้องแน่ใจว่า managed node ไม่มี requiretty ใน sudoers

Section [privilege_escalation] — sudo และ become

ใช้เมื่อ playbook ต้องรัน command ด้วยสิทธิ์ root หรือ user อื่น

[privilege_escalation]
# เปิด privilege escalation โดย default
become = True

# method ที่ใช้ (sudo, su, pbrun, pfexec, doas)
become_method = sudo

# user ที่ต้องการเปลี่ยนไป (default = root)
become_user = root

# ถามรหัสผ่าน sudo หรือเปล่า
become_ask_pass = False

ค่าเหล่านี้ตั้งใน config เป็น global default ได้ แต่สามารถ override ได้ในระดับ play หรือ task ด้วย become: true/false ใน playbook

Section [inventory] — ตั้งค่า Inventory Plugins

Section นี้ควบคุมการใช้งาน inventory plugins ซึ่งมีประโยชน์เมื่อต้องการ dynamic inventory จากแหล่งข้อมูลภายนอก

[inventory]
# เปิดใช้ plugin เพิ่มเติมสำหรับ dynamic inventory
enable_plugins = host_list, script, auto, yaml, ini, toml

# ตรวจสอบ inventory ก่อนรัน
unparsed_is_failed = True

ตัวอย่าง ansible.cfg สมบูรณ์สำหรับ Production

นี่คือตัวอย่าง config ที่ใช้งานได้จริงในสภาพแวดล้อม production โดยเน้น security และ performance

[defaults]
inventory          = ./inventory.ini
remote_user        = deploy
private_key_file   = ~/.ssh/deploy_key
host_key_checking  = True
forks              = 20
roles_path         = ./roles:~/.ansible/roles
timeout            = 30
nocows             = 1
retry_files_enabled = False

[ssh_connection]
pipelining   = True
ssh_args     = -o ControlMaster=auto -o ControlPersist=120s

[privilege_escalation]
become          = True
become_method   = sudo
become_user     = root
become_ask_pass = False

สังเกตว่าใน production ให้เปิด host_key_checking = True เสมอ และใช้ deploy_key แยกต่างหากจาก personal key เพื่อ security และการ revoke ที่ง่ายกว่า

Override ค่าผ่าน Environment Variable

ทุก setting ใน ansible.cfg สามารถ override ด้วย environment variable ได้ โดยเปลี่ยนชื่อเป็นตัวพิมพ์ใหญ่และเติม ANSIBLE_ นำหน้า

# override forks เป็น 30 สำหรับ session นี้
export ANSIBLE_FORKS=30

# ปิด host key checking ชั่วคราว
export ANSIBLE_HOST_KEY_CHECKING=False

# ระบุ inventory file อื่น
export ANSIBLE_INVENTORY=/path/to/other/inventory.ini

Environment variable มีความสำคัญสูงกว่า ansible.cfg ทุกไฟล์ จึงเหมาะกับการ override ชั่วคราวในบางสถานการณ์โดยไม่ต้องแก้ไฟล์

ตรวจสอบ Config ที่ใช้งานอยู่จริง

ใช้คำสั่งนี้เพื่อดูว่าระบบกำลังอ่าน config จากไฟล์ไหน และค่าปัจจุบันเป็นอะไร

# แสดงไฟล์ config ที่ใช้งานอยู่
ansible --version

# แสดงค่า config ทั้งหมดที่ถูก apply (รวม defaults)
ansible-config dump --only-changed

# แสดงทุก setting พร้อม source ว่ามาจากไหน
ansible-config dump

สรุป

การตั้งค่า ansible.cfg ที่ดีช่วยลดความซับซ้อนในการรัน playbook และทำให้ทีมใช้งานได้สม่ำเสมอ settings ที่ส่งผลมากที่สุดคือ inventory, remote_user, private_key_file, และ pipelining ในส่วน [ssh_connection] ควรสร้างไฟล์นี้ในโฟลเดอร์โปรเจกต์เสมอและเก็บใน version control ร่วมกับ playbook