Iván

Ansible

运维安全v1.0.0
作者:Iván更新于 2026-05-11ansible@clawhub_ivangdavila/ansible
服务器运维DevOps

据 AlphaMoat 统计,截至 2026-09,Ansible 累计下载 3,371 次、获 4 个星标,在全部 5,078 个 AI 技能中排名第 3,340 位,在「运维安全」分类(264 个)中排名第 214 位。

累计安装
115
累计下载
3,371
星标
4
全站排名
#3,340
全站前 66% · 「运维安全」前 82%
平台累计安装累计下载星标评论
ClawHub ↗1033,3713-
SkillHub ↗1153,3074—

各平台的计数口径不同,统计时间不一致。

这个技能是做什么的

这份参考资料汇总 YAML 引号与缩进、变量覆盖顺序、幂等任务、handler 触发条件、条件判断、循环和 facts 等常见陷阱。它也涉及 become 或 sudo 等提权选项;应用涉及权限提升的 playbook 改动前应先审查。

资料发布于 2026-10-29

可以用来做什么

  • 排查 YAML 语法或缩进问题
  • 理解变量覆盖与 handler 执行条件
  • 检查命令任务的幂等性处理

什么时候用

  • 编写或审查 Ansible playbook 时
  • 排查变量、handler 或条件判断造成的意外行为时

示例提示词

以下是根据技能文档生成的示例,不代表实际运行结果。

检查这段 Ansible YAML 的引号和缩进问题,并说明修正理由。
解释这段 playbook 的变量优先级和 handler 触发条件;不要执行任何命令。

月度数据趋势

累计下载3,371
1,7282,6113,4932026-03 · 累计下载 1,8502026-05 · 累计下载 2,4972026-06 · 累计下载 2,6522026-07 · 累计下载 2,9612026-08 · 累计下载 3,1952026-09 · 累计下载 3,37126-0326-09
星标4
3442026-03 · 星标 32026-05 · 星标 32026-06 · 星标 32026-07 · 星标 42026-08 · 星标 42026-09 · 星标 426-0326-09

2026-09 起统计方法调整:同名技能按作者分别统计,并覆盖全部技能。图中虚线为调整前的月份,与之后的数据不直接比较,也不计算跨越调整的增长。

月份累计下载环比星标
2026-093,371—4
2026-083,195+7.9%4
2026-072,961+11.7%4
2026-062,652+6.2%3
2026-052,497—3
2026-031,850—3

SKILL.md 原文

以下是作者发布的原始文件,按纯文本显示,本站未做修改或审核;其中的链接不可点击,命令请核实后再运行。抓取于 2026-10-23。

3.1 KB · ClawHub v1.0.0
---
name: Ansible
description: Avoid common Ansible mistakes — YAML syntax traps, variable precedence, idempotence failures, and handler gotchas.
metadata: {"clawdbot":{"emoji":"🔧","requires":{"bins":["ansible"]},"os":["linux","darwin"]}}
---

## YAML Syntax Traps
- Jinja2 in value needs quotes — `"{{ variable }}"` not `{{ variable }}`
- `:` in string needs quotes — `msg: "Note: this works"` not `msg: Note: this`
- Boolean strings: `yes`, `no`, `true`, `false` parsed as bool — quote if literal string
展开全部(共 60 行)
- Indentation must be consistent — 2 spaces standard, tabs forbidden

## Variable Precedence
- Extra vars (`-e`) override everything — highest precedence
- Host vars beat group vars — more specific wins
- `vars:` in playbook beats inventory vars — order: inventory < playbook < extra vars
- Undefined variable fails — use `{{ var | default('fallback') }}`

## Idempotence
- `command`/`shell` modules aren't idempotent — always "changed", use `creates:` or specific module
- Use `apt`, `yum`, `copy` etc. — designed for idempotence
- `changed_when: false` for commands that don't change state — like queries
- `creates:`/`removes:` for command idempotence — skips if file exists/doesn't

## Handlers
- Handlers only run if task reports changed — not on "ok"
- Handlers run once at end of play — not immediately after notify
- Multiple notifies to same handler = one run — deduplicated
- `--force-handlers` to run even on failure — or `meta: flush_handlers`

## Become (Privilege Escalation)
- `become: yes` to run as root — `become_user:` for specific user
- `become_method: sudo` is default — use `su` or `doas` if needed
- Password needed for sudo — `--ask-become-pass` or in ansible.cfg
- Some modules need become at task level — even if playbook has `become: yes`

## Conditionals
- `when:` without Jinja2 braces — `when: ansible_os_family == "Debian"` not `when: "{{ ... }}"`
- Multiple conditions use `and`/`or` — or list for implicit `and`
- `is defined`, `is not defined` for optional vars — `when: my_var is defined`
- Boolean variables: `when: my_bool` — don't compare `== true`

## Loops
- `loop:` is modern, `with_items:` is legacy — both work, loop preferred
- `loop_control.loop_var` for nested loops — avoids variable collision
- `item` is the loop variable — use `loop_control.label` for cleaner output
- `until:` for retry loops — `until: result.rc == 0 retries: 5 delay: 10`

## Facts
- `gather_facts: no` speeds up play — but can't use `ansible_*` variables
- Facts cached with `fact_caching` — persists across runs
- Custom facts in `/etc/ansible/facts.d/*.fact` — JSON or INI, available as `ansible_local`

## Common Mistakes
- `register:` captures output even on failure — check `result.rc` or `result.failed`
- `ignore_errors: yes` continues but doesn't change result — task still "failed" in register
- `delegate_to: localhost` for local commands — but `local_action` is cleaner
- Vault password for encrypted files — `--ask-vault-pass` or vault password file
- `--check` (dry run) not supported by all modules — `command`, `shell` always skip

「运维安全」分类下的热门技能

Iván 的其他技能

数据月份:2026-09 · 下载/星标/安装数聚合自公开技能注册库(ClawHub、SkillHub),按月更新。口径说明见数据方法。