修复 Ansible stat 模块属性访问错误

- 修正 xxxigcc_update_pool_url.yaml 中的条件判断
- 将 log_dir_stat.isdir 修正为 log_dir_stat.stat.isdir
- 新增 stat 模块属性访问最佳实践文档
- 更新故障排除指南中的常见错误处理部分
This commit is contained in:
2025-12-16 13:49:00 +08:00
parent fab44ceab6
commit a6399d1bce
4 changed files with 42 additions and 5 deletions

View File

@@ -45,8 +45,32 @@
3. 系统权限
4. Ansible 版本兼容性
### 常见错误处理
#### Stat 模块属性访问问题
- **错误**`'dict object' has no attribute 'isdir'`
- **原因**Ansible stat 模块返回嵌套字典结构
- **解决方案**:始终使用 `log_dir_stat.stat.isdir` 而非 `log_dir_stat.isdir`
示例修复:
```yaml
- name: 检查日志目录
ansible.builtin.stat:
path: "/var/log/xxxigcc"
register: log_dir_stat
# 正确的条件判断
- name: 处理日志目录
when: log_dir_stat.stat.exists and log_dir_stat.stat.isdir
block:
- name: 清理日志
# 清理操作
```
## 5. 安全注意事项
- 仅在可信环境中执行此 Playbook
- 谨慎使用 `old_pool_url` 参数
- 确保 Ansible 版本满足最低要求
- 确保 Ansible 版本满足最低要求
- 注意处理 stat 模块返回的嵌套字典属性

View File

@@ -19,9 +19,9 @@
## 最近更新
- 修复 `xxxigcc_update_pool_url.yaml` 中的版本兼容性问题
- 新增 Ansible 版本兼容性参考文档
- 更新 Pool URL 更新指南
- 修复 `xxxigcc_update_pool_url.yaml` 中的 stat 模块属性访问问题
- 新增 Ansible 模块属性访问的最佳实践说明
- 更新 XXXigCC Pool URL 更新指南中的故障排除部分
## 联系方式

View File

@@ -10,6 +10,18 @@
## 关键变更详情
### 模块属性访问
Ansible stat 模块返回的是嵌套字典结构,访问属性时需要注意:
- **错误示例**`log_dir_stat.isdir`
- **正确示例**`log_dir_stat.stat.isdir`
原因:
- stat 模块返回一个包含 `stat` 子字典的对象
- 必须通过 `stat` 子字典访问文件/目录属性
- 直接访问根对象属性会导致 `'dict object' has no attribute` 错误
### `warn` 参数的移除
- **版本**Ansible 2.14
@@ -32,6 +44,7 @@
- 始终使用最新的 Ansible 版本
- 定期检查并更新 Playbook
- 使用 `ansible_version` 进行版本兼容性检查
- 谨慎访问模块返回的嵌套字典属性
## 参考资源

View File

@@ -106,7 +106,7 @@
- name: 显示日志清理结果
ansible.builtin.debug:
msg: "日志目录 {{ log_dir }} 已清理"
when: log_dir_stat.stat.exists and log_dir_stat.isdir
when: log_dir_stat.stat.exists and log_dir_stat.stat.isdir
- name: 重启 xxxigcc-daemon 服务
ansible.builtin.systemd: