Home » Network Infrastructure as Code: Terraform และ Ansible สำหรับ Network
Network Infrastructure as Code: Terraform และ Ansible สำหรับ Network
Network Infrastructure as Code: Terraform และ Ansible สำหรับ Network
Infrastructure as Code (IaC) สำหรับ network คือการ define, provision และ manage network infrastructure ผ่าน code แทนการ config ทีละ device ด้วย CLI Terraform ใช้สำหรับ provisioning (สร้าง/ลบ resources) ส่วน Ansible ใช้สำหรับ configuration management (config devices ที่มีอยู่แล้ว) ทั้งสองใช้ร่วมกันได้
การ manage network ด้วย CLI ทีละ device ไม่ scale เมื่อมีหลายร้อยตัว เสี่ยงต่อ human error ไม่มี version control และ audit trail IaC แก้ปัญหาเหล่านี้: config เป็น code ใน Git, reproducible, testable และ auditable ทำให้ network operations reliable และ scalable
Terraform vs Ansible
| Feature |
Terraform |
Ansible |
| Primary Use |
Provisioning (create/destroy) |
Configuration management (configure existing) |
| Approach |
Declarative (define desired state) |
Procedural/Declarative hybrid (define tasks) |
| State |
Maintains state file (track resources) |
Stateless (no state file) |
| Agent |
Agentless (API-based) |
Agentless (SSH/NETCONF/API) |
| Language |
HCL (HashiCorp Configuration Language) |
YAML (playbooks) |
| Network Use |
Cloud networking (VPC, subnets, firewalls) |
Device config (routers, switches, firewalls) |
| Idempotent |
Yes (built-in) |
Yes (if modules written correctly) |
Terraform for Network
| Provider |
ใช้ทำอะไร |
| AWS (VPC) |
Create VPCs, subnets, route tables, security groups, NACLs |
| Azure (VNet) |
Create VNets, subnets, NSGs, route tables, VPN gateways |
| GCP |
Create VPC networks, firewall rules, Cloud Router |
| Palo Alto (PAN-OS) |
Manage firewall rules, NAT, zones, interfaces |
| Fortinet (FortiOS) |
Manage firewall policies, VPN, interfaces |
| Cisco ACI |
Manage ACI fabric (tenants, BDs, EPGs, contracts) |
| Cloudflare |
DNS records, firewall rules, page rules |
Ansible for Network
| Module Collection |
ใช้กับ |
| cisco.ios |
Cisco IOS routers/switches (ios_config, ios_command) |
| cisco.nxos |
Cisco Nexus (nxos_config, nxos_vlan) |
| arista.eos |
Arista EOS switches |
| junipernetworks.junos |
Juniper Junos devices |
| ansible.netcommon |
Common network modules (cli_command, netconf) |
| fortinet.fortios |
FortiGate firewalls |
| paloaltonetworks.panos |
Palo Alto firewalls |
Ansible Network Concepts
| Concept |
รายละเอียด |
| Inventory |
List ของ devices (hosts) จัดกลุ่มตาม site, role, vendor |
| Playbook |
YAML file ที่ define tasks ที่ต้อง run |
| Role |
Reusable collection ของ tasks (e.g., role: base_config) |
| Template (Jinja2) |
Template สำหรับ config generation (variables + logic) |
| Vault |
Encrypt sensitive data (passwords, keys) |
| network_cli |
Connection plugin สำหรับ CLI-based devices (SSH) |
| netconf |
Connection plugin สำหรับ NETCONF-based devices |
Terraform Workflow
| Step |
Command |
ทำอะไร |
| 1. Write |
(edit .tf files) |
Define desired state ใน HCL |
| 2. Init |
terraform init |
Download providers + initialize backend |
| 3. Plan |
terraform plan |
Preview changes (what will be created/modified/destroyed) |
| 4. Apply |
terraform apply |
Apply changes (create/modify resources) |
| 5. Destroy |
terraform destroy |
Remove all managed resources |
GitOps for Network
| Practice |
วิธีทำ |
| Git as Single Source of Truth |
ทุก config/IaC อยู่ใน Git repository |
| Pull Request Review |
Config changes ผ่าน PR review ก่อน merge |
| CI/CD Pipeline |
Automated: lint → validate → plan → test → apply |
| Automated Testing |
Test config ก่อน deploy (syntax, compliance, simulation) |
| Rollback |
git revert → re-apply = rollback config |
| Audit Trail |
Git history = who changed what, when, why |
CI/CD Pipeline for Network
| Stage |
Tool |
ทำอะไร |
| Lint |
yamllint, terraform fmt |
Check syntax + formatting |
| Validate |
terraform validate, ansible-lint |
Validate config correctness |
| Plan/Dry-Run |
terraform plan, ansible –check |
Preview changes without applying |
| Test |
Molecule, Batfish, pytest |
Test config ใน lab/simulation |
| Approve |
Manual approval gate |
Human review before production |
| Apply |
terraform apply, ansible-playbook |
Deploy to production |
| Verify |
Automated tests, monitoring |
Confirm changes work correctly |
Best Practices
| Practice |
รายละเอียด |
| Version control everything |
ทุก config, playbook, template อยู่ใน Git |
| Use variables/templates |
ใช้ Jinja2 templates + variables (ไม่ hardcode) |
| Test before deploy |
Lab testing + dry-run ก่อน production |
| Encrypt secrets |
Ansible Vault / HashiCorp Vault สำหรับ credentials |
| Modular design |
Terraform modules + Ansible roles = reusable components |
| State management |
Terraform remote state (S3, Azure Blob) + state locking |
| Incremental adoption |
เริ่มจาก read-only automation → ค่อยเพิ่ม config changes |
ทิ้งท้าย: IaC = Reliable, Scalable Network Operations
IaC = define network config as code (version controlled, testable) Terraform = provisioning (cloud networking, firewalls) Ansible = configuration management (routers, switches, firewalls) GitOps: Git → PR review → CI/CD → deploy → verify เริ่มจาก read-only → templates → config changes → full CI/CD
อ่านเพิ่มเติมเกี่ยวกับ Python Network Automation และ Network Monitoring SNMP ที่ siamlancard.com หรือจาก icafeforex.com และ siam2r.com