214 lines
4.5 KiB
Markdown
214 lines
4.5 KiB
Markdown
# Ansible批量部署工具
|
|
|
|
基于 Go + Ansible 的批量运维部署系统,支持批量执行命令、Playbook管理、主机分组等功能。
|
|
|
|
## 功能特性
|
|
|
|
- 🌐 **Web界面** - 简洁美观的Web管理界面
|
|
- 🖥️ **主机管理** - 支持添加、删除、编辑主机
|
|
- 📁 **分组管理** - 灵活的主机组管理
|
|
- 💻 **命令执行** - 批量并行/串行执行命令
|
|
- 📜 **Playbook管理** - 预置Playbook模板,支持快速执行
|
|
- 📊 **任务跟踪** - 实时任务进度监控
|
|
- 🔄 **自动刷新** - 数据自动同步
|
|
|
|
## 快速开始
|
|
|
|
### 1. 安装依赖
|
|
|
|
```bash
|
|
# 安装Go
|
|
curl -fsSL https://go.dev/dl/go1.21.linux-amd64.tar.gz | tar -C /usr/local -xzf -
|
|
|
|
# 安装Ansible
|
|
pip install ansible
|
|
|
|
# 安装SSH
|
|
apt install openssh-client # Debian/Ubuntu
|
|
yum install openssh-clients # CentOS/RHEL
|
|
```
|
|
|
|
### 2. 构建项目
|
|
|
|
```bash
|
|
cd /root/ansible-deploy
|
|
go mod tidy
|
|
go build -o ansible-deploy cmd/main.go
|
|
```
|
|
|
|
### 3. 配置SSH免密登录
|
|
|
|
```bash
|
|
# 生成SSH密钥
|
|
ssh-keygen -t rsa
|
|
|
|
# 复制到目标主机
|
|
ssh-copy-id user@hostname
|
|
```
|
|
|
|
### 4. 启动服务
|
|
|
|
```bash
|
|
# 默认端口8080
|
|
./ansible-deploy
|
|
|
|
# 自定义端口
|
|
./ansible-deploy -port 9000
|
|
|
|
# 自定义配置
|
|
./ansible-deploy -config /path/to/config.yaml
|
|
```
|
|
|
|
### 5. 访问Web界面
|
|
|
|
打开浏览器访问: `http://localhost:8080`
|
|
|
|
## 配置说明
|
|
|
|
配置文件位于 `config/config.yaml`:
|
|
|
|
```yaml
|
|
# Ansible路径
|
|
ansible_path: /usr/bin/ansible
|
|
|
|
# 资产清单目录
|
|
inventory_dir: ~/ansible-deploy/inventory
|
|
|
|
# Playbook目录
|
|
playbook_dir: ~/ansible-deploy/playbooks
|
|
|
|
# 日志目录
|
|
log_dir: ~/ansible-deploy/logs
|
|
|
|
# SSH超时(秒)
|
|
ssh_timeout: 30
|
|
|
|
# 最大并发数
|
|
max_parallelism: 10
|
|
```
|
|
|
|
## API接口
|
|
|
|
### 主机管理
|
|
|
|
| 方法 | 路径 | 说明 |
|
|
|------|------|------|
|
|
| GET | `/api/hosts` | 获取主机列表 |
|
|
| POST | `/api/hosts` | 添加主机 |
|
|
| PUT | `/api/hosts/:id` | 更新主机 |
|
|
| DELETE | `/api/hosts/:id` | 删除主机 |
|
|
| POST | `/api/hosts/test/:id` | 测试连接 |
|
|
|
|
### 主机组
|
|
|
|
| 方法 | 路径 | 说明 |
|
|
|------|------|------|
|
|
| GET | `/api/groups` | 获取组列表 |
|
|
| POST | `/api/groups` | 创建组 |
|
|
| PUT | `/api/groups/:name` | 更新组 |
|
|
| DELETE | `/api/groups/:name` | 删除组 |
|
|
|
|
### 命令执行
|
|
|
|
| 方法 | 路径 | 说明 |
|
|
|------|------|------|
|
|
| POST | `/api/command/execute` | 执行命令 |
|
|
| POST | `/api/command/batch` | 批量执行 |
|
|
|
|
### Playbook
|
|
|
|
| 方法 | 路径 | 说明 |
|
|
|------|------|------|
|
|
| GET | `/api/playbooks` | 列出Playbook |
|
|
| POST | `/api/playbooks/execute` | 执行Playbook |
|
|
|
|
### 任务
|
|
|
|
| 方法 | 路径 | 说明 |
|
|
|------|------|------|
|
|
| GET | `/api/tasks` | 获取任务列表 |
|
|
| GET | `/api/tasks/:id` | 获取任务详情 |
|
|
| DELETE | `/api/tasks/:id` | 取消任务 |
|
|
|
|
## 使用示例
|
|
|
|
### 添加主机
|
|
|
|
```bash
|
|
curl -X POST http://localhost:8080/api/hosts \
|
|
-H "Content-Type: application/json" \
|
|
-d '{
|
|
"name": "web-server-01",
|
|
"ip": "192.168.1.100",
|
|
"port": 22,
|
|
"username": "root",
|
|
"password": "your-password",
|
|
"groups": ["webservers"]
|
|
}'
|
|
```
|
|
|
|
### 批量执行命令
|
|
|
|
```bash
|
|
curl -X POST http://localhost:8080/api/command/batch \
|
|
-H "Content-Type: application/json" \
|
|
-d '{
|
|
"hosts": ["web1", "web2", "web3"],
|
|
"command": "df -h",
|
|
"parallel": true
|
|
}'
|
|
```
|
|
|
|
### 执行Playbook
|
|
|
|
```bash
|
|
curl -X POST http://localhost:8080/api/playbooks/execute \
|
|
-H "Content-Type: application/json" \
|
|
-d '{
|
|
"name": "update-packages",
|
|
"hosts": ["all"],
|
|
"extra_vars": {}
|
|
}'
|
|
```
|
|
|
|
## 预置Playbook
|
|
|
|
| 文件 | 说明 |
|
|
|------|------|
|
|
| `update-packages.yml` | 更新系统包 |
|
|
| `deploy-docker.yml` | 安装Docker |
|
|
| `deploy-nginx.yml` | 部署Nginx |
|
|
| `check-system.yml` | 系统信息检查 |
|
|
|
|
## 目录结构
|
|
|
|
```
|
|
ansible-deploy/
|
|
├── cmd/
|
|
│ └── main.go # 主程序入口
|
|
├── config/
|
|
│ └── config.yaml # 配置文件
|
|
├── internal/
|
|
│ ├── handlers/ # HTTP处理器
|
|
│ ├── models/ # 数据模型
|
|
│ └── services/ # 业务逻辑
|
|
├── web/
|
|
│ └── dist/
|
|
│ └── index.html # 前端页面
|
|
├── playbooks/ # Playbook目录
|
|
├── scripts/
|
|
│ └── install.sh # 安装脚本
|
|
└── README.md
|
|
```
|
|
|
|
## 注意事项
|
|
|
|
1. **SSH免密** - 建议配置SSH密钥对实现免密登录
|
|
2. **权限** - 部分操作需要sudo权限,确保用户有sudo权限
|
|
3. **防火墙** - 确保SSH端口开放
|
|
4. **Python** - Ansible需要目标主机安装Python
|
|
|
|
## License
|
|
|
|
MIT License
|