Home » Ansible for Network Automation: Playbooks และ Modules
Ansible for Network Automation: Playbooks และ Modules
Ansible for Network Automation: Playbooks และ Modules
Ansible เป็น automation tool ที่นิยมที่สุดสำหรับ network automation เพราะ agentless (ไม่ต้องติดตั้ง agent บน network devices), ใช้ SSH/NETCONF สำหรับเชื่อมต่อ และเขียน playbooks เป็น YAML ที่อ่านง่าย Ansible มี network modules สำหรับ Cisco, Arista, Juniper, Fortinet และ vendors อื่นๆ
Network engineers หลายคนยังคง configure devices ด้วยมือ ทีละตัว ซึ่งช้า เสี่ยงต่อ human error และไม่ scalable Ansible ช่วยให้ configure, deploy, backup และ validate network devices จำนวนมากพร้อมกัน ด้วย playbook เดียว ลดเวลาจากชั่วโมงเหลือนาที และ ensure consistency
ทำไมต้อง Ansible สำหรับ Network
| Feature |
ประโยชน์ |
| Agentless |
ไม่ต้องติดตั้ง agent บน switch/router (ใช้ SSH ที่มีอยู่แล้ว) |
| YAML Playbooks |
อ่านง่าย เขียนง่าย ไม่ต้องเป็น programmer |
| Idempotent |
รัน playbook กี่ครั้งก็ได้ ผลลัพธ์เหมือนเดิม (safe to re-run) |
| Multi-vendor |
Modules สำหรับ Cisco, Arista, Juniper, Palo Alto, Fortinet, + |
| Version Control |
Playbooks เป็น text files → Git version control |
| Dry Run |
Check mode (–check) ดูว่าจะเปลี่ยนอะไร โดยไม่เปลี่ยนจริง |
Ansible Architecture
| Component |
บทบาท |
| Control Node |
เครื่องที่รัน Ansible (Linux/macOS) |
| Managed Nodes |
Network devices ที่ถูก manage (switches, routers, firewalls) |
| Inventory |
รายการ devices + groups + variables |
| Playbook |
YAML file ที่กำหนดว่าทำอะไรกับ devices ไหน |
| Module |
Unit of work (e.g., ios_config, junos_command) |
| Role |
Reusable collection ของ tasks, vars, templates |
| Collection |
Package ของ modules + roles (e.g., cisco.ios, arista.eos) |
Network Modules
| Vendor |
Collection |
Key Modules |
| Cisco IOS/IOS-XE |
cisco.ios |
ios_config, ios_command, ios_facts, ios_interfaces |
| Cisco NX-OS |
cisco.nxos |
nxos_config, nxos_vlans, nxos_bgp |
| Arista EOS |
arista.eos |
eos_config, eos_command, eos_vlans |
| Juniper Junos |
junipernetworks.junos |
junos_config, junos_command, junos_facts |
| Palo Alto |
paloaltonetworks.panos |
panos_security_rule, panos_nat_rule |
| Fortinet |
fortinet.fortios |
fortios_firewall_policy, fortios_system_interface |
| Generic |
ansible.netcommon |
cli_command, cli_config, netconf_config |
Connection Types
| Connection |
ใช้กับ |
Transport |
| network_cli |
CLI-based devices (IOS, EOS, Junos) |
SSH |
| netconf |
NETCONF-enabled devices |
SSH (port 830) |
| httpapi |
REST API devices (NX-OS, EOS eAPI, Fortinet) |
HTTPS |
Common Use Cases
| Use Case |
Module/Approach |
| Backup configurations |
ios_config + backup parameter → save running-config |
| Deploy VLANs |
ios_vlans / nxos_vlans → create/modify VLANs |
| Configure interfaces |
ios_interfaces → set description, speed, duplex |
| Push ACLs |
ios_acls → deploy access control lists |
| Configure BGP |
ios_bgp_global + ios_bgp_address_family |
| Compliance check |
ios_command + assert → verify config meets standards |
| OS upgrade |
ios_command + copy → upload + reload |
| Generate reports |
ios_facts + template → create HTML/CSV reports |
Jinja2 Templates
| Feature |
ใช้ทำอะไร |
| Variable substitution |
{{ hostname }}, {{ mgmt_ip }} → แทนค่าตาม device |
| Loops |
{% for vlan in vlans %} → สร้าง config หลาย VLANs |
| Conditionals |
{% if device_role == “access” %} → config ตาม role |
| Filters |
{{ ip | ipaddr(‘address’) }} → manipulate data |
| Config templates |
สร้าง device config จาก template + variables |
Best Practices
| Practice |
รายละเอียด |
| ใช้ Roles |
แยก tasks เป็น roles (base, vlans, routing, security) |
| Variables hierarchy |
group_vars → host_vars → playbook vars (override order) |
| Vault สำหรับ secrets |
Encrypt passwords/keys ด้วย ansible-vault |
| Check mode ก่อน |
รัน –check –diff ก่อนทุกครั้ง ดูว่าจะเปลี่ยนอะไร |
| Git version control |
ทุก playbook/role/inventory อยู่ใน Git |
| CI/CD pipeline |
Lint → test → deploy ผ่าน CI/CD (GitLab CI, GitHub Actions) |
| Idempotent tasks |
ใช้ resource modules (ios_vlans) แทน ios_config เมื่อทำได้ |
ทิ้งท้าย: Ansible = Network Automation Standard
Ansible = agentless, YAML playbooks, multi-vendor network_cli (SSH) สำหรับ CLI devices Resource modules (ios_vlans, ios_interfaces) = idempotent Jinja2 templates สำหรับ config generation Roles + Git + CI/CD = Infrastructure as Code สำหรับ network เริ่มจาก backup → VLAN deploy → compliance check
อ่านเพิ่มเติมเกี่ยวกับ Network Automation Python และ Network Monitoring SNMP ที่ siamlancard.com หรือจาก icafeforex.com และ siam2r.com