Git และ Ansible เป็นคู่ค่าที่ทรงพลังสำหรับการบริหาร Infrastructure as Code (IaC) Git ทำหน้าที่เป็น version control system สำหรับเก็บรักษา configuration files และ Ansible playbooks ขณะที่ Ansible ช่วยในการ deploy และจัดการเซิร์ฟเวอร์หลายเครื่องพร้อมๆ กัน การผสมผสาน Git และ Ansible เข้าด้วยกันช่วยให้การจัดการโครงสร้าง IT มีประสิทธิภาพ ปลอดภัย และสามารถทำซ้ำได้
Git กับ Ansible ทำงานร่วมกันอย่างไร
Git และ Ansible ทำงานร่วมกันในการ Configuration Management ดังนี้:
- Git Repository: เก็บรักษา infrastructure code (Ansible playbooks, inventory files, roles)
- Ansible Playbooks: ใช้ code ใน Git repository เพื่อ deploy configuration ไปยังเซิร์ฟเวอร์
- Version Control: ติดตามประวัติการเปลี่ยนแปลงทั้งหมด พร้อมข้อมูล who changed what when
- CI/CD Pipeline: ทำให้การ deploy เป็นอัตโนมัติและปลอดภัยผ่าน Pull Request review
เก็บ Ansible Playbook ใน Git Repository
การจัดเก็บ Ansible playbooks ใน Git repository ช่วยให้คุณสามารถ:
- ติดตามการเปลี่ยนแปลง configuration ทั้งหมด
- ทำการ rollback ไปยังเวอร์ชันก่อนหน้าได้อย่างง่าย
- ทำให้สามารถ review code ก่อน deploy ได้
- ทำให้ configuration management มี transparency และ accountability
- ใช้ได้กับหลาย environment (development, staging, production)
Version Control สำหรับ Infrastructure Code
Infrastructure as Code (IaC) คือการจัดการโครงสร้าง IT โดยใช้ code แบบเดียวกับการพัฒนา software การเก็บรักษา infrastructure code ใน Git repository ช่วยให้:
- เซิร์ฟเวอร์ใหม่สามารถสร้างได้จาก code เดียวกัน
- ทีมทั้งหมดมีความเข้าใจเหมือนกันว่า infrastructure เป็นอย่างไร
- สามารถ version ได้เช่นเดียวกับ source code
- สามารถ review changes ก่อน apply ไปยัง production
Git Workflow สำหรับ Ansible
workflow ที่ดีควรประกอบด้วยขั้นตอนต่อไปนี้:
- Feature Branch: สร้าง branch ใหม่สำหรับการเปลี่ยนแปลง เช่น
git checkout -b feature/add-nginx-role - Local Testing: ทดสอบ playbook ท้องถิ่นบน development environment
- Dry-run Check: รัน
ansible-playbook -i inventory/development.ini playbooks/site.yml --checkเพื่อดูว่าจะเกิดการเปลี่ยนแปลงอะไร - Commit: commit changes ด้วยข้อความที่ชัดเจน
- Push to Remote: push branch ไปยัง remote repository
- Pull Request: สร้าง pull request เพื่อให้ทีม review
- Code Review: ทีมตรวจสอบและ comment
- Merge: เมื่อ approved ให้ merge ไปยัง main branch
- Deploy to Production: รัน Ansible playbook จาก main branch ไปยัง production
Best Practices: Ansible + Git
- หลีกเลี่ยง Hardcoded Values: ใช้ variables แทน hardcoded values
- ใช้ .gitignore: ไม่ให้เก็บไฟล์ sensitive เช่น SSH keys, secrets
- Meaningful Commit Messages: เขียน commit message ที่อธิบายถึงการเปลี่ยนแปลง
- Role-based Structure: ใช้ Ansible roles เพื่อให้ code เป็นระเบียบ reusable
- Tagging for Releases: ใช้ Git tags สำหรับเขียน release version
- CI/CD Integration: ใช้ GitHub Actions หรือ GitLab CI เพื่อ automate testing และ deployment
- Documentation: เขียน README ใน repository เพื่ออธิบายวิธีใช้
ตัวอย่าง Directory Structure
โครงสร้างที่แนะนำสำหรับ Ansible + Git repository:
infrastructure/
├── README.md
├── .gitignore
├── ansible.cfg
├── inventory/
│ ├── development.ini # Development environment
│ ├── staging.ini # Staging environment
│ └── production.ini # Production environment
├── roles/
│ ├── common/
│ │ ├── tasks/main.yml
│ │ ├── templates/
│ │ └── files/
│ ├── webserver/
│ │ ├── tasks/main.yml
│ │ ├── templates/nginx.conf.j2
│ │ └── handlers/main.yml
│ ├── database/
│ │ ├── tasks/main.yml
│ │ └── templates/postgresql.conf.j2
│ └── monitoring/
│ └── tasks/main.yml
├── playbooks/
│ ├── site.yml # Main playbook
│ ├── deploy.yml # Deployment playbook
│ ├── webservers.yml # Web server configuration
│ └── databases.yml # Database configuration
├── group_vars/
│ ├── all.yml # Variables for all servers
│ ├── webservers.yml # Variables for web servers
│ └── databases.yml # Variables for databases
├── host_vars/
│ ├── prod-web-01.yml
│ ├── prod-db-01.yml
│ └── dev-server.yml
└── requirements.yml # Ansible Galaxy requirements
ตัวอย่าง Playbook พื้นฐาน
ไฟล์ playbooks/site.yml สำหรับ provision infrastructure:
---
- name: Configure all servers
hosts: all
become: yes
roles:
- common
- name: Configure web servers
hosts: webservers
become: yes
roles:
- webserver
vars:
nginx_version: latest
enable_ssl: true
- name: Configure databases
hosts: databases
become: yes
roles:
- database
vars:
postgres_version: 14
max_connections: 200
- name: Configure monitoring
hosts: monitoring
become: yes
roles:
- monitoring
vars:
prometheus_version: latest
grafana_version: latest
ตัวอย่าง Inventory File
ไฟล์ inventory/production.ini สำหรับ production environment:
[webservers]
prod-web-01 ansible_host=10.0.1.10 ansible_user=ubuntu
prod-web-02 ansible_host=10.0.1.11 ansible_user=ubuntu
prod-web-03 ansible_host=10.0.1.12 ansible_user=ubuntu
[databases]
prod-db-01 ansible_host=10.0.1.20 ansible_user=ubuntu
prod-db-02 ansible_host=10.0.1.21 ansible_user=ubuntu
[loadbalancers]
prod-lb-01 ansible_host=10.0.1.5 ansible_user=ubuntu
[monitoring]
prod-mon-01 ansible_host=10.0.1.30 ansible_user=ubuntu
[prod_servers:children]
webservers
databases
loadbalancers
monitoring
[prod_servers:vars]
environment=production
debug_mode=false
backup_enabled=true
ตัวอย่าง Role: Common (Security Hardening)
ไฟล์ roles/common/tasks/main.yml สำหรับการ hardening ระบบ:
---
- name: Update system packages
apt:
update_cache: yes
cache_valid_time: 3600
- name: Install essential packages
apt:
name:
- curl
- wget
- git
- vim
- htop
- build-essential
- fail2ban
state: present
- name: Configure firewall rules
ufw:
rule: allow
port: "{{ item }}"
proto: tcp
loop:
- '22' # SSH
- '80' # HTTP
- '443' # HTTPS
- name: Disable SSH password authentication
lineinfile:
path: /etc/ssh/sshd_config
regexp: '^#?PasswordAuthentication'
line: 'PasswordAuthentication no'
notify: restart ssh
- name: Configure automatic security updates
apt:
name: unattended-upgrades
state: present
- name: Harden sysctl settings
sysctl:
name: "{{ item.key }}"
value: "{{ item.value }}"
sysctl_set: yes
loop:
- { key: 'net.ipv4.ip_forward', value: '0' }
- { key: 'net.ipv4.conf.all.send_redirects', value: '0' }
- { key: 'net.ipv4.conf.all.accept_source_route', value: '0' }
handlers:
- name: restart ssh
service:
name: sshd
state: restarted
ไฟล์ .gitignore สำหรับ Ansible
ระหว่างเก็บ Ansible repository ให้มี .gitignore เพื่อไม่เก็บไฟล์ที่ไม่ควรเก็บ:
# Sensitive files
*.key
*.pem
.ssh/
secrets/
.vault_pass
.vault_password
ansible_vault
# Local development
.venv/
venv/
.env
.env.local
.env.*.local
# Ansible caches
.ansible/
*.pyc
__pycache__/
.pytest_cache/
*.retry
# IDE and editors
.vscode/
.idea/
*.swp
*.swo
*~
.DS_Store
Thumbs.db
# Temporary files
*.tmp
*.bak
.temp/
Workflow ทีมในการบริหาร Git + Ansible
ขั้นตอนที่ดีสำหรับทีมในการ deploy configuration:
- Local Development: ผู้พัฒนา clone repository และสร้าง feature branch
- Testing: ทดสอบ playbook ด้วยคำสั่ง
ansible-playbook --check - Code Review via PR: ส่ง Pull Request เพื่อให้ทีม review
- Automated Testing: GitHub Actions/GitLab CI ทดสอบ syntax และ logic
- Approval: Team lead approve เมื่อพอใจ
- Merge to Main: Merge branch ไปยัง main/master branch
- Staging Deployment: Deploy ไปยัง staging environment เพื่อทดสอบจริง
- Production Deployment: Deploy ไปยัง production พร้อมการ monitoring
- Documentation: อัปเดต changelog และ documentation
CI/CD Pipeline ตัวอย่าง (GitHub Actions)
ไฟล์ .github/workflows/ansible-deploy.yml สำหรับ automate deployment:
name: Ansible Deployment
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.9
- name: Install Ansible
run: pip install ansible
- name: Validate playbook syntax
run: ansible-playbook playbooks/site.yml --syntax-check
- name: Deploy to production
run: |
mkdir -p ~/.ssh
echo "${{ secrets.DEPLOY_SSH_KEY }}" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
ansible-playbook -i inventory/production.ini playbooks/site.yml
- name: Slack notification
if: always()
run: echo "Deployment completed"
- ความปลอดภัยระดับ Enterprise พร้อม DDoS protection
- Full root access สำหรับการจัดการเซิร์ฟเวอร์ด้วย Ansible
- Uptime SLA 99.9% เพื่อการบริการที่เชื่อถือได้
- Support ทีม 24/7 ในการช่วยเหลือปัญหา
- ราคาที่สมเหตุสมผล พร้อมความมืดหลากหลาย
สรุป
การใช้ Git กับ Ansible ร่วมกันนั้นให้ประโยชน์อย่างมากในการบริหาร Infrastructure ด้วยวิธีที่ปลอดภัย มีประสิทธิภาพ และสามารถทำซ้ำได้ ด้วยการ version control infrastructure code ช่วยให้ทีมสามารถทำงานร่วมกันอย่างมีประสิทธิภาพมากขึ้น และลดความเสี่ยงในการ deploy ไปยัง production
