Network Automation 101 — Ansible Playbook สำหรับ Cisco / FortiGate
เรียนรู้การใช้ Ansible จัดการ Cisco และ FortiGate แบบอัตโนมัติ ตั้งแต่ Backup Config ไปจนถึง Audit Firmware — เหมาะสำหรับ Network Engineer ใน SMB ที่มีอุปกรณ์ไม่ถึง 20 ตัว
1. ทำไม SMB ที่มีอุปกรณ์ไม่ถึง 20 ตัว ก็ควรเริ่มใช้ Automation#
หลายคนคิดว่า Network Automation เป็นเรื่องของ Data Center ขนาดใหญ่หรือ Enterprise ระดับพัน Device แต่ความเป็นจริงคือ ความผิดพลาดจาก Manual CLI ไม่ได้ขึ้นอยู่กับจำนวนอุปกรณ์
ลองนึกภาพนี้: มี Switch Cisco อยู่ 15 ตัวกระจายอยู่ตาม Branch ทั่วภาคใต้ ทุกครั้งที่ต้องเพิ่ม VLAN ใหม่ ก็ต้อง SSH เข้าไปทีละตัว พิมพ์คำสั่งซ้ำๆ 15 ครั้ง ถ้าพิมพ์ผิดสักตัว อาจทำให้ Traffic หยุดได้ทันที
การใช้ Ansible แม้แค่ 10 Device ช่วยได้ในหลายด้าน:
- ความแม่นยำ — Playbook รันคำสั่งเดิมทุกครั้ง ไม่มีการพิมพ์ผิด
- ความเร็ว — Backup Config 15 ตัวพร้อมกันใช้เวลาไม่ถึง 1 นาที
- การตรวจสอบ — Audit Firmware Version ทุกตัวในคราวเดียว รู้ทันทีว่าตัวไหนล้าหลัง
- เอกสาร — YAML Playbook ทำหน้าที่เป็นเอกสารการตั้งค่าในตัวเอง
สำหรับ Network Engineer ที่ยังทำ CLI Manual อยู่ นี่คือจุดเริ่มต้นที่คุ้มค่าที่สุด
2. Ansible vs Python Script — ทำไม Ansible ถึงเหมาะกับงาน Network มากกว่า#
Python Script ทำ Network Automation ได้เช่นกัน แต่มีข้อแตกต่างสำคัญดังนี้:
| Ansible | Python (Netmiko/Paramiko) | |
|---|---|---|
| Learning curve | ต่ำ — YAML ไม่ต้องเขียน Code | สูง — ต้องมีพื้นฐาน Python |
| Idempotency | มีในตัว (ส่วนใหญ่) | ต้องเขียนเอง |
| Community Module | cisco.ios, fortinet.fortios พร้อมใช้ | ต้องสร้าง Logic เอง |
| Credential Management | Ansible Vault เข้ารหัสในตัว | ต้องจัดการเอง |
| Parallel Execution | รองรับ forks ในตัว | ต้องใช้ threading |
สำหรับ Network Engineer ที่ไม่ได้เป็น Developer Ansible คือทางเลือกที่เหมาะสมกว่ามาก
3. การ Setup ขั้นต่ำ (ansible-core + collection install)#
ต้องการแค่ Linux หรือ macOS สำหรับ Control Node (Windows ใช้ WSL2 ได้)
# Install ansible-core (lightweight, ไม่รวม community package ทั้งหมด)
pip install ansible-core
# Install Network Collection ที่จำเป็น
ansible-galaxy collection install ansible.netcommon
ansible-galaxy collection install cisco.ios
ansible-galaxy collection install fortinet.fortios
# ตรวจสอบ version
ansible --version
ansible-galaxy collection list | grep -E "cisco|fortinet|netcommon"
ใช้ ansible-core แทน ansible เต็มๆ เพื่อลด dependency ที่ไม่จำเป็น แล้วค่อย install เฉพาะ Collection ที่ใช้จริง
โครงสร้าง Project ที่แนะนำ:
network-automation/
├── inventory/
│ ├── hosts.ini
│ └── group_vars/
│ ├── cisco_switches.yml
│ └── fortigate_devices.yml
├── playbooks/
│ ├── backup-config.yml
│ ├── push-vlan.yml
│ └── audit-firmware.yml
└── vault/
└── secrets.yml # เข้ารหัสด้วย Ansible Vault
4. Inventory + Vault สำหรับ credential (อย่า hardcode password ใน playbook!)#
ห้าม hardcode Password หรือ Secret ใน Playbook หรือ Inventory โดยตรง ใช้ Ansible Vault เท่านั้น
ตัวอย่าง Inventory File:
[cisco_switches]
sw-hq-01 ansible_host=192.168.1.11
sw-hq-02 ansible_host=192.168.1.12
sw-branch-01 ansible_host=10.10.1.11
sw-branch-02 ansible_host=10.10.1.12
[fortigate_devices]
fg-hq ansible_host=192.168.1.1
fg-branch-01 ansible_host=10.10.1.1
[cisco_switches:vars]
ansible_network_os=cisco.ios.ios
ansible_connection=network_cli
ansible_user=admin
ansible_password={{ vault_cisco_password }}
ansible_become=yes
ansible_become_method=enable
ansible_become_password={{ vault_cisco_enable }}
[fortigate_devices:vars]
ansible_network_os=fortinet.fortios.fortios
ansible_connection=httpapi
ansible_user=admin
ansible_password={{ vault_fg_password }}
ansible_httpapi_use_ssl=yes
ansible_httpapi_validate_certs=no
สร้างและแก้ไข Vault File:
# สร้าง Vault (จะถามสร้าง Vault Password)
ansible-vault create vault/secrets.yml
# เนื้อหาใน secrets.yml
vault_cisco_password: "YourCiscoPass"
vault_cisco_enable: "YourEnablePass"
vault_fg_password: "YourFGPass"
# รัน Playbook พร้อม Vault
ansible-playbook playbooks/backup-config.yml -i inventory/hosts.ini --ask-vault-pass
5. Playbook ที่ 1: Backup running-config Cisco IOS#
Playbook นี้ดึง Running Config จากทุก Switch แล้วบันทึกเป็นไฟล์พร้อม Timestamp โดยอัตโนมัติ
---
- name: Backup Cisco IOS Running Config
hosts: cisco_switches
gather_facts: false
tasks:
- name: Get running config
cisco.ios.ios_command:
commands:
- show running-config
register: config_output
- name: Save config to local file
ansible.builtin.copy:
content: "{{ config_output.stdout[0] }}"
dest: "./backups/{{ inventory_hostname }}_{{ ansible_date_time.date }}.cfg"
delegate_to: localhost
- name: Show backup status
ansible.builtin.debug:
msg: "Backup completed: {{ inventory_hostname }}"
รัน Playbook:
mkdir -p backups
ansible-playbook playbooks/backup-config.yml \
-i inventory/hosts.ini \
--ask-vault-pass
เพิ่ม Playbook นี้ใน Cron Job รันทุกคืนเวลาเที่ยงคืน เก็บ Config ย้อนหลังได้อัตโนมัติ — ไม่ต้องนึกถึงทุกครั้งก่อนเปลี่ยนแปลง
6. Playbook ที่ 2: Push VLAN ทุก Switch พร้อมกัน#
แทนที่จะ SSH เข้าทีละตัว Playbook นี้จะ Push VLAN Configuration ไปยังทุก Switch ใน Group พร้อมกันในครั้งเดียว
---
- name: Push VLAN Configuration to Cisco Switches
hosts: cisco_switches
gather_facts: false
vars:
vlans_to_add:
- { id: 100, name: "MGMT" }
- { id: 200, name: "USERS" }
- { id: 300, name: "SERVERS" }
- { id: 400, name: "VOIP" }
tasks:
- name: Create VLANs
cisco.ios.ios_vlans:
config:
- vlan_id: "{{ item.id }}"
name: "{{ item.name }}"
state: active
state: merged
loop: "{{ vlans_to_add }}"
- name: Verify VLANs exist
cisco.ios.ios_command:
commands:
- show vlan brief
register: vlan_output
- name: Display VLAN table
ansible.builtin.debug:
var: vlan_output.stdout_lines
Module cisco.ios.ios_vlans รองรับ Idempotency — ถ้า VLAN มีอยู่แล้วก็จะไม่ทำซ้ำ รันกี่ครั้งก็ผลเหมือนเดิม
7. Playbook ที่ 3: Audit Firmware Version FortiGate และแจ้งเตือนตัวที่ล้าหลัง#
Playbook นี้ดึง Firmware Version จาก FortiGate ทุกตัว เปรียบเทียบกับ Minimum Version ที่กำหนดไว้ แล้วแจ้งเตือนอุปกรณ์ที่ยังใช้ Version ล้าหลัง
---
- name: Audit FortiGate Firmware Version
hosts: fortigate_devices
gather_facts: false
vars:
minimum_firmware: "7.4.3"
tasks:
- name: Get system status
fortinet.fortios.fortios_monitor_fact:
selector: "system_status"
register: fg_status
- name: Extract firmware version
ansible.builtin.set_fact:
current_firmware: "{{ fg_status.meta.version }}"
- name: Display firmware version
ansible.builtin.debug:
msg: "{{ inventory_hostname }}: Firmware {{ current_firmware }}"
- name: Flag devices with outdated firmware
ansible.builtin.fail:
msg: >
OUTDATED FIRMWARE on {{ inventory_hostname }}!
Current: {{ current_firmware }} | Minimum required: {{ minimum_firmware }}
when: current_firmware is version(minimum_firmware, '<')
ignore_errors: true
ปรับ minimum_firmware ให้ตรงกับ Version ล่าสุดที่ Fortinet แนะนำเสมอ โดยเฉพาะหลัง Security Advisory
8. ข้อผิดพลาดที่เจอบ่อย (SSH banner, become, idempotency)#
SSH Banner ทำให้ Connection หลุด
Cisco บางรุ่นมี Login Banner ที่ทำให้ Ansible อ่านผลตอบสนองสับสน แก้ได้โดยเพิ่มใน Inventory:
ansible_ssh_common_args='-o StrictHostKeyChecking=no'
หรือระบุใน ansible.cfg:
[defaults]
host_key_checking = False
Become กับ Enable Mode
Cisco IOS ใช้ enable แทน sudo ต้องตั้งค่าให้ถูกต้อง:
ansible_become=yes
ansible_become_method=enable
ansible_become_password={{ vault_cisco_enable }}
ถ้าไม่มี Enable Password ให้ใส่ ansible_become_password="" เว้นว่างไว้
Idempotency ไม่ได้มีทุก Module
Module อย่าง cisco.ios.ios_command (ส่ง raw command) ไม่มี Idempotency — รันซ้ำจะรันคำสั่งซ้ำทุกครั้ง ควรเปลี่ยนไปใช้ Module เฉพาะทาง เช่น ios_vlans หรือ ios_interfaces แทนเมื่อเป็นไปได้
FortiGate SSL Certificate
ถ้าใช้ Self-Signed Certificate บน FortiGate ต้องเพิ่ม:
ansible_httpapi_validate_certs=no
สำหรับ Production environment ควรใช้ Certificate จริงและเปิด Validation
9. สรุป + Action Priority#
Network Automation ด้วย Ansible ไม่ต้องรอให้มีอุปกรณ์ครบร้อยตัวก่อน เริ่มได้เลยแม้มีแค่ 10 Device:
- ติดตั้ง —
ansible-core+ 3 Collection ใช้เวลาไม่เกิน 5 นาที - สร้าง Inventory — จัด Group ตามประเภทอุปกรณ์ (Cisco / FortiGate)
- ตั้ง Vault — เก็บ Credential ให้ปลอดภัยก่อนทำอย่างอื่น
- รัน Backup Playbook — เริ่มจาก Playbook ที่ปลอดภัยที่สุดก่อน (อ่านอย่างเดียว)
- ทดสอบ VLAN Playbook ใน Lab ก่อน Push Production
- ตั้ง Cron Audit — รัน Firmware Audit ทุกสัปดาห์อัตโนมัติ
Playbook ที่ดีคือ Playbook ที่ทีมทุกคนอ่านแล้วเข้าใจ — ไม่ต้องซับซ้อน เริ่มง่ายๆ แล้วค่อยพัฒนาต่อ
C9NETWORK ให้บริการออกแบบและ Deploy Ansible Automation สำหรับ Network Engineer ใน SMB — ตั้งแต่ Setup Control Node, เขียน Playbook เฉพาะระบบ, ไปจนถึง Training ทีม ติดต่อทีมงานเพื่อเริ่มต้น Automation ที่เหมาะกับ Infrastructure ของคุณได้เลย