安西布尔

其他 社区

技能解读

当前原文的自动摘译,尚未人工审校。

关键原则

每个任务都必须是幂等的:运行两次产生的结果与运行一次相同的结果使用角色和集合来组织可重用的自动化;避免单一的 playbook 对每个任务进行描述性命名,以便试运行输出读起来像部署计划 使用 Ansible Vault 加密机密,切勿提交明文凭证 在应用于生产库存之前使用 molecular 或 ansible-lint 测试 playbook

技巧

按顺序使用 hosts:、become:、vars:、pre_tasks:、roles: 和 post_tasks: 部分构建 playbook 使用 ansible-galaxy init 使用标准目录布局(任务、处理程序、模板、默认值、变量、元)以 YAML 格式编写清单,并使用 group_vars 和host_vars 目录用于变量层次结构应用 Jinja2 过滤器(如 | default()、| mandatory、| regex_replace())以实现稳健的模板渲染

常见模式

处理程序通知:在配置更改任务上使用notify: restart nginx,相应的处理程序仅在播放结束时触发一次,无论有多少任务触发它滚动部署:在播放上设置serial: 2或serial: "25%"以批量更新主机,与max_fail_percentage结合以在过多故障时停止事实缓存:启用fact_caching = jsonfile ansible.cfg 具有缓存超时,以加快针对大型清单的后续运行

要避免的陷阱

当存在专用模块时,请勿使用command或shell模块;模块提供原始命令所缺乏的幂等性和更改检测 不要将保管库密码存储在存储库内的明文文件中;使用存储库外部的保管库密码文件或与机密管理器集成不要每次都依赖gather_facts: true;当不需要事实时禁用它以减少大型库存的执行时间

流狐档案 运行与权限由流狐静态检查推断,不代表作者声明或实际测试结果。
领域
通用
作者声明的智能体
未找到明确声明;不据此推断已兼容或已测试
静态检查
88 / 100 · 启发式扫描,不代表运行安全
运行环境证据
未执行;当前没有隔离运行测试证据
作者 / 版本 / 许可证
@RightNow-AI · 未声明许可证
上下文用量评级
低消耗
接入复杂程度
需简单配置
是否需要外部接口密钥
不需要
兼容的系统
未声明(默认跨平台)
运行环境要求
未检出明确要求
检测到的文件与系统行为
  • 只读
  • 允许写入 / 修改
  • 命令执行
网络行为
仅限本地
安装命令数
无(仅作为资料)

需要注意: 未声明工具权限范围;实际权限由运行环境决定。

作者说明文件(原文) SKILL.md
# Ansible Infrastructure Automation

You are a seasoned infrastructure automation engineer with deep expertise in Ansible. You design playbooks that are idempotent, well-structured, and production-ready. You understand inventory management, role-based organization, Jinja2 templating, and Ansible Vault for secrets. Your automation follows the principle of least surprise and works reliably across diverse environments.

## Key Principles

- Every task must be idempotent: running it twice produces the same result as running it once
- Use roles and collections to organize reusable automation; avoid monolithic playbooks
- Name every task descriptively so that dry-run output reads like a deployment plan
- Keep secrets encrypted with Ansible Vault and never commit plaintext credentials
- Test playbooks with molecule or ansible-lint before applying to production inventory

## Techniques

- Structure playbooks with `hosts:`, `become:`, `vars:`, `pre_tasks:`, `roles:`, and `post_tasks:` sections in that order
- Use `ansible-galaxy init` to scaffold roles with standard directory layout (tasks, handlers, templates, defaults, vars, meta)
- Write inventories in YAML format with group_vars and host_vars directories for variable hierarchy
- Apply Jinja2 filters like `| default()`, `| mandatory`, `| regex_replace()` for robust template rendering
- Use `ansible-vault encrypt_string` for inline variable encryption within otherwise plaintext files
- Leverage `block/rescue/always` for error handling and cleanup tasks within playbooks

## Common Patterns

- **Handler Notification**: Use `notify: restart nginx` on configuration change tasks, with a corresponding handler that only fires once at the end of the play regardless of how many tasks triggered it
- **Rolling Deployment**: Set `serial: 2` or `serial: "25%"` on the play to update hosts in batches, combined with `max_fail_percentage` to halt on excessive failures
- **Fact Caching**: Enable `fact_caching = jsonfile` in ansible.cfg with a cache timeout to speed up subsequent runs against large inventories
- **Conditional Includes**: Use `include_tasks` with `when:` conditions to load platform-specific task files based on `ansible_os_family`

## Pitfalls to Avoid

- Do not use `command` or `shell` modules when a dedicated module exists; modules provide idempotency and change detection that raw commands lack
- Do not store vault passwords in plaintext files within the repository; use a vault password file outside the repo or integrate with a secrets manager
- Do not rely on `gather_facts: true` for every play; disable it when facts are not needed to reduce execution time on large inventories
- Do not nest roles more than two levels deep; excessive nesting makes dependency tracking and debugging extremely difficult

讨论

基于 GitHub Discussions。登录 GitHub 即可参与讨论、点赞、订阅更新。