Ansible 运维速查表
Ansible 是开源的 IT 自动化引擎,支持配置管理、应用部署与任务编排。本速查表覆盖从临时命令到高级剧本编写、模块使用、清单管理及变量调试,助力高效运维。
临时命令 (Ad-Hoc)
临时命令用于快速执行单个任务,格式为 ansible <主机模式> -m <模块> -a "<参数>"。
### 连通性测试
``yaml
# 测试所有主机连通性(ping 模块)
ansible all -m ping
`
### 执行命令
`yaml
# 在主机组执行 shell 命令
ansible webservers -m shell -a "uptime"
# 执行 command 模块(不支持管道/重定向)
ansible webservers -m command -a "whoami"
`
### 文件操作
`yaml
# 复制本地文件到远程主机
ansible all -m copy -a "src=/etc/hosts dest=/tmp/hosts owner=root group=root mode=0644"
# 管理文件属性(创建/删除/权限) ansible all -m file -a "path=/app/data state=directory mode=0755"
# 删除文件
ansible all -m file -a "path=/tmp/old.log state=absent"
`
### 软件包管理
`yaml
# Debian/Ubuntu 安装包
ansible all -m apt -a "name=nginx state=present" -b
# RHEL/CentOS 安装包
ansible all -m yum -a "name=httpd state=latest" -b
`
### 服务管理
`yaml
# 启动并启用服务
ansible all -m service -a "name=nginx state=started enabled=yes" -b
# 使用 systemd 模块
ansible all -m systemd -a "name=nginx state=restarted daemon_reload=yes" -b
`
清单管理 (Inventory)
清单定义目标主机和分组,支持 INI 和 YAML 格式。
### 基本清单 (INI 格式)
`yaml
# /etc/ansible/hosts
# [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=deploy
# ansible_port=22
# 查看清单 ansible-inventory --list
# 使用指定清单文件 ansible all -i inventory.ini -m ping
# 限制目标为单个主机
ansible-playbook site.yml --limit web1
`
### 动态清单
`yaml
# 使用 AWS EC2 动态清单
ansible-inventory -i aws_ec2.yaml --graph
# 列出所有主机
ansible all -i aws_ec2.yaml --list-hosts
`
模块使用 (Modules)
Ansible 内置数千个模块,以下为常用模块示例。
### 模板与配置
`yaml
# 使用 Jinja2 模板渲染配置文件
- name: 配置 nginx 虚拟主机
ansible.builtin.template:
src: nginx.conf.j2
dest: /etc/nginx/sites-available/default
owner: root
group: root
mode: "0644"
notify: restart nginx
`
### 网络与下载
`yaml
# 下载文件
ansible all -m get_url -a "url=https://example.com/file.tar.gz dest=/tmp/file.tar.gz checksum=sha256:abc123"
# 解压归档
ansible all -m unarchive -a "src=/tmp/file.tar.gz dest=/opt/app remote_src=yes creates=/opt/app/bin"
`
### Git 操作
`yaml
# 克隆 Git 仓库
ansible all -m git -a "repo=https://github.com/example/app.git dest=/opt/app version=main force=yes"
`
### Docker 容器
`yaml
# 管理 Docker 容器
ansible all -m docker_container -a "name=myapp image=nginx:alpine state=started ports=80:80 restart_policy=always"
`
剧本编写 (Playbook)
剧本是 Ansible 的核心,用 YAML 描述自动化流程。
### 基础剧本结构
`yaml
---
- name: 部署 Web 应用
hosts: webservers
become: yes
vars:
app_port: 8080
tasks:
- name: 确保 Nginx 已安装
apt:
name: nginx
state: present
- name: 部署配置文件 template: src: app.conf.j2 dest: /etc/nginx/conf.d/app.conf notify: restart nginx
- name: 确保服务运行 service: name: nginx state: started enabled: yes
handlers:
- name: restart nginx
service:
name: nginx
state: restarted
`
### 条件与循环
`yaml
# 条件执行 (when)
- name: 仅在 Debian 系统执行
apt:
name: python3
state: present
when: ansible_os_family == "Debian"
# 循环 (loop) - name: 创建多个用户 user: name: "{{ item }}" state: present groups: sudo loop: - alice - bob - charlie
# 字典循环
- name: 配置多个服务端口
firewalld:
service: "{{ item.key }}"
port: "{{ item.value }}"
permanent: yes
state: enabled
loop: "{{ services | dict2items }}"
vars:
services:
http: 80
https: 443
`
### 包含与导入
`yaml
# 包含任务文件
- name: 包含通用任务
include_tasks: tasks/common_setup.yml
# 使用角色
- name: 应用 nginx 角色
hosts: webservers
roles:
- role: nginx
nginx_port: 8080
- role: geerlingguy.security
`
变量与模板 (Variables)
### 变量定义与使用
`yaml
# 剧本中定义变量
vars:
domain: example.com
ssl_cert_path: /etc/ssl/certs/{{ domain }}.pem
# 变量提示
- name: 交互式变量
hosts: all
vars_prompt:
- name: admin_password
prompt: "请输入管理员密码"
private: yes
confirm: yes
- name: deploy_version
prompt: "部署版本"
default: "1.0.0"
`
### 注册变量与调试
`yaml
# 注册任务输出
- name: 检查磁盘空间
shell: df -h /
register: disk_output
# 调试输出 - name: 显示磁盘信息 debug: msg: "磁盘使用情况:{{ disk_output.stdout }}"
# 调试变量 - name: 打印所有变量 debug: var: hostvars[inventory_hostname]
# 条件基于注册变量
- name: 磁盘空间不足时告警
fail:
msg: "磁盘使用率超过 90%"
when: "'90%' in disk_output.stdout"
`
高级特性 (Advanced)
### 标签管理
`yaml
# 为任务打标签
tasks:
- name: 安装基础包
apt:
name: "{{ item }}"
loop:
- curl
- vim
- git
tags:
- packages
- base
- name: 配置防火墙 ufw: rule: allow port: "22,80,443" tags: - firewall - security
# 运行指定标签 # ansible-playbook site.yml --tags security
# 跳过指定标签
# ansible-playbook site.yml --skip-tags firewall
`
### 加密与安全 (Vault)
`yaml
# 加密敏感文件
# ansible-vault encrypt secrets.yml
# 使用加密变量 # vars_files: # - secrets.yml
# 运行剧本时解密
# ansible-playbook site.yml --ask-vault-pass
`
### 剧本常用选项
`yaml
# 检查模式(不实际执行)
# ansible-playbook site.yml --check
# 显示差异 # ansible-playbook site.yml --diff
# 详细输出 # ansible-playbook site.yml -vvv
# 指定 SSH 用户 # ansible-playbook site.yml -u deploy
# 指定密钥文件 # ansible-playbook site.yml --private-key ~/.ssh/deploy_key
# 逐台执行(串行) # ansible-playbook site.yml --forks 1
# 仅语法检查 # ansible-playbook site.yml --syntax-check ``
Ad-Hoc(10)
| 命令 | 难度 | ||
|---|---|---|---|
ansible all -m ping测试所有主机的连通性 | 基础 | ansible all -m ping | |
ansible webservers -m command -a "whoami"在主机组执行命令(不支持管道) | 基础 | ansible webservers -m command -a "whoami" | |
ansible all -m shell -a "uptime"在主机组执行 shell 命令 | 基础 | ansible all -m shell -a "uptime" | |
ansible all -m copy -a "src=/etc/hosts dest=/tmp/hosts mode=0644"复制本地文件到远程主机 | 基础 | ansible all -m copy -a "src=/etc/hosts dest=/tmp/hosts owner=root group=root mode=0644" | |
ansible all -m file -a "path=/app/data state=directory mode=0755"创建目录并设置权限 | 基础 | ansible all -m file -a "path=/app/data state=directory mode=0755" | |
ansible all -m file -a "path=/tmp/old.log state=absent"删除远程文件 | 基础 | ansible all -m file -a "path=/tmp/old.log state=absent" | |
ansible all -m apt -a "name=nginx state=present" -b使用 apt 安装软件包 | 基础 | ansible all -m apt -a "name=nginx state=present" -b | |
ansible all -m yum -a "name=httpd state=latest" -b使用 yum 安装最新软件包 | 基础 | ansible all -m yum -a "name=httpd state=latest" -b | |
ansible all -m service -a "name=nginx state=started enabled=yes" -b启动并启用服务 | 基础 | ansible all -m service -a "name=nginx state=started enabled=yes" -b | |
ansible all -m systemd -a "name=nginx state=restarted daemon_reload=yes"使用 systemd 模块重启服务并重新加载守护进程 | 中级 | ansible all -m systemd -a "name=nginx state=restarted daemon_reload=yes" |
清单(3)
| 命令 | 难度 | ||
|---|---|---|---|
ansible-inventory --list查看清单内容(JSON 格式) | 基础 | ansible-inventory --list | |
ansible all -i inventory.ini -m ping使用指定清单文件执行命令 | 基础 | ansible all -i inventory.ini -m ping | |
ansible-inventory -i aws_ec2.yaml --graph查看动态清单的依赖图 | 中级 | ansible-inventory -i aws_ec2.yaml --graph |
模块(5)
| 命令 | 难度 | ||
|---|---|---|---|
ansible all -m get_url -a "url=https://example.com/file.tar.gz dest=/tmp/file.tar.gz"从 URL 下载文件到远程主机 | 中级 | ansible all -m get_url -a "url=https://example.com/file.tar.gz dest=/tmp/file.tar.gz" | |
ansible all -m unarchive -a "src=/tmp/file.tar.gz dest=/opt/app remote_src=yes"在远程主机解压归档文件 | 中级 | ansible all -m unarchive -a "src=/tmp/file.tar.gz dest=/opt/app remote_src=yes" | |
ansible all -m git -a "repo=https://github.com/example/app.git dest=/opt/app version=main"从 Git 仓库克隆代码 | 中级 | ansible all -m git -a "repo=https://github.com/example/app.git dest=/opt/app version=main force=yes" | |
ansible all -m docker_container -a "name=myapp image=nginx:alpine state=started ports=80:80"管理 Docker 容器 | 高级 | ansible all -m docker_container -a "name=myapp image=nginx:alpine state=started ports=80:80 restart_policy=always" | |
ansible.builtin.template使用 Jinja2 模板渲染配置文件 | 中级 | ansible.builtin.template:
src: nginx.conf.j2
dest: /etc/nginx/nginx.conf
mode: '0644' |
剧本(11)
| 命令 | 难度 | ||
|---|---|---|---|
ansible-playbook site.yml运行 Ansible 剧本 | 基础 | ansible-playbook site.yml | |
ansible-playbook site.yml --check --diff以检查模式运行剧本并显示差异 | 中级 | ansible-playbook site.yml --check --diff | |
ansible-playbook site.yml --syntax-check检查剧本语法但不执行 | 基础 | ansible-playbook site.yml --syntax-check | |
when: ansible_os_family == "Debian"条件判断,仅在指定条件下执行任务 | 中级 | when: ansible_os_family == "Debian" | |
loop循环执行任务 | 中级 | loop:
- alice
- bob | |
include_tasks包含外部任务文件 | 中级 | include_tasks: tasks/common_setup.yml | |
roles应用 Ansible 角色 | 高级 | roles:
- role: nginx
nginx_port: 8080 | |
tags为任务打标签以便选择性执行 | 中级 | tags:
- packages
- base | |
ansible-playbook site.yml --tags security只运行带有指定标签的任务 | 中级 | ansible-playbook site.yml --tags security | |
ansible-playbook site.yml --limit web1将剧本执行限制在单个主机 | 中级 | ansible-playbook site.yml --limit web1 | |
ansible-playbook site.yml --private-key ~/.ssh/deploy_key指定 SSH 私钥连接远程主机 | 基础 | ansible-playbook site.yml --private-key ~/.ssh/deploy_key |
调试(2)
| 命令 | 难度 | ||
|---|---|---|---|
ansible-playbook site.yml -vvv以详细模式运行剧本(三级调试) | 中级 | ansible-playbook site.yml -vvv | |
debug打印调试信息 | 基础 | debug:
msg: "{{ disk_output.stdout }}" |
变量(5)
| 命令 | 难度 | ||
|---|---|---|---|
register注册任务输出到变量 | 中级 | register: disk_output | |
vars_prompt交互式变量提示 | 中级 | vars_prompt:
- name: admin_password
prompt: "Enter password"
private: yes | |
ansible-vault encrypt secrets.yml使用 Vault 加密敏感文件 | 高级 | ansible-vault encrypt secrets.yml | |
ansible-vault decrypt secrets.yml解密 Vault 加密文件 | 高级 | ansible-vault decrypt secrets.yml | |
ansible-playbook site.yml --ask-vault-pass运行剧本时提示输入 Vault 密码 | 高级 | ansible-playbook site.yml --ask-vault-pass |