Debian 环境中自动化部署的实用方案
一 场景与术语澄清
二 典型自动化部署路径
三 快速上手示例
deploy-nginx/
├── inventory.ini
└── site.yml
[web]
192.168.1.10
192.168.1.11
- hosts: web
become: yes
tasks:
- name: Install Nginx
apt:
name: nginx
state: present
update_cache: yes
- name: Ensure Nginx is enabled and running
service:
name: nginx
state: started
enabled: yes
- name: Deploy index.html
copy:
content: "<h1>Deployed by Ansible</h1>"
dest: /var/www/html/index.html
mode: '0644'
ansible-playbook -i inventory.ini site.yml
#!/usr/bin/env bash
set -e
REPO=/var/www/myapp
BRANCH=${1:-main}
cd "$REPO"
git fetch --all
git reset --hard "origin/$BRANCH"
systemctl restart myapp || true
#!/usr/bin/env bash
while read old new ref; do
if [[ "$ref" == "refs/heads/main" ]]; then
/opt/deploy.sh main
fi
done
四 安全与运维要点