
Network Automation: ใช้ Ansible จัดการ Switch Router อัตโนมัติ
Network Automation คือการใช้ software จัดการ configure และ monitor อุปกรณ์ network อัตโนมัติ แทนที่จะ SSH เข้าไป configure ทีละตัวด้วยมือ ในองค์กรที่มี switches 50+ ตัว routers 10+ ตัว การ configure ทีละตัวใช้เวลามาก มีโอกาสผิดพลาดสูง และไม่ scalable
Ansible เป็น automation tool ที่นิยมที่สุดสำหรับ network automation เพราะ agentless (ไม่ต้องติดตั้ง agent บนอุปกรณ์) ใช้ SSH ที่มีอยู่แล้ว เขียน playbooks ด้วย YAML ที่อ่านง่าย มี modules สำหรับ Cisco, Aruba, Juniper, FortiGate และอื่นๆ บทความนี้จะสอนวิธีเริ่มต้นใช้ Ansible สำหรับ network automation
ทำไมต้อง Network Automation
| Manual | Automated (Ansible) |
|---|---|
| SSH เข้าทีละตัว | Run playbook ครั้งเดียว config ทุกตัว |
| พิมพ์ commands ทีละบรรทัด | Playbook ทำซ้ำได้ 100% เหมือนกัน |
| ใช้เวลาเป็นชั่วโมง-วัน | ใช้เวลาเป็นนาที |
| Human error สูง (typo, ลืม config) | Consistent ทุกครั้ง |
| ไม่มี version control | Playbooks เก็บใน Git ได้ |
| ไม่มี audit trail | Log ทุกการเปลี่ยนแปลง |
Ansible Architecture
Components
Control Node เครื่องที่ติดตั้ง Ansible (Linux/macOS) เป็นเครื่องที่ run playbooks Managed Nodes อุปกรณ์ network ที่จะถูก configure (switches, routers, firewalls) Inventory ไฟล์ที่ระบุ managed nodes ทั้งหมด (IP, credentials, groups) Playbook ไฟล์ YAML ที่กำหนดว่าจะทำอะไรกับ managed nodes Modules code ที่ Ansible ใช้ทำงานจริง (ios_config, nxos_config, fortios_config)
ติดตั้ง Ansible
บน Ubuntu/Debian
ติดตั้ง Ansible ด้วย pip install ansible หรือ apt install ansible ติดตั้ง network modules เพิ่ม: ansible-galaxy collection install cisco.ios cisco.nxos junipernetworks.junos fortinet.fortios ตรวจสอบ version ด้วย ansible –version
สร้าง Inventory File
ตัวอย่าง inventory.yml
สร้างไฟล์ inventory.yml แบ่ง groups ตามประเภทอุปกรณ์ เช่น group switches มี sw-floor1, sw-floor2, sw-floor3 group routers มี rtr-core1, rtr-core2 แต่ละ host กำหนด ansible_host (IP), ansible_network_os (ios, nxos, junos), ansible_user, ansible_password กำหนด ansible_connection: ansible.netcommon.network_cli สำหรับ CLI-based devices
Playbook ตัวอย่าง
1. Backup Configuration ทุก Switch
สร้าง playbook backup_config.yml ใช้ module ios_command เพื่อรัน show running-config บันทึก output ลงไฟล์ด้วย copy module ตั้ง cron job รัน playbook นี้ทุกวัน ได้ backup config อัตโนมัติทุกวัน ไม่ต้องทำเอง
2. Configure VLAN บนทุก Switch
สร้าง playbook add_vlan.yml ใช้ module ios_vlans หรือ ios_config กำหนด VLAN ID, name, interfaces run playbook ครั้งเดียว VLAN ถูก configure บนทุก switch ในกลุ่ม ถ้ามี 20 switches ก็ config เสร็จภายในนาที
3. Update NTP Server ทุกอุปกรณ์
สร้าง playbook update_ntp.yml ใช้ ios_config ส่ง commands: ntp server 10.10.10.5 no ntp server [old_server] ตรวจสอบผลด้วย ios_command: show ntp associations run playbook เปลี่ยน NTP server ทุกอุปกรณ์ในวินาที
Ansible สำหรับ Network ต่างจาก Server
| คุณสมบัติ | Server Automation | Network Automation |
|---|---|---|
| Connection | SSH + Python on target | SSH + CLI (network_cli) |
| Agent | ไม่ต้อง (agentless) | ไม่ต้อง (agentless) |
| Module execution | บน target host | บน control node |
| Idempotency | ดี (most modules) | ต้องระวัง (บาง modules ไม่ idempotent) |
| Gather facts | setup module | *_facts modules (ios_facts, nxos_facts) |
Jinja2 Templates
ใช้ Template สร้าง Config
Jinja2 templates ให้สร้าง configuration จาก template + variables เหมาะสำหรับสร้าง config ที่โครงสร้างเหมือนกันแต่ค่าต่างกัน (เช่น interface config, VLAN config) ตัวอย่าง: template interface.j2 กำหนด interface name, IP, description จาก variables แต่ละ switch มี variables ต่างกัน (IP ต่างกัน, VLAN ต่างกัน) Ansible render template + variables = configuration ที่ถูกต้องสำหรับแต่ละ switch
Best Practices
ใช้ Git Version Control
เก็บ playbooks, inventory, templates ใน Git repository ทุกการเปลี่ยนแปลงถูก track มี history ว่าใครเปลี่ยนอะไรเมื่อไหร่ สามารถ rollback ได้ถ้ามีปัญหา ทำ code review ก่อน merge เป็น Infrastructure as Code (IaC)
ทดสอบด้วย –check Mode
ก่อน run playbook จริง ใช้ –check (dry run) ดูว่า playbook จะทำอะไร โดยไม่เปลี่ยนแปลง config จริง ใช้ –diff ดูว่า config จะเปลี่ยนอะไร ตรวจสอบให้แน่ใจก่อน run จริง
ใช้ Ansible Vault สำหรับ Credentials
Ansible Vault encrypt ไฟล์ที่มี passwords/secrets ไม่เก็บ password เป็น plaintext ใน inventory หรือ playbooks encrypt ด้วย ansible-vault encrypt secrets.yml decrypt ตอน run ด้วย –ask-vault-pass
เริ่มจากงาน Read-Only
เริ่มต้น automation จาก งานที่ไม่เปลี่ยนแปลง config เช่น backup config, gather facts, show commands สร้างความมั่นใจก่อน แล้วค่อยขยายไป config changes
ทิ้งท้าย: Automation คืออนาคตของ Network Engineering
Network Automation ด้วย Ansible ไม่ได้ทำให้ network engineer ตกงาน แต่ทำให้ทำงานได้เร็วขึ้น ผิดพลาดน้อยลง มีเวลาไปทำงานที่สร้าง value มากขึ้น เริ่มจาก backup config อัตโนมัติ แล้วค่อยขยายไป VLAN provisioning, compliance checks, และ full configuration management
อ่านเพิ่มเติมเกี่ยวกับ SNMP Monitoring และ Network Design ที่ siamlancard.com หรือจาก icafeforex.com และ siam2r.com