with one click
ansible
Ansible 安装配置、Inventory 管理、常用模块、Playbook 编写、批量操作最佳实践
Install with Codex or Claude Copy this prompt, paste it into Codex, Claude, or another assistant, and let it review the skill page and install it for you.
Menu
Ansible 安装配置、Inventory 管理、常用模块、Playbook 编写、批量操作最佳实践
Install with Codex or Claude Copy this prompt, paste it into Codex, Claude, or another assistant, and let it review the skill page and install it for you.
Based on SOC occupation classification
生成或审计模块的 SPEC.md。使用场景:用户要求"给 XX 模块写 spec"、"审计所有 spec"、"检查 spec 是否过期"。
Automates SailFish release workflow: fix build/type errors, update CHANGELOG (EN + CN), run npm version with pre/post hooks. Use when the user asks to release, 发版, 发布, bump version, or update changelog.
后端代码修改后,通过 CLI 测试验证功能正确性。使用场景:修改了 electron/services/ 下的代码需要测试、准备提交代码前跑回归、用户要求"跑测试"/"验证一下"。
完成新功能开发或较大修改后,使用本机 Claude CLI 进行代码审查。
使用 ts-morph 静态分析工具查询代码结构(类层次、方法签名、引用、依赖等),替代手动读源码。使用场景:需要了解类的方法/属性、继承链、符号引用、文件结构、依赖关系时。
When committing or staging changes, only include files related to the current task or conversation; do not stage or commit unrelated modifications. Use when the user asks to commit, stage, 提交, or when preparing to run git add/commit.
| name | Ansible 批量运维 |
| description | Ansible 安装配置、Inventory 管理、常用模块、Playbook 编写、批量操作最佳实践 |
| version | 1.0.0 |
# pip 安装(推荐)
pip install ansible
# Ubuntu
sudo apt install -y ansible
# 验证
ansible --version
INI 格式(/etc/ansible/hosts 或项目内 inventory.ini):
[webservers]
web1 ansible_host=192.168.1.10
web2 ansible_host=192.168.1.11
[dbservers]
db1 ansible_host=192.168.1.20
[all:vars]
ansible_user=admin
ansible_ssh_private_key_file=~/.ssh/id_ed25519
ansible_python_interpreter=/usr/bin/python3
连通性测试:
ansible all -i inventory.ini -m ping
ansible webservers -i inventory.ini -m ping
# 执行命令
ansible webservers -i inventory.ini -m shell -a "uptime"
ansible all -i inventory.ini -m shell -a "df -h"
# 复制文件
ansible webservers -i inventory.ini -m copy -a "src=./app.conf dest=/etc/app/app.conf"
# 安装软件
ansible webservers -i inventory.ini -m apt -a "name=nginx state=present" -b
# 管理服务
ansible webservers -i inventory.ini -m service -a "name=nginx state=restarted" -b
# 收集系统信息
ansible all -i inventory.ini -m setup -a "filter=ansible_distribution*"
# deploy.yml
---
- name: 部署 Web 应用
hosts: webservers
become: yes
vars:
app_version: "1.2.0"
tasks:
- name: 安装依赖
apt:
name: [nginx, python3-pip]
state: present
update_cache: yes
- name: 拷贝配置文件
template:
src: templates/nginx.conf.j2
dest: /etc/nginx/sites-available/app.conf
notify: reload nginx
- name: 确保服务启动
service:
name: nginx
state: started
enabled: yes
handlers:
- name: reload nginx
service:
name: nginx
state: reloaded
# 执行 playbook
ansible-playbook -i inventory.ini deploy.yml
# 预检(不执行)
ansible-playbook -i inventory.ini deploy.yml --check --diff
# 指定标签
ansible-playbook -i inventory.ini deploy.yml --tags "config"
# 限制目标主机
ansible-playbook -i inventory.ini deploy.yml --limit web1
| 模块 | 用途 | 示例 |
|---|---|---|
shell / command | 执行命令 | -m shell -a "ls -la" |
copy | 复制文件 | -m copy -a "src=x dest=y" |
template | 渲染 Jinja2 模板 | template: src=x.j2 dest=y |
apt / yum | 包管理 | -m apt -a "name=x state=present" |
service | 服务管理 | -m service -a "name=x state=started" |
user | 用户管理 | -m user -a "name=x state=present" |
file | 文件/目录属性 | -m file -a "path=x state=directory" |
cron | 定时任务 | -m cron -a "name=x job=y minute=0" |
lineinfile | 修改文件内容 | 确保某行存在或替换 |
--check --diff 预检,确认改动范围后再执行ansible-vault 加密--forks 控制并发数(默认 5)