ตั้งค่า Ansible SSH Authentication: Key-based Auth ด้วย Private Key

SSH Key-based Authentication เป็นรากฐานที่ขาดไม่ได้เพราะเครื่องมือนี้ทำงานผ่าน SSH ทุกครั้งที่รัน playbook หรือ ad-hoc command การตั้งค่า authentication ให้ถูกต้องตั้งแต่ต้น ช่วยให้จัดการเซิร์ฟเวอร์จำนวนมากได้โดยไม่ต้องพิมพ์ password และลดความเสี่ยงด้านความปลอดภัยไปพร้อมกัน

บทความนี้อธิบายการสร้าง SSH key pair, กระจาย public key ไปยัง managed node, และการตั้งค่าให้ใช้ private key ที่ถูกต้อง พร้อมแนวทางจัดการหลาย key สำหรับสภาพแวดล้อมที่มีหลาย environment

ทำไมต้องใช้ Key-based Authentication

การใช้ password authentication แบบธรรมดามีปัญหาสำคัญ: ต้องพิมพ์ password ทุกครั้งหรือใช้ --ask-pass flag ซึ่งไม่เหมาะกับ automation และไม่สามารถ parallelize การทำงานบนหลาย host ได้ดี Key-based authentication แก้ปัญหาเหล่านี้ได้ทั้งหมด และยังปลอดภัยกว่าเพราะไม่มี password ที่จะถูก brute force

สร้าง SSH Key Pair สำหรับ Ansible

แนะนำให้สร้าง key pair แยกต่างหากสำหรับงาน automation โดยเฉพาะ (ไม่ใช้ key เดียวกับ personal key) เพื่อให้ควบคุมและ revoke ได้อิสระ

# สร้าง key pair แบบ Ed25519 (แนะนำ — ปลอดภัยและ key สั้น)
ssh-keygen -t ed25519 -f ~/.ssh/control_key -C "automation@control-node"

# หรือแบบ RSA 4096 ถ้า managed node รุ่นเก่าไม่รองรับ Ed25519
ssh-keygen -t rsa -b 4096 -f ~/.ssh/control_key -C "automation@control-node"

เมื่อสร้างแล้วจะได้ไฟล์สองไฟล์: ~/.ssh/control_key (private key — เก็บไว้บน control node เท่านั้น) และ ~/.ssh/control_key.pub (public key — copy ไปใส่บน managed node ทุกเครื่อง)

กระจาย Public Key ไปยัง Managed Node

มีสองวิธีหลักในการ copy public key ไปยัง managed node

วิธีที่ 1: ใช้ ssh-copy-id (ง่ายที่สุด)

# Copy public key ด้วย ssh-copy-id (ใช้ password ครั้งสุดท้าย)
ssh-copy-id -i ~/.ssh/control_key.pub [email protected]
ssh-copy-id -i ~/.ssh/control_key.pub [email protected]

# ทดสอบว่า login ได้โดยไม่ต้อง password แล้ว
ssh -i ~/.ssh/control_key [email protected]

วิธีที่ 2: Copy ผ่านระบบ automation เอง (เมื่อมี key เริ่มต้นอยู่แล้ว)

# ใช้ authorized_key module กระจาย key ไปพร้อมกันทุก host
ansible all -m authorized_key \
  -a "user=ubuntu key='{{ lookup(\"file\", \"~/.ssh/control_key.pub\") }}'"\
  --ask-pass

ตั้งค่า Private Key ใน ansible.cfg

ระบุ private key ที่ใช้ใน ansible.cfg เพื่อไม่ต้องส่ง flag ทุกครั้ง

[defaults]
inventory = ./inventory.ini
remote_user = ubuntu
private_key_file = ~/.ssh/control_key

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

ระบุ Key ใน Inventory สำหรับ Host ต่างกัน

เมื่อต้องจัดการ server ที่ใช้ key ต่างกัน (เช่น production กับ staging ใช้ key คนละชุด) ระบุ ansible_ssh_private_key_file ในระดับ host หรือ group ได้

[production]
prod1.example.com ansible_ssh_private_key_file=~/.ssh/prod_key

[staging]
staging1.example.com ansible_ssh_private_key_file=~/.ssh/staging_key

[all:vars]
ansible_user=ubuntu

ใช้ SSH Agent เพื่อความสะดวก

ถ้า private key มี passphrase ป้องกัน ใช้ SSH agent เพื่อไม่ต้องพิมพ์ passphrase ทุกครั้ง

# เริ่ม ssh-agent และ add key
eval $(ssh-agent -s)
ssh-add ~/.ssh/control_key

# ตรวจสอบ key ที่ load แล้ว
ssh-add -l

ตรวจสอบความถูกต้องด้วย Ping

หลังตั้งค่าทุกอย่างแล้ว รันคำสั่งนี้เพื่อยืนยันว่า key authentication ทำงานถูกต้องกับ managed node ทุกเครื่อง

ansible all -m ping --private-key ~/.ssh/control_key

# ถ้าตั้งค่า ansible.cfg แล้ว ใช้แค่
ansible all -m ping

สรุป

Key-based SSH authentication คือขั้นตอนที่ขาดไม่ได้ก่อนเริ่มใช้ Ansible ในงานจริง การสร้าง key pair แยกเฉพาะสำหรับงาน automation ช่วยให้ควบคุมและ revoke ได้ง่าย การตั้งค่า private_key_file ใน ansible.cfg ทำให้รัน playbook ได้โดยไม่ต้องระบุ key ทุกครั้ง