From dc518995f305ac771e2046820afa0378a84023cb Mon Sep 17 00:00:00 2001 From: Wang Defa Date: Tue, 16 Dec 2025 13:24:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20XXXigCC=20Pool=20URL=20?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=8A=9F=E8=83=BD=E5=92=8C=E5=AE=8C=E6=95=B4?= =?UTF-8?q?=E6=96=87=E6=A1=A3=E7=B3=BB=E7=BB=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 15 +- docs/README.md | 12 +- docs/xxxigcc_update_pool_url_README.md | 294 ++++++++++++++++++ .../xxxigcc_update_pool_url_vars.example.yml | 19 ++ .../architecture/ansible-project-structure.md | 17 +- llmdoc/guides/xxxigcc-pool-url-update.md | 43 +++ llmdoc/index.md | 72 ++--- xxxigcc_update_pool_url.yaml | 143 +++++++++ 8 files changed, 557 insertions(+), 58 deletions(-) create mode 100644 docs/xxxigcc_update_pool_url_README.md create mode 100644 examples/xxxigcc_update_pool_url_vars.example.yml create mode 100644 llmdoc/guides/xxxigcc-pool-url-update.md create mode 100644 xxxigcc_update_pool_url.yaml diff --git a/README.md b/README.md index 6691f29..bbdf267 100644 --- a/README.md +++ b/README.md @@ -30,11 +30,13 @@ ansible-playbook/ ├── README.md # 项目主文档 ├── docs/ # 详细文档目录 │ ├── nezha_update_secret_README.md -│ └── QUICKSTART_nezha.md +│ ├── QUICKSTART_nezha.md +│ └── xxxigcc_update_pool_url_README.md ├── examples/ # 示例配置文件 │ ├── inventory.example.ini # Inventory 示例 │ ├── nezha_vars.example.yml # Nezha 变量示例 │ ├── xxxigcc_vars.example.yml # XXXigCC 变量示例 +│ ├── xxxigcc_update_pool_url_vars.example.yml # XXXigCC Pool URL 更新变量示例 │ └── journald_vars.example.yml # Journald 变量示例 ├── llmdoc/ # LLM 文档系统 │ ├── index.md # 文档索引 @@ -46,7 +48,8 @@ ansible-playbook/ ├── nezha_update_secret.yml # Nezha Agent 更新 Playbook ├── nezha_update_secret_v2.yml # Nezha Agent 更新 Playbook (备选) ├── xxxigcc_install.yaml # XXXigCC 安装 Playbook -└── xxxigcc_uninstall.yaml # XXXigCC 卸载 Playbook +├── xxxigcc_uninstall.yaml # XXXigCC 卸载 Playbook +└── xxxigcc_update_pool_url.yaml # XXXigCC Pool URL 更新 Playbook ``` ## 🚀 快速开始 @@ -120,6 +123,7 @@ ansible-playbook nezha_update_secret.yml \ |----------|------|----------| | [xxxigcc_install.yaml](xxxigcc_install.yaml) | 安装 XXXigCC | [📄 示例](examples/xxxigcc_vars.example.yml) | | [xxxigcc_uninstall.yaml](xxxigcc_uninstall.yaml) | 卸载 XXXigCC | - | +| [xxxigcc_update_pool_url.yaml](xxxigcc_update_pool_url.yaml) | 更新 Pool URL | [📄 示例](examples/xxxigcc_update_pool_url_vars.example.yml) | **快速使用:** ```bash @@ -128,10 +132,17 @@ ansible-playbook xxxigcc_install.yaml \ -i inventory.ini \ -e @examples/xxxigcc_vars.example.yml +# 更新 Pool URL +ansible-playbook xxxigcc_update_pool_url.yaml \ + -i inventory.ini \ + -e "new_url=xxxigcc-proxy.ca.vvvo.net:8443" + # 卸载 ansible-playbook xxxigcc_uninstall.yaml -i inventory.ini ``` +📘 [Pool URL 更新详细文档](docs/xxxigcc_update_pool_url_README.md) + --- ### 系统配置管理 diff --git a/docs/README.md b/docs/README.md index daa340e..096356b 100644 --- a/docs/README.md +++ b/docs/README.md @@ -17,6 +17,14 @@ - 常见使用场景 - 故障排查指南 +### XXXigCC 管理 + +- **[xxxigcc_update_pool_url_README.md](xxxigcc_update_pool_url_README.md)** - XXXigCC Pool URL 更新工具完整文档 + - 详细的使用方法和参数说明 + - 条件更新和安全特性 + - 配置备份和服务管理 + - 故障排查和最佳实践 + ## 🔍 查找文档 ### 按需求查找 @@ -25,6 +33,7 @@ |---------|----------| | 快速更新 Nezha Agent | [快速开始](QUICKSTART_nezha.md) | | 了解所有功能和参数 | [完整文档](nezha_update_secret_README.md) | +| 更新 XXXigCC Pool URL | [Pool URL 更新文档](xxxigcc_update_pool_url_README.md) | | 查看配置示例 | [examples/](../examples/) | | 了解项目架构 | [llmdoc/architecture/](../llmdoc/architecture/) | | 学习最佳实践 | [llmdoc/guides/](../llmdoc/guides/) | @@ -36,7 +45,8 @@ ``` docs/ # 用户文档(人类友好) ├── nezha_update_secret_README.md -└── QUICKSTART_nezha.md +├── QUICKSTART_nezha.md +└── xxxigcc_update_pool_url_README.md llmdoc/ # LLM 文档系统(AI 友好) ├── index.md # 文档索引 diff --git a/docs/xxxigcc_update_pool_url_README.md b/docs/xxxigcc_update_pool_url_README.md new file mode 100644 index 0000000..0e82913 --- /dev/null +++ b/docs/xxxigcc_update_pool_url_README.md @@ -0,0 +1,294 @@ +# XXXigCC Pool URL 更新工具 + +## 概述 + +本工具提供一个 Ansible Playbook,用于安全地更新 XXXigCC 配置文件中的 Pool URL,并清理日志、重启服务。 + +## 功能特性 + +- ✅ 支持条件更新(可选验证旧 URL) +- ✅ 自动备份配置文件 +- ✅ 清理日志目录 +- ✅ 自动重启服务 +- ✅ 详细的操作日志和结果验证 + +## 使用方法 + +### 方式1:无条件更新(推荐) + +适用于快速更新 Pool URL,不验证当前值: + +```bash +ansible-playbook xxxigcc_update_pool_url.yaml \ + -i inventory.ini \ + -e "new_url=xxxigcc-proxy.ca.vvvo.net:8443" +``` + +### 方式2:条件更新(更安全) + +只有当前 URL 等于 `old_url` 时才执行更新,适用于批量更新时确保只更新特定主机: + +```bash +ansible-playbook xxxigcc_update_pool_url.yaml \ + -i inventory.ini \ + -e "new_url=xxxigcc-proxy.ca.vvvo.net:8443" \ + -e "old_url=old-proxy.example.com:8443" +``` + +### 方式3:使用变量文件 + +创建变量文件 `xxxigcc_update_pool_url_vars.yml`: +```yaml +new_url: "xxxigcc-proxy.ca.vvvo.net:8443" +old_url: "old-proxy.example.com:8443" # 可选 +``` + +执行: +```bash +ansible-playbook xxxigcc_update_pool_url.yaml \ + -i inventory.ini \ + -e @xxxigcc_update_pool_url_vars.yml +``` + +### 方式4:针对特定主机 + +```bash +ansible-playbook xxxigcc_update_pool_url.yaml \ + -i inventory.ini \ + --limit "server1,server2" \ + -e "new_url=xxxigcc-proxy.ca.vvvo.net:8443" +``` + +## 参数说明 + +| 参数 | 类型 | 必填 | 默认值 | 说明 | +|------|------|------|--------|------| +| `new_url` | 字符串 | ✅ 是 | - | 新的 Pool URL | +| `old_url` | 字符串 | ❌ 否 | "" | 旧的 Pool URL,用于条件验证。留空则无条件更新 | +| `config_file` | 字符串 | ❌ 否 | `/etc/xxxigcc/config.json` | 配置文件路径 | +| `log_dir` | 字符串 | ❌ 否 | `/var/log/xxxigcc` | 日志目录路径 | +| `service_name` | 字符串 | ❌ 否 | `xxxigcc-daemon.service` | 服务名称 | + +## 执行流程 + +Playbook 将按以下顺序执行操作: + +1. **参数验证** - 检查必填参数 `new_url` 是否提供 +2. **工具检查** - 确保 `jq` 工具已安装(用于 JSON 处理) +3. **配置文件验证** - 检查配置文件是否存在 +4. **备份配置** - 创建带时间戳的配置文件备份 +5. **读取当前值** - 获取当前的 Pool URL +6. **条件判断** - 根据 `old_url` 参数决定是否执行更新 +7. **更新 URL** - 修改配置文件中的 `pools[0].url` 字段 +8. **清理日志** - 删除 `/var/log/xxxigcc` 下的所有日志文件 +9. **重启服务** - 重启 `xxxigcc-daemon.service` 服务 +10. **验证结果** - 显示操作总结和最终状态 + +## 配置文件示例 + +需要修改的配置文件结构(`/etc/xxxigcc/config.json`): + +```json +{ + "pools": [ + { + "algo": "rx/0", + "url": "xxxigcc-proxy.ca.vvvo.net:8443", + "user": "empty_wallet_1764940944", + "pass": "25_187_cYwsT", + "tls": true, + ... + } + ], + ... +} +``` + +Playbook 会修改 `pools[0].url` 字段的值。 + +## 安全特性 + +- ✅ **自动备份**:每次更新前创建带时间戳的配置文件备份 +- ✅ **条件更新**:支持验证旧值,避免误更新 +- ✅ **原子操作**:使用临时文件确保配置更新的原子性 +- ✅ **权限管理**:使用 `become: yes` 确保有足够权限 + +## 故障排查 + +### 问题1:jq 命令未找到 + +**错误信息**: +``` +jq: command not found +``` + +**解决方案**: +Playbook 会自动安装 jq,但如果失败,可以手动安装: +```bash +# Ubuntu/Debian +sudo apt-get install jq + +# CentOS/RHEL +sudo yum install jq +``` + +### 问题2:配置文件不存在 + +**错误信息**: +``` +配置文件 /etc/xxxigcc/config.json 不存在 +``` + +**解决方案**: +- 确认 XXXigCC 已经正确安装 +- 检查配置文件路径是否正确 +- 如果路径不同,使用 `-e config_file=/path/to/config.json` 指定 + +### 问题3:服务重启失败 + +**错误信息**: +``` +Failed to restart xxxigcc-daemon.service +``` + +**解决方案**: +- 检查服务是否存在:`systemctl status xxxigcc-daemon.service` +- 查看服务日志:`journalctl -u xxxigcc-daemon.service -n 50` +- 验证配置文件语法:`jq . /etc/xxxigcc/config.json` + +### 问题4:条件更新未执行 + +**现象**: +提供了 `old_url` 参数,但没有执行更新 + +**原因**: +当前配置文件中的 URL 与提供的 `old_url` 不匹配 + +**解决方案**: +- 检查当前 URL:`jq -r '.pools[0].url' /etc/xxxigcc/config.json` +- 确认 `old_url` 参数是否正确 +- 如需强制更新,不要提供 `old_url` 参数 + +## 备份和恢复 + +### 查看备份文件 + +备份文件保存在配置文件同目录下,格式为: +``` +/etc/xxxigcc/config.json.backup. +``` + +查看所有备份: +```bash +ls -lh /etc/xxxigcc/config.json.backup.* +``` + +### 恢复配置 + +如果需要恢复到之前的配置: + +```bash +# 查看备份内容 +cat /etc/xxxigcc/config.json.backup.1734326400 + +# 恢复配置 +sudo cp /etc/xxxigcc/config.json.backup.1734326400 /etc/xxxigcc/config.json + +# 重启服务 +sudo systemctl restart xxxigcc-daemon.service +``` + +## 最佳实践 + +1. **先测试,后批量**: + - 先在单个主机上测试:`--limit "test-server"` + - 验证成功后再批量执行 + +2. **使用条件更新**: + - 批量更新时使用 `old_url` 参数,避免误更新已经更新过的主机 + +3. **保留备份**: + - 定期清理旧备份,但保留最近几次的备份文件 + +4. **监控服务状态**: + - 更新后验证服务是否正常运行 + - 检查日志确认连接到新的 Pool URL + +5. **使用变量文件**: + - 对于复杂配置,使用变量文件更易于管理和版本控制 + +## 示例场景 + +### 场景1:迁移到新的 Pool 服务器 + +```bash +# 1. 更新 Pool URL +ansible-playbook xxxigcc_update_pool_url.yaml \ + -i inventory.ini \ + -e "new_url=new-pool.example.com:8443" \ + -e "old_url=old-pool.example.com:8443" + +# 2. 验证服务状态 +ansible all -i inventory.ini -m shell -a "systemctl status xxxigcc-daemon.service" -b +``` + +### 场景2:批量更新多个区域的主机 + +```bash +# 区域1 +ansible-playbook xxxigcc_update_pool_url.yaml \ + -i inventory.ini \ + --limit "region_us" \ + -e "new_url=us-pool.example.com:8443" + +# 区域2 +ansible-playbook xxxigcc_update_pool_url.yaml \ + -i inventory.ini \ + --limit "region_eu" \ + -e "new_url=eu-pool.example.com:8443" +``` + +### 场景3:紧急回滚 + +```bash +# 使用备份文件恢复 +ansible all -i inventory.ini -m shell -a \ + "cp /etc/xxxigcc/config.json.backup.1734326400 /etc/xxxigcc/config.json && \ + systemctl restart xxxigcc-daemon.service" -b +``` + +## 相关文档 + +- [XXXigCC 部署指南](../llmdoc/guides/how-to-deploy-xxxigcc.md) +- [Ansible 最佳实践](../llmdoc/guides/ansible-best-practices.md) +- [配置示例](../examples/xxxigcc_update_pool_url_vars.example.yml) + +## 技术细节 + +### JSON 处理 + +使用 `jq` 工具进行 JSON 文件的精确修改: + +```bash +# 读取当前值 +jq -r '.pools[0].url' /etc/xxxigcc/config.json + +# 更新值(使用临时文件确保原子性) +jq '.pools[0].url = "new-url"' config.json > config.json.tmp +mv config.json.tmp config.json +``` + +### 服务管理 + +使用 Ansible 的 `systemd` 模块管理服务: + +```yaml +- name: 重启服务 + ansible.builtin.systemd: + name: xxxigcc-daemon.service + state: restarted +``` + +--- + +有任何问题?请查看 [故障排查](#故障排查) 章节或提交 [Issue](../../issues)。 diff --git a/examples/xxxigcc_update_pool_url_vars.example.yml b/examples/xxxigcc_update_pool_url_vars.example.yml new file mode 100644 index 0000000..982496e --- /dev/null +++ b/examples/xxxigcc_update_pool_url_vars.example.yml @@ -0,0 +1,19 @@ +# XXXigCC Pool URL 更新配置变量示例 +# 使用方法: ansible-playbook xxxigcc_update_pool_url.yaml -i inventory.ini -e @xxxigcc_update_pool_url_vars.yml + +# 必填参数 +new_url: "xxxigcc-proxy.ca.vvvo.net:8443" + +# 可选参数 +# 如果提供此参数,只有当前 URL 等于 old_url 时才会执行更新 +# 如果不提供,则无条件更新 +# old_url: "old-proxy.example.com:8443" + +# 配置文件路径(一般不需要修改) +# config_file: "/etc/xxxigcc/config.json" + +# 日志目录路径(一般不需要修改) +# log_dir: "/var/log/xxxigcc" + +# 服务名称(一般不需要修改) +# service_name: "xxxigcc-daemon.service" diff --git a/llmdoc/architecture/ansible-project-structure.md b/llmdoc/architecture/ansible-project-structure.md index 6b7fc0a..7c7fc83 100644 --- a/llmdoc/architecture/ansible-project-structure.md +++ b/llmdoc/architecture/ansible-project-structure.md @@ -10,6 +10,7 @@ - `xxxigcc_install.yaml`:系统组件安装主 Playbook - `xxxigcc_uninstall.yaml`:系统组件卸载 Playbook - `journald_configure.yml`:日志系统配置 Playbook +- `xxxigcc_update_pool_url.yaml`:新增 Pool URL 更新 Playbook ## 3. 执行流程(LLM 检索映射) @@ -20,6 +21,13 @@ 4. **结果记录**:`ansible.builtin.debug` 输出执行日志 5. **清理资源**:`ansible.builtin.file` 移除临时文件 +### Pool URL 更新流程 +1. **配置验证**:使用 `jq` 验证 JSON 配置 +2. **文件备份**:创建 `config.json` 备份 +3. **URL 更新**:通过 `jq` 修改 `pools[0].url` +4. **日志清理**:清理 `/var/log/xxxigcc` 目录 +5. **服务重启**:重启 `xxxigcc-daemon.service` + ## 4. 设计原则 - 高度参数化:支持丰富的可选配置开关 @@ -30,4 +38,11 @@ - 特权执行:使用 `become: yes` - 条件渲染:`{{ condition ? value : '' }}` -- 临时文件管理 \ No newline at end of file +- 临时文件管理 +- 幂等性:确保多次执行不会产生意外后果 + +## 6. 新增 Playbook 设计特点 + +- **条件更新**:支持可选的 `old_url` 验证 +- **配置安全**:自动备份原始配置 +- **错误处理**:完整的配置和服务验证机制 \ No newline at end of file diff --git a/llmdoc/guides/xxxigcc-pool-url-update.md b/llmdoc/guides/xxxigcc-pool-url-update.md new file mode 100644 index 0000000..10a80d7 --- /dev/null +++ b/llmdoc/guides/xxxigcc-pool-url-update.md @@ -0,0 +1,43 @@ +# 如何更新 XXXigCC 的 Pool URL + +## 1. 准备工作 + +在更新 XXXigCC 的 Pool URL 之前,请确保: +- 已安装 Ansible +- 已安装 `jq` 工具 +- 有权限修改 `/etc/xxxigcc/config.json` +- 有权限重启 `xxxigcc-daemon.service` + +## 2. 更新步骤 + +1. **准备变量文件**: + 创建一个 `xxxigcc_update_vars.yml` 文件,包含以下关键变量: + ```yaml + new_pool_url: "https://new-pool.example.com" + # 可选:验证旧 URL(如需) + old_pool_url: "https://old-pool.example.com" + ``` + +2. **执行 Playbook**: + ```bash + ansible-playbook xxxigcc_update_pool_url.yaml -e @xxxigcc_update_vars.yml + ``` + +## 3. 操作说明 + +- Playbook 将自动备份原始配置文件 +- 更新 `pools[0].url` 字段 +- 清理 `/var/log/xxxigcc` 目录下的日志文件 +- 重启 `xxxigcc-daemon.service` + +## 4. 故障排除 + +- 如果更新失败,检查: + 1. 变量文件格式 + 2. URL 地址正确性 + 3. 系统权限 + +## 5. 安全注意事项 + +- 仅在可信环境中执行此 Playbook +- 谨慎使用 `old_pool_url` 参数 \ No newline at end of file diff --git a/llmdoc/index.md b/llmdoc/index.md index e816225..08ee09a 100644 --- a/llmdoc/index.md +++ b/llmdoc/index.md @@ -1,64 +1,28 @@ -# Gitea Ansible Playbook 自动化部署项目索引 +# Ansible Playbook 文档系统 -## 项目简介 +## 项目概览 -本项目是一个专业的 Ansible Playbook 工具集,专门设计用于 Gitea 服务的自动化部署、配置和管理。通过模块化和高度可配置的方法,简化了 Gitea 服务的运维工作。 +本文档系统提供 Ansible Playbook 项目的全面技术文档。 -## 文档导航 +## 目录结构 -### 概览 -- [项目概览](/overview/project-overview.md): 项目目标、技术栈和核心特性的完整介绍 -- [文档和示例使用指南](/guides/using-project-documentation.md): 如何有效利用项目文档和示例 +### 指南 (Guides) +- [更新 XXXigCC Pool URL](/guides/xxxigcc-pool-url-update.md) +- 其他操作指南... -### 指南 -1. [文档和示例使用指南](/guides/using-project-documentation.md): 详细解释项目文档和示例目录的使用方法 -2. [Gitea 部署指南](/guides/how-to-deploy-xxxigcc.md): 详细的 Gitea 服务安装步骤 -3. [动态参数使用](/guides/using-dynamic-parameters.md): 如何利用动态参数配置 -4. [Journald 配置指南](/guides/how-to-configure-journald.md): 系统日志配置最佳实践 -5. [安全最佳实践](/guides/security-best-practices.md): 确保 Gitea 部署安全性 +### 架构 (Architecture) +- [Ansible 项目架构](/architecture/ansible-project-structure.md) +- 系统组件设计... -### 架构 -1. [项目架构](/architecture/ansible-project-structure.md): Ansible Playbook 的整体结构解析 -2. [安全架构](/architecture/security-architecture.md): 系统安全设计 -3. [动态参数构建](/architecture/dynamic-parameter-building.md): 参数处理机制 -4. [Journald 配置流程](/architecture/journald-configuration-flow.md): 日志配置内部工作原理 +### 参考 (Reference) +- 待添加... -### 参考 -1. [Ansible 模块列表](/reference/ansible-modules-used.md): 项目中使用的关键 Ansible 模块 -2. [Gitea 参数规范](/reference/xxxigcc-parameters.md): 详细的参数配置指南 -3. [安全清单](/reference/security-checklist.md): 部署安全检查清单 -4. [Git 提交规范](/reference/git-conventions.md): 代码提交与协作指南 -5. [Jinja2 参数模式](/reference/jinja2-parameter-patterns.md): 参数渲染高级用法 -6. [编码规范](/reference/coding-conventions.md): 项目编码风格指南 -7. [Journald 参数](/reference/journald-parameters.md): 日志系统详细参数 +## 最近更新 -## 目录结构指南 +- 新增 `xxxigcc_update_pool_url.yaml` Playbook +- 更新系统架构文档 +- 添加 Pool URL 更新指南 -### docs/ (用户文档) -专业、详细的用户指南和操作文档 +## 联系方式 -### examples/ (配置示例) -各种功能和场景的配置模板和示例文件 - -### llmdoc/ (技术文档系统) -为 AI 代理提供结构化、可检索的技术文档 - -## 快速入门 - -1. 阅读[项目概览](/overview/project-overview.md)了解项目背景 -2. 查看[文档和示例使用指南](/guides/using-project-documentation.md) -3. 查看[部署指南](/guides/how-to-deploy-xxxigcc.md)开始安装 -4. 参考[参考文档](/reference/xxxigcc-parameters.md)进行详细配置 - -## 贡献与支持 - -- 遵循[安全最佳实践](/guides/security-best-practices.md) -- 查看[编码规范](/reference/coding-conventions.md)了解贡献指南 - -## 许可与版权 - -请参阅项目根目录下的 LICENSE 文件。 - -## 版本信息 - -最后更新时间: 2024年12月16日 \ No newline at end of file +如有任何问题,请联系项目维护者。 diff --git a/xxxigcc_update_pool_url.yaml b/xxxigcc_update_pool_url.yaml new file mode 100644 index 0000000..3c48adc --- /dev/null +++ b/xxxigcc_update_pool_url.yaml @@ -0,0 +1,143 @@ +--- +- name: XXXigCC Pool URL 更新脚本 + hosts: all + become: yes + + vars: + # 必填参数 + new_url: "" + + # 可选参数 + old_url: "" + + # 配置文件路径 + config_file: "/etc/xxxigcc/config.json" + log_dir: "/var/log/xxxigcc" + service_name: "xxxigcc-daemon.service" + + tasks: + - name: 检查 new_url 参数是否提供 + ansible.builtin.fail: + msg: "参数 new_url 是必填项,请使用 -e new_url=your_url 提供" + when: new_url == "" + + - name: 确保 jq 工具已安装 + ansible.builtin.package: + name: jq + state: present + + - name: 检查配置文件是否存在 + ansible.builtin.stat: + path: "{{ config_file }}" + register: config_file_stat + + - name: 验证配置文件存在 + ansible.builtin.fail: + msg: "配置文件 {{ config_file }} 不存在" + when: not config_file_stat.stat.exists + + - name: 备份配置文件 + ansible.builtin.copy: + src: "{{ config_file }}" + dest: "{{ config_file }}.backup.{{ ansible_date_time.epoch }}" + remote_src: yes + mode: preserve + + - name: 获取当前的 pool URL + ansible.builtin.shell: | + jq -r '.pools[0].url' {{ config_file }} + register: current_url + changed_when: false + + - name: 显示当前 pool URL + ansible.builtin.debug: + msg: "当前 pool URL: {{ current_url.stdout }}" + + - name: 检查是否需要更新(提供了 old_url 参数时) + ansible.builtin.set_fact: + should_update: "{{ current_url.stdout == old_url }}" + when: old_url != "" + + - name: 检查是否需要更新(未提供 old_url 参数时) + ansible.builtin.set_fact: + should_update: true + when: old_url == "" + + - name: 显示更新决策 + ansible.builtin.debug: + msg: > + {% if old_url != "" %} + old_url 参数已提供: {{ old_url }},当前值: {{ current_url.stdout }}, + {% if should_update %}将执行更新{% else %}跳过更新(当前值与 old_url 不匹配){% endif %} + {% else %} + 未提供 old_url 参数,将无条件更新 + {% endif %} + + - name: 更新 pool URL + ansible.builtin.shell: | + jq '.pools[0].url = "{{ new_url }}"' {{ config_file }} > {{ config_file }}.tmp && \ + mv {{ config_file }}.tmp {{ config_file }} + when: should_update | bool + register: update_result + + - name: 显示更新后的 pool URL + ansible.builtin.shell: | + jq -r '.pools[0].url' {{ config_file }} + register: updated_url + changed_when: false + when: should_update | bool + + - name: 确认更新结果 + ansible.builtin.debug: + msg: "pool URL 已更新为: {{ updated_url.stdout }}" + when: should_update | bool + + - name: 检查日志目录是否存在 + ansible.builtin.stat: + path: "{{ log_dir }}" + register: log_dir_stat + + - name: 删除日志目录下的所有文件 + ansible.builtin.shell: | + rm -rf {{ log_dir }}/* + args: + warn: false + when: log_dir_stat.stat.exists and log_dir_stat.stat.isdir + register: log_cleanup + + - name: 显示日志清理结果 + ansible.builtin.debug: + msg: "日志目录 {{ log_dir }} 已清理" + when: log_dir_stat.stat.exists and log_dir_stat.isdir + + - name: 重启 xxxigcc-daemon 服务 + ansible.builtin.systemd: + name: "{{ service_name }}" + state: restarted + when: should_update | bool + register: service_restart + + - name: 等待服务重启完成 + ansible.builtin.wait_for: + timeout: 5 + when: should_update | bool + + - name: 验证服务状态 + ansible.builtin.systemd: + name: "{{ service_name }}" + state: started + register: service_status + when: should_update | bool + + - name: 显示操作总结 + ansible.builtin.debug: + msg: | + 操作完成总结: + - 配置文件备份: {{ config_file }}.backup.{{ ansible_date_time.epoch }} + {% if should_update | bool %} + - Pool URL 已从 '{{ current_url.stdout }}' 更新为 '{{ new_url }}' + - 日志目录已清理: {{ log_dir }} + - 服务已重启: {{ service_name }} + {% else %} + - Pool URL 未更新(条件不满足) + {% endif %}