
Ansible Network Automation: จัดการ Config Switch Router อัตโนมัติ
Ansible เป็น automation tool ที่นิยมที่สุดสำหรับจัดการ configuration ของอุปกรณ์ network (switches, routers, firewalls) แบบอัตโนมัติ Ansible เป็น agentless ไม่ต้องติดตั้ง software บนอุปกรณ์ ใช้ SSH เชื่อมต่อ เขียน playbook เป็น YAML อ่านง่าย เหมาะสำหรับ network engineers ที่ไม่ถนัด programming
ในองค์กรที่มีอุปกรณ์ network หลายสิบหรือหลายร้อยตัว การ configure ทีละตัวด้วยมือ เสียเวลา ผิดพลาดง่าย ไม่ consistent Ansible แก้ปัญหานี้ด้วยการ automate ทุกอย่างตั้งแต่ deploy config, backup, compliance check จนถึง firmware upgrade
ทำไมต้อง Ansible
| Manual | Ansible |
|---|---|
| SSH เข้าทีละตัว | Run playbook ครั้งเดียว ทำทุกตัวพร้อมกัน |
| พิมพ์ commands ซ้ำๆ | เขียน playbook ครั้งเดียว ใช้ซ้ำได้ |
| อาจลืมหรือพิมพ์ผิด | Consistent ทุกครั้ง (idempotent) |
| ไม่มี audit trail | Playbook เป็น documentation + version control |
| Backup config ด้วยมือ | Schedule backup อัตโนมัติ |
Ansible Architecture
Components
Control Node: เครื่องที่รัน Ansible (Linux หรือ macOS ติดตั้ง Ansible) Inventory: ไฟล์ที่ระบุ managed hosts (switches, routers) + groups + variables Playbook: ไฟล์ YAML ที่กำหนด tasks ที่จะทำ Modules: ชุดคำสั่งสำเร็จรูปสำหรับแต่ละ vendor (ios_config, nxos_config, eos_config, junos_config) Roles: collection ของ tasks, templates, variables ที่จัดกลุ่มเป็น reusable unit
Inventory
กำหนดอุปกรณ์
Inventory file (hosts.yml) กำหนดอุปกรณ์แยกตาม group: group “switches” มี switch 10 ตัว group “routers” มี router 3 ตัว group “firewalls” มี firewall 2 ตัว แต่ละอุปกรณ์กำหนด: hostname/IP, ansible_network_os (ios, nxos, eos), ansible_user, ansible_password, ansible_connection (network_cli)
Playbook พื้นฐาน
ตัวอย่าง: Backup Config
Playbook สำหรับ backup running-config จาก Cisco IOS switches ทุกตัว: ใช้ module ios_command ส่งคำสั่ง “show running-config” เก็บ output ลง variable ใช้ module copy เขียน output ลงไฟล์ ตั้งชื่อไฟล์ตาม hostname + วันที่ ผลลัพธ์: backup config ทุกตัวในไม่กี่วินาที แทนที่จะ SSH เข้าทีละตัว
ตัวอย่าง: Deploy VLAN
สร้าง VLAN ใหม่ บน switches ทุกตัวพร้อมกัน: ใช้ module ios_vlans กำหนด VLAN ID, name, state (active) Ansible จะ SSH เข้าแต่ละ switch ส่งคำสั่ง idempotent: ถ้า VLAN มีอยู่แล้ว จะไม่ทำซ้ำ ถ้ายังไม่มี จะสร้างใหม่
ตัวอย่าง: Configure NTP
ตั้ง NTP server บนอุปกรณ์ทุกตัว: ใช้ ios_config module ส่ง config lines: ntp server 10.0.20.1 ntp server 10.0.20.2 ทำพร้อมกันทุกตัว ใน 1 playbook run
Jinja2 Templates
Dynamic Configuration
Jinja2 templates ทำให้สร้าง config ที่แตกต่างกันสำหรับแต่ละอุปกรณ์ ตัวอย่าง: template สำหรับ interface config ที่มี variables (IP, subnet, description) แต่ละ switch มี variables ต่างกัน (host_vars) Ansible render template แล้ว push config ไปยังแต่ละ switch ได้ config ที่ถูกต้องสำหรับแต่ละตัว
Network Modules ที่สำคัญ
| Vendor | Modules | Connection |
|---|---|---|
| Cisco IOS | ios_config, ios_command, ios_facts, ios_vlans, ios_interfaces | network_cli (SSH) |
| Cisco NX-OS | nxos_config, nxos_command, nxos_vlans | network_cli / httpapi |
| Arista EOS | eos_config, eos_command, eos_vlans | network_cli / httpapi |
| Juniper JunOS | junos_config, junos_command | netconf |
| Fortinet | fortios modules | httpapi |
| Palo Alto | panos modules | httpapi |
Best Practices
Version Control
เก็บ playbooks ใน Git (GitHub, GitLab) ทุก config change ต้อง commit ก่อน run สามารถ rollback ได้ถ้ามีปัญหา ทุกคนเห็น change history รู้ว่าใครเปลี่ยนอะไร เมื่อไหร่
Testing
ใช้ check mode (–check) ก่อน run จริง เพื่อดูว่า playbook จะทำอะไร (dry run) ใช้ –diff เพื่อดูว่า config จะเปลี่ยนอะไรบ้าง test ใน lab environment ก่อน production
Idempotency
Playbook ที่ดีต้อง idempotent run กี่ครั้งก็ได้ผลลัพธ์เหมือนกัน ไม่สร้าง duplicate config ถ้า config ตรงกับ desired state แล้ว จะไม่เปลี่ยนแปลง
Ansible vs Alternatives
| Tool | จุดเด่น | เหมาะกับ |
|---|---|---|
| Ansible | Agentless, YAML, ง่าย, community ใหญ่ | Network automation ทุกขนาด |
| Terraform | Infrastructure as Code, state management | Cloud infrastructure, ไม่ใช่ network config |
| Nornir | Python-native, flexible | Python developers, complex automation |
| NAPALM | Multi-vendor abstraction | ใช้ร่วมกับ Ansible/Nornir |
ทิ้งท้าย: Automate หรือถูกทิ้งไว้ข้างหลัง
Network Automation ด้วย Ansible ไม่ใช่อนาคตอีกต่อไป มันคือปัจจุบัน เริ่มจาก backup config อัตโนมัติ (ง่ายที่สุด) แล้วค่อยเพิ่ม VLAN deploy, NTP config, compliance check เก็บ playbooks ใน Git ใช้ check mode ก่อน run จริง
อ่านเพิ่มเติมเกี่ยวกับ Network Automation Python Netmiko และ SNMP Monitoring ที่ siamlancard.com หรือจาก icafeforex.com และ siam2r.com