Ansible 自動化部署 Linux Server 新手入門教學

# 用 Ansible 自動化部署 Linux Server:新手入門 Step-by-Step 做 IT 嘅你一定試過呢個情況:老細話要 deploy 十部新 server,你要逐部 SSH 入去裝 package、改 config、開 service。做第一部覺得 OK,做到第五部開始懷疑人生,做完十部已經想辭職。 今日就同大家介紹 **Ansible** — 一個可以幫你同時管理幾十甚至幾百...

# 用 Ansible 自動化部署 Linux Server:新手入門 Step-by-Step

做 IT 嘅你一定試過呢個情況:老細話要 deploy 十部新 server,你要逐部 SSH 入去裝 package、改 config、開 service。做第一部覺得 OK,做到第五部開始懷疑人生,做完十部已經想辭職。

今日就同大家介紹 **Ansible** — 一個可以幫你同時管理幾十甚至幾百部 server 嘅自動化工具。唔使裝 agent,用 SSH 就得,學識佢之後你嘅工作效率會提升 N 倍。

## 乜嘢係 Ansible?

Ansible 係 Red Hat 開發嘅開源 IT 自動化平台,用 YAML 寫 playbook 定義你想做嘅嘢,然後佢會自動喺所有 target server 上面執行。最正係佢 **agentless** — target server 只需要有 Python 同 SSH,唔使裝任何額外軟件。

## Ansible 核心概念

開始之前,你要明三個核心概念:

– **Inventory**:定義你要管理嘅 server list(可以係 static file 或者 dynamic inventory)
– **Playbook**:用 YAML 寫嘅自動化劇本,定義執行步驟
– **Module**:Ansible 內置嘅功能模組(例如 `apt` 裝 package、`copy` 抄檔案、`service` 管理 service)

## Step 1:安裝 Ansible

喺你嘅 control node(即係你部 laptop 或者跳板機)安裝 Ansible:

# Ubuntu / Debian
sudo apt update && sudo apt install -y ansible

# macOS
brew install ansible

# 檢查安裝
ansible --version

## Step 2:設定 Inventory

建立一個 inventory file,列出你要管理嘅 server:

mkdir ~/ansible-lab && cd ~/ansible-lab

cat > inventory.ini << 'EOF'
[webservers]
web01 ansible_host=192.168.1.101 ansible_user=admin
web02 ansible_host=192.168.1.102 ansible_user=admin

[dbservers]
db01 ansible_host=192.168.1.201 ansible_user=admin
EOF

## Step 3:測試 SSH 連接

執行之前,先確認 Ansible 可以連到所有 server:

# 用 ping module 測試連接
ansible all -i inventory.ini -m ping

# 應該見到類似輸出:
# web01 | SUCCESS => {"changed": false, "ping": "pong"}

如果出現 permission denied,你可能需要設定 SSH key:

ssh-copy-id admin@192.168.1.101
ssh-copy-id admin@192.168.1.102

## Step 4:寫你第一個 Playbook

而家寫一個簡單 playbook,幫所有 webserver 安裝 Nginx:

cat > deploy-nginx.yml << 'EOF'
---
- name: Deploy Nginx on Web Servers
  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: "<h1>Deployed by Ansible!</h1>"
        dest: /var/www/html/index.html
        owner: www-data
        group: www-data
        mode: '0644'
EOF

## Step 5:執行 Playbook

ansible-playbook -i inventory.ini deploy-nginx.yml

執行完之後,你嘅 webserver 就會自動裝好 Nginx 兼有 custom homepage。就係咁簡單!

## 進階技巧:用 Variables 同 Templates

當你管理多個環境(dev / staging / production),可以用 variables 同 Jinja2 templates:

cat > nginx.conf.j2 << 'EOF'
server {
    listen 80;
    server_name {{ server_name }};
    root /var/www/{{ app_name }};
}
EOF

Playbook 入面用 `template` module 取代 `copy`,Ansible 會自動將 variable 填入 template。

## 總結

Ansible 係每個 Linux admin 必學嘅工具。由幾部 server 到幾百部,同一套 playbook 都搞得掂。最緊要係佢嘅 **idempotent** 特性 — 同一個 playbook 行幾多次結果都一樣,唔會重複裝嘢或者搞亂 config。

🔗 參考資料:NVD NIST 漏洞資料庫

聽日開始,唔好再手動 SSH 逐部機做嘢啦。寫個 playbook,飲杯咖啡等佢自己跑完,呢個先係專業 IT 人嘅工作方式。

#Ansible #技術分享 #SSH #Server #Linux

Ansible 自動化部署 Linux Server 新手入門教學 - 文章重點速覽 infographic