Ansible Cheatsheet - Automation

All essential Ansible commands organized by use case, with 42+ entries you can copy and run directly. Find the right command fast when you need it.

SysOps·42 commands·Last updated 2026-07-21
Back to SysOps

Core Commands 6

ansible all -m ping
Test所有主机connectivity
ansible-playbook site.yml
Execute Playbook
ansible-galaxy init myrole
初始化角色Directory结构
ansible-galaxy install geerlingguy.nginx
从 Galaxy Install角色
ansible-vault encrypt secrets.yml
加密敏感variableFile
ansible-doc -l | grep copy
Listmodule并Searchusage

ad-hoc 7

ansible webservers -m shell -a "uptime"
在指定组Execute shell 命令
ansible all -m setup -a "filter=ansible_os_family"
收集主机 facts 信息
ansible all -m copy -a "src=file.txt dest=/tmp/"
CopyFile到远程主机
ansible all -m service -a "name=nginx state=started"
StartService
ansible webservers -m yum -a "name=nginx state=latest"
Install或Upgrade软件package
ansible all -m ping --limit web1
只对Match的主机Execute
ansible all -m shell -a "df -h" -f 10
Setparallel fork 数量加速Execute

Common Modules 7

- name: Install nginx\n apt:\n name: nginx\n state: present
apt/yum package管理module
- name: Copy config\n template:\n src: nginx.conf.j2\n dest: /etc/nginx/nginx.conf
template templaterendermodule
- name: Start service\n service:\n name: nginx\n state: started\n enabled: yes
service Service管理module
- name: Create user\n user:\n name: deploy\n shell: /bin/bash
user User管理module
- name: Run command\n shell: npm run build\n args:\n chdir: /app
shell 命令Executemodule
- name: Create dir\n file:\n path: /opt/app\n state: directory\n mode: "0755"
file File/Directory管理module
- name: Git clone\n git:\n repo: https://github.com/x/y.git\n dest: /srv/app
git 代码检出module

Playbook 6

- hosts: webservers\n become: yes\n tasks: [...]
指定目标主机并启用 sudo
pre_tasks: / post_tasks:
在 roles 前后Execute的task
handlers:\n - name: restart nginx\n service: name=nginx state=restarted
定义 handler,被 notify 触发
vars:\n nginx_port: 80
在 Playbook 中内联variable
- name: notify handler\n template: ...\n notify: restart nginx
task变更后通知 handler Execute
- import_playbook: db.yml
引入其他 Playbook

Vars & Templates 6

vars_files:\n - vars/secrets.yml
从File加载variable
- command: ...\n when: ansible_os_family == "Debian"
conditionExecute,supports Jinja2 表达式
- user: name={{ item }}\n with_items:\n - alice\n - bob
loopiteratelist
- command: ...\n register: result
将taskOutput注册到variable
{{ nginx_port }} / {{ ansible_hostname }}
Jinja2 template语法引用variable
when: result.rc == 0
基于上一taskreturn value做condition判断

Inventory 5

[webservers]\nweb1 ansible_host=10.0.1.1\nweb2 ansible_host=10.0.1.2
INI 格式 Inventory
[web:children]\nweb1\nweb2
定义组嵌套关系
ansible all -i hosts.yml --list-hosts
指定 Inventory 并ListMatch主机
ansible all -i ec2.py --list-hosts
使用动态 Inventory(云平台)
web1 ansible_host=10.0.1.1 ansible_user=deploy ansible_port=2222
为主机指定ConnectionOptions

Roles & Best Practices 5

ansible-galaxy init myrole
生成标准角色Directory结构
roles/myrole/{tasks,handlers,templates,files,defaults,vars}/main.yml
角色标准Directory约定
- hosts: web\n roles:\n - nginx\n - { role: db, when: enable_db }
在 Playbook 中使用角色
- import_role:\n name: nginx\n tags: setup
静态引入角色并打标签
[defaults]\ninventory = hosts\nremote_user = deploy\nhost_key_checking = False
ansible.cfg 全局Configure

💡 Tips

  • Ansible 默认使用 SSH Connection,无需在目标机器Install Agent。
  • template module用 Jinja2 语法renderConfigureFile,比 copy 更灵活。
  • --check 模式(dry-run)可以预览 Playbook Execute效果而不实际修改System。
  • roles Directory结构:tasks/main.yml、handlers/main.yml、templates/、files/、defaults/main.yml。
  • 敏感数据用 ansible-vault 加密,配合 vars_files 引入,不要硬编码进 Playbook。
  • group_vars/ 和 host_vars/ Directory可按组和主机自动加载variable,比内联更易维护。

Official References

Commands are compiled from the official docs below. Click to verify the latest usage.

Maintained by LaoHand

Publicly updated on Jul 21, 2026, continuously proofread against official docs.

Found an error? Report it

Wrong command or description? Open an issue to help us fix it.

Found an error? Report it