# Ansible 自動化部署入門:由零開始 15 分鐘搞掂 Server 設定
你有冇試過手動 setup 十部 server,每部都要裝同一堆 packages、改同一個 config file、create 同一堆 user accounts?做完之後手指抽筋、眼花撩亂,仲要驚手殘打錯字搞到 production 死機。阿媽係女人,但重複性工作交俾 Ansible 做,你先可以做返個真正嘅 IT 人。
今日我哋就用 Ansible 做例子,示範點樣喺 15 分鐘內由安裝到成功部署第一部 server,保證你試完返唔到轉頭。
Ansible 係乜?點解你一定要學 Ansible?
Ansible 係 Red Hat 開發嘅開源 IT 自動化工具,屬於 infrastructure as code(IaC)嘅一種。簡單講:你寫一個 YAML playbook,描述你心目中 server 應該係點嘅樣,然後 Ansible 幫你將所有 server 一次過變成你想要嘅狀態。最重要嘅係——Ansible 係 agentless 嘅!即係 target server 唔使裝任何 agent,只需要 SSH 同 Python 就得,呢點同 Puppet、Chef 好唔同。
而家 2026 年,Ansible 已經成為 DevOps 同 SRE 嘅標準工具之一,唔識 Ansible 真係會俾人笑。由 cloud provisioning、application deployment、configuration management 到 security compliance,Ansible 通通搞得掂。
Ansible 安裝教學:三步搞掂 Control Node
首先要搞清楚:Ansible 只需要安裝喺一部機(control node),唔需要喺 target server 裝任何嘢。
Step 1:安裝 Ansible
Ubuntu/Debian:
“`
sudo apt update && sudo apt install ansible -y
“`
macOS:
“`
brew install ansible
“`
RHEL/CentOS/Fedora:
“`
sudo dnf install ansible -y
“`
裝完 check version:
“`
ansible –version
“`
Step 2:準備 Inventory File
Inventory 係 Ansible 嘅核心,定義你要管理嘅 target servers。建立 `~/inventory.ini`:
“`ini
[webservers]
web01 ansible_host=192.168.1.10 ansible_user=ubuntu
web02 ansible_host=192.168.1.11 ansible_user=ubuntu
[dbservers]
db01 ansible_host=192.168.1.20 ansible_user=ubuntu
“`
就係咁簡單!你而家有三部 server 分咗兩個 group。
Step 3:Test SSH Connection(最重要嘅一步)
Ansible 靠 SSH 溝通,所以一定要確保 connectivity:
“`bash
ansible all -i inventory.ini -m ping
“`
見到 `”ping”: “pong”` 就代表成功!如果 fail,check 嚇 SSH key 係咪 set 好、firewall 有冇開放 port 22。
Ansible Playbook 實戰:自動化 Nginx 部署
而家嚟個實戰!寫一個 playbook 自動 install + configure Nginx:
建立 `nginx-playbook.yml`:
“`yaml
—
– name: Deploy Nginx Web Server
hosts: webservers
become: yes
tasks:
– name: Update apt cache
apt:
update_cache: yes
cache_valid_time: 3600
– name: Install Nginx
apt:
name: nginx
state: present
– name: Start and enable Nginx
service:
name: nginx
state: started
enabled: yes
– name: Deploy custom index.html
copy:
content: “
Hello from Ansible!
”
dest: /var/www/html/index.html
“`
執行:
“`bash
ansible-playbook -i inventory.ini nginx-playbook.yml
“`
Ansible 會依次執行每個 task,output 會清楚顯示邊部機成功、邊部 changed、邊部 failed。綠色代表 OK、黃色代表 changed、紅色代表 fail。
Ansible 進階技巧:Variables + Templates
當你要 manage 十部唔同 config 嘅 server 嗰陣,hardcode 值係死路一條。Ansible 嘅 variables + Jinja2 templates 係解決方案。
建立 `vars/main.yml`:
“`yaml
nginx_port: 80
server_name: example.com
“`
然後喺 playbook 引用:
“`yaml
– name: Deploy Nginx config template
template:
src: nginx.conf.j2
dest: /etc/nginx/nginx.conf
“`
Templates 令 Ansible 真正可以 scale,同一套 playbook 可以部署去 staging / production / DR 三個 environment,只需換 variables file。
Ansible 實用 Tips
1. **用 `–check` mode 做 dry run**:唔想真係執行但又想知會改咩 → `ansible-playbook –check playbook.yml`
2. **用 `ansible-vault` 加密 secrets**:password、API key 千祈唔好 plain text → `ansible-vault encrypt secrets.yml`
3. **用 Roles 組織大型 project**:當 playbook 超過 100 行,一定要拆成 roles(類似 module 化)
4. **用 `ansible-lint` 檢查語法**:避免低級錯誤 deploy 上去先爆
Ansible vs 其他工具
你可能問:咁多 automation tools(Puppet, Chef, Salt, Terraform),點解揀 Ansible?
– **vs Puppet/Chef**:Ansible agentless,唔使 maintain agent infrastructure
– **vs Terraform**:Terraform 做 infrastructure provisioning(create VM、network),Ansible 做 configuration management(裝 software、改 config),兩者通常一齊用
– **vs Bash scripts**:Ansible 係 idempotent(執行 N 次結果一樣),Bash script 行第二次可能爆 error
總結:Ansible 值唔值得學?
絕對值得。Ansible 係現代 IT 運維嘅基礎技能,無論你係 sysadmin、DevOps engineer、cloud architect 定 security engineer,識 Ansible 可以幫你慳返幾百個鐘嘅重複勞動。由今日開始,試嚇將你每日做嘅 repetitive tasks 寫成 Ansible playbook,一個月後你會發現自己多咗好多時間做真正有價值嘅工作。
而家就去 `apt install ansible` 啦!
📎 參考資料: Ansible 官方文檔
📌 延伸閲讀:
Ansible 自動化部署入門|
Docker Container 安全加固|
SSH 安全加固實戰



