Ansible for Network: Automate Config Management ด้วย Playbooks
Ansible เป็น automation tool ยอดนิยมที่ใช้จัดการ configuration ของ network devices ได้อย่างมีประสิทธิภาพ ไม่ต้องติดตั้ง agent บนอุปกรณ์ (agentless) ใช้ SSH เชื่อมต่อ เขียน playbooks เป็น YAML ที่อ่านง่าย รองรับ multi-vendor (Cisco, Juniper, Arista, Palo Alto และอื่นๆ) เหมาะสำหรับ config management, compliance checking และ deployment automation
ต่างจาก Netmiko ที่เป็น Python library สำหรับเขียน scripts Ansible เป็น framework ที่จัดการ inventory, variables, templates, roles และ error handling ให้ ทำให้ automation เป็นระบบมากกว่า reusable มากกว่า และ scale ได้ดีกว่า บทความนี้จะสอนวิธีใช้ Ansible สำหรับ network automation
ทำไมใช้ Ansible สำหรับ Network
| คุณสมบัติ | รายละเอียด |
|---|---|
| Agentless | ไม่ต้องติดตั้งอะไรบนอุปกรณ์ ใช้ SSH/NETCONF |
| YAML Playbooks | อ่านง่าย เขียนง่าย ไม่ต้องเป็น programmer |
| Idempotent | รัน playbook ซ้ำกี่ครั้งก็ได้ ผลลัพธ์เหมือนเดิม |
| Multi-vendor | รองรับ Cisco, Juniper, Arista, F5, Palo Alto ฯลฯ |
| Inventory Management | จัดการ device list + variables อย่างเป็นระบบ |
| Templates (Jinja2) | สร้าง config จาก template + variables |
| Roles | Reusable automation packages |
| Ansible Galaxy | Community shared roles + collections |
Ansible Architecture
| Component | บทบาท |
|---|---|
| Control Node | เครื่องที่รัน Ansible (Linux/Mac) |
| Managed Nodes | Network devices ที่ถูกจัดการ |
| Inventory | รายชื่อ devices + groups + variables |
| Playbook | YAML file กำหนด tasks ที่ต้องทำ |
| Module | หน่วยงานย่อย เช่น ios_config, junos_command |
| Collection | Package ของ modules สำหรับ vendor เฉพาะ |
Network Modules
| Vendor | Collection | Modules ตัวอย่าง |
|---|---|---|
| Cisco IOS | cisco.ios | ios_config, ios_command, ios_facts, ios_vlans |
| Cisco NX-OS | cisco.nxos | nxos_config, nxos_vlans, nxos_interfaces |
| Juniper Junos | junipernetworks.junos | junos_config, junos_command, junos_facts |
| Arista EOS | arista.eos | eos_config, eos_command, eos_vlans |
| Palo Alto | paloaltonetworks.panos | panos_security_rule, panos_nat_rule |
| F5 BIG-IP | f5networks.f5_modules | bigip_pool, bigip_virtual_server |
Inventory File
จัดกลุ่ม Devices
INI format: แบ่งกลุ่ม devices เช่น [switches], [routers], [firewalls] กำหนด host variables เช่น ansible_host, ansible_network_os ใช้ group variables สำหรับ credentials และ connection settings
YAML format: เหมือน INI แต่เขียนเป็น YAML ดูเป็นระเบียบกว่า กำหนด children groups สำหรับ nested grouping
Playbook ตัวอย่าง
Use Cases
| Use Case | Module | วิธี |
|---|---|---|
| Backup Config ทุก device | ios_command / ios_config | รัน show running-config → save to file |
| Deploy VLAN ทุก switch | ios_vlans | กำหนด VLAN list ใน variables → apply ทุก switch |
| Update NTP servers | ios_config | ส่ง config lines สำหรับ NTP |
| Compliance Check | ios_command + assert | ตรวจ config → assert ว่าตรงกับ standard |
| Generate Config from Template | template + ios_config | Jinja2 template + variables → generate + push config |
| Firmware Upgrade | ios_command + copy | Copy firmware → verify → reload |
Jinja2 Templates
สร้าง Config แบบ Dynamic
Jinja2 ช่วยสร้าง config จาก template + variables: กำหนด variables ใน inventory หรือ vars file Template ใช้ {{ variable }} และ {% for %} loops สร้าง config ที่ต่างกันสำหรับแต่ละ device จาก template เดียวกัน ตัวอย่าง: Interface config template ที่ใช้ variables สำหรับ IP, description, VLAN
Ansible vs Other Tools
| Tool | ประเภท | จุดเด่น | เมื่อไหร่ใช้ |
|---|---|---|---|
| Ansible | Automation framework | Agentless, YAML, multi-vendor, easy | Config management at scale |
| Netmiko | Python library | Simple SSH automation | Quick scripts, ad-hoc tasks |
| Nornir | Python framework | Threaded, Python-native | Complex Python-based workflows |
| Terraform | IaC tool | State management, cloud-native | Cloud infrastructure provisioning |
| Salt | Automation tool | Event-driven, fast (ZeroMQ) | Large-scale real-time automation |
Best Practices
| Practice | รายละเอียด |
|---|---|
| ใช้ Ansible Vault สำหรับ secrets | Encrypt passwords/keys อย่า plaintext ใน playbook |
| ใช้ roles สำหรับ reusability | แบ่ง playbook เป็น roles ที่ reuse ได้ |
| Version control ทุกอย่าง | Playbooks, inventory, templates ต้องอยู่ใน Git |
| ใช้ –check mode ก่อน apply | Dry-run ดูว่าจะเปลี่ยนอะไรก่อน apply จริง |
| Backup config ก่อนเปลี่ยน | Task แรกใน playbook = backup running-config |
| ใช้ tags สำหรับ selective runs | รัน เฉพาะ tasks ที่ต้องการ (ไม่ต้องรันทั้ง playbook) |
| Test ใน lab ก่อน production | ใช้ GNS3/EVE-NG/CML ทดสอบ playbooks ก่อน |
AWX / Ansible Tower
GUI + Scheduling + RBAC
| Feature | รายละเอียด |
|---|---|
| Web GUI | รัน playbooks จาก browser (ไม่ต้อง CLI) |
| Scheduling | ตั้งเวลารัน playbook อัตโนมัติ (เช่น backup ทุกคืน) |
| RBAC | กำหนดสิทธิ์ว่าใครรันอะไรได้ |
| Logging/Audit | บันทึกทุก playbook run (who, what, when) |
| API | Integrate กับ systems อื่นผ่าน REST API |
ทิ้งท้าย: Ansible = มาตรฐาน Network Automation
Ansible เป็น automation tool ที่เหมาะกับ network มากที่สุด agentless + YAML + multi-vendor เริ่มจาก backup config → deploy changes → compliance checks ใช้ Jinja2 templates สร้าง config แบบ dynamic เก็บทุกอย่างใน Git (Infrastructure as Code)
อ่านเพิ่มเติมเกี่ยวกับ Python Netmiko และ Network Config Backup ที่ siamlancard.com หรือจาก icafeforex.com และ siam2r.com