Ansible 初学者指南 2026

Table of Contents
Ansible 初学者指南:2026 年 IT 自动化完整指南
Ansible 已发展为行业领先的自动化平台。根据来源,财富 500 强企业中有超过 80% 使用 Ansible 管理复杂的 IT 基础设施。2026 年,Ansible 仍是重要的自动化方案,兼具易用性和企业级功能。
本指南将帮助你从入门走向熟练使用 Ansible,内容涵盖基础概念、高级技术、实际案例和 2026 年最佳实践。
什么是 Ansible?了解基础知识
Ansible 是一个开源自动化平台,通过无代理的声明式自动化简化 IT 基础设施管理。与传统配置管理工具不同,它无需在受管节点上安装代理。远程管理 Linux 和 Unix 时使用 SSH,管理 Windows 时使用 WinRM。
Ansible 的主要特点
1. 无代理架构:
- 无需在受管节点上安装软件
- 使用现有的 SSH 或 WinRM 协议
- 减少攻击面和维护工作
- 比需要代理的工具更容易上手
2. 声明式语言 (YAML):
- 配置文件易于阅读
- 将基础设施描述为期望状态
- 幂等操作可安全地重复执行
- 没有编程经验也容易理解
3. 推送模型:
- 控制节点向受管节点发送配置
- 无需等待代理即可执行任务
- 集中控制并提供可见性
- 适合按需自动化
4. 丰富的模块库:
- 超过 5,000 个内置模块 (截至 2026 年)
- 支持 Linux、Windows、网络设备和云平台
- 提供社区和供应商开发的模块
- 使用 Python 编写自定义模块很简单
Ansible 可以自动化哪些任务?
Ansible 适用于以下自动化工作:
- 配置管理:保持服务器配置一致
- 应用部署:在不同环境中部署应用
- 资源配置:创建并配置基础设施
- 持续交付:集成 CI/CD 流水线
- 安全与合规:执行安全策略和合规要求
- 编排:协调复杂的多层部署
- 云自动化:管理 AWS、Azure、GCP 和其他云平台
- 网络自动化:配置交换机、路由器和防火墙
- Windows 自动化:管理 Windows 服务器和工作站
Ansible 的发展:2012 至 2026 年
时间线:
- 2012 年:Michael DeHaan 创建 Ansible
- 2015 年:Red Hat 收购 Ansible
- 2019 年:Ansible Engine 2.8 引入集合
- 2020 年:Ansible 2.10+ 拆分为 ansible-core 和集合
- 2023 年:Ansible 8.x 扩展云集成
- 2026 年:Ansible 10.x 增加 AI 辅助 Playbook 生成、改进的 Windows 支持和扩展的安全功能
Ansible 与竞品:快速比较
| 功能 | Ansible | Terraform | Puppet | Chef |
|---|---|---|---|---|
| 类型 | 配置管理 | 基础设施配置 | 配置管理 | 配置管理 |
| 语言 | YAML | HCL | Puppet DSL (类似 Ruby) | Ruby DSL |
| 是否需要代理 | 否 | 否 | 是 | 是 |
| 学习难度 | 低 | 中等 | 高 | 高 |
| 适合场景 | 配置管理、部署 | 云基础设施 | 大型企业 | 复杂工作流 |
| 状态管理 | 推送模型 | 声明式状态文件 | 拉取模型 | 拉取模型 |
| Windows 支持 | 优秀 (2026) | 有限 | 良好 | 良好 |
| 社区规模 | 很大 | 很大 | 大 | 中等 |
| 主要用途 | 日常运维 | 基础设施配置 | 企业配置管理 | 应用部署 |
选择 Ansible 的情况:
- ✅ 在混合环境中管理配置
- ✅ 自动化应用部署
- ✅ 需要较短的学习周期
- ✅ 优先选择无代理架构
- ✅ 需要强大的 Windows 自动化 (2026 年的改进)
- ✅ 已有 SSH 基础设施
考虑替代方案的情况:
- Terraform:主要用于基础设施配置 (AWS、Azure、GCP)
- Puppet:拥有数千个节点的大型企业
- Chef:以应用为中心的复杂工作流
入门:2026 年安装 Ansible
系统要求
运行 Ansible 的控制节点:
- Linux、macOS 或 Windows 上的 WSL2
- Python 3.9 或更高版本 (2026 年推荐 Python 3.11+)
- 至少 2 GB RAM,建议 4 GB 或更多
- 为集合预留 5 GB 磁盘空间
受管节点:
- SSH 访问 (Linux/Unix) 或 WinRM (Windows)
- Python 2.7 或 Python 3.6+ (Linux)
- PowerShell 5.1+ (Windows)
- 无需安装代理
安装方式
方式 1:包管理器 (推荐初学者使用)
Ubuntu/Debian:
# 添加最新版 Ansible PPA
sudo apt-add-repository ppa:ansible/ansible
sudo apt update
sudo apt install ansible -y
RHEL/CentOS/Rocky Linux:
# 启用 EPEL 仓库
sudo dnf install epel-release -y
sudo dnf install ansible -y
macOS:
# 使用 Homebrew
brew install ansible
方式 2:Python pip (获取最新功能)
# 安装 pipx(推荐使用隔离环境)
python3 -m pip install --user pipx
python3 -m pipx ensurepath
# 使用 pipx 安装 Ansible
pipx install --include-deps ansible
# 验证安装
ansible --version
方式 3:Docker (便携方式)
# 拉取官方 Ansible 容器
docker pull quay.io/ansible/ansible:latest
# 在容器中运行 Ansible
docker run --rm -it quay.io/ansible/ansible:latest ansible --version
验证安装
# 检查 Ansible 版本
ansible --version
# 预期输出 (2026):
# ansible [core 2.16.5]
# config file = /etc/ansible/ansible.cfg
# python version = 3.11.8
# jinja version = 3.1.3
初始配置
创建 Ansible 配置文件:
# 创建配置目录
mkdir -p ~/.ansible
# 创建 ansible.cfg
cat > ~/.ansible/ansible.cfg << 'EOF'
[defaults]
inventory = ~/ansible/inventory
host_key_checking = False
retry_files_enabled = False
gathering = smart
fact_caching = jsonfile
fact_caching_connection = /tmp/ansible_facts
fact_caching_timeout = 3600
[privilege_escalation]
become = True
become_method = sudo
become_user = root
become_ask_pass = False
EOF
核心概念:Ansible 的组成部分
1. 清单:定义基础设施
清单定义 Ansible 管理的主机和组。清单可以是静态的 (INI 或 YAML 文件),也可以是动态的 (脚本或插件)。
静态清单 (INI 格式)
# inventory/hosts.ini
# Web 服务器组
[webservers]
web1.example.com
web2.example.com
web3.example.com
# 数据库服务器组
[databases]
db1.example.com ansible_host=192.168.1.100
db2.example.com ansible_host=192.168.1.101
# 负载均衡器
[loadbalancers]
lb1.example.com
# 组的组
[production:children]
webservers
databases
loadbalancers
# 所有 Web 服务器的变量
[webservers:vars]
ansible_user=deploy
ansible_port=22
http_port=80
https_port=443
# 所有主机的变量
[all:vars]
ansible_python_interpreter=/usr/bin/python3
静态清单 (YAML 格式,2026 年推荐)
# inventory/hosts.yml
all:
vars:
ansible_python_interpreter: /usr/bin/python3
children:
webservers:
hosts:
web1.example.com:
web2.example.com:
web3.example.com:
vars:
ansible_user: deploy
http_port: 80
https_port: 443
databases:
hosts:
db1.example.com:
ansible_host: 192.168.1.100
db2.example.com:
ansible_host: 192.168.1.101
vars:
ansible_user: dbadmin
production:
children:
webservers:
databases:
动态清单 (云服务提供商)
在云环境中,使用动态清单插件:
AWS EC2:
# inventory/aws_ec2.yml
plugin: amazon.aws.ec2
regions:
- us-east-1
- us-west-2
filters:
tag:Environment: production
keyed_groups:
- key: tags.Application
prefix: app
Azure:
# inventory/azure_rm.yml
plugin: azure.azcollection.azure_rm
include_vm_resource_groups:
- production-rg
keyed_groups:
- key: tags.role
prefix: role
2. Playbook:自动化蓝图
Playbook 是描述自动化工作流的 YAML 文件,用于定义基础设施的期望状态。
简单的 Playbook 示例
# simple_playbook.yml
---
- name: 配置 Web 服务器
hosts: webservers
become: yes
tasks:
- name: 安装 Nginx
apt:
name: nginx
state: present
update_cache: yes
when: ansible_os_family == "Debian"
- name: 启动并启用 Nginx
service:
name: nginx
state: started
enabled: yes
- name: 复制网站内容
copy:
src: ./website/
dest: /var/www/html/
owner: www-data
group: www-data
mode: '0644'
- name: 配置防火墙
ufw:
rule: allow
port: '{{ item }}'
proto: tcp
loop:
- 80
- 443
运行 Playbook
# 基本执行
ansible-playbook simple_playbook.yml
# 使用指定清单
ansible-playbook -i inventory/hosts.yml simple_playbook.yml
# 限制到指定主机
ansible-playbook simple_playbook.yml --limit web1.example.com
# 试运行(检查模式)
ansible-playbook simple_playbook.yml --check
# 用于故障排查的详细输出
ansible-playbook simple_playbook.yml -vvv
3. 模块:可重复使用的任务单元
Ansible 模块是自动化的基础组件。每个模块执行特定任务。
常用模块 (2026)
系统模块:
# 软件包管理
- name: 安装软件包 (apt)
apt:
name:
- nginx
- git
- python3-pip
state: present
# 服务管理
- name: 管理服务
systemd:
name: nginx
state: started
enabled: yes
文件模块:
# 文件操作
- name: 创建目录
file:
path: /opt/myapp
state: directory
owner: appuser
mode: '0755'
# 复制文件
- name: 部署配置
copy:
src: config.yml
dest: /etc/myapp/config.yml
backup: yes
# 带变量的模板
- name: 部署 nginx 配置
template:
src: nginx.conf.j2
dest: /etc/nginx/nginx.conf
notify: restart nginx
云模块 (AWS 示例):
# 创建 EC2 实例
- name: 启动 EC2 实例
amazon.aws.ec2_instance:
name: webserver-01
instance_type: t3.medium
image_id: ami-0c55b159cbfafe1f0
region: us-east-1
vpc_subnet_id: subnet-12345678
security_group: web-sg
key_name: my-keypair
tags:
Environment: production
Application: web
# 创建 S3 存储桶
- name: 创建 S3 存储桶
amazon.aws.s3_bucket:
name: my-app-bucket
region: us-east-1
versioning: yes
encryption: AES256
数据库模块:
# MySQL 数据库管理
- name: 创建 MySQL 数据库
mysql_db:
name: webapp_db
state: present
login_user: root
login_password: "{{ mysql_root_password }}"
- name: 创建 MySQL 用户
mysql_user:
name: webapp_user
password: "{{ db_password }}"
priv: 'webapp_db.*:ALL'
state: present
4. 变量:让 Playbook 更灵活
变量允许你在不同环境中重复使用 Playbook。
变量优先级 (从高到低)
- 额外变量 (命令行中的
-e) - 任务变量
- 块变量
- 角色和 include 变量
- Play 变量
- 主机事实
- Playbook 的
host_vars - Playbook 的
group_vars - 清单的
host_vars - 清单的
group_vars - 角色默认值
变量示例
Playbook 变量:
---
- name: 部署应用程序
hosts: webservers
vars:
app_name: myapp
app_version: 2.5.0
app_port: 8080
tasks:
- name: 部署 {{ app_name }} 版本 {{ app_version }}
docker_container:
name: "{{ app_name }}"
image: "registry.example.com/{{ app_name }}:{{ app_version }}"
ports:
- "{{ app_port }}:8080"
group_vars (环境专用):
# group_vars/production.yml
app_environment: production
db_host: prod-db.example.com
db_port: 3306
log_level: warning
max_connections: 1000
# group_vars/development.yml
app_environment: development
db_host: dev-db.example.com
db_port: 3306
log_level: debug
max_connections: 100
Ansible Vault (加密变量):
# 创建加密文件
ansible-vault create secrets.yml
# 编辑加密文件
ansible-vault edit secrets.yml
# secrets.yml 内容:
# db_password: supersecret123
# api_key: abc123xyz789
# 在 playbook 中使用
ansible-playbook deploy.yml --ask-vault-pass
5. 角色:组织复杂自动化
角色提供结构化的 Playbook 组织方式,使其模块化并可重复使用。
完整角色结构
roles/
└── nginx/
├── defaults/
│ └── main.yml # 默认变量
├── vars/
│ └── main.yml # 角色变量
├── tasks/
│ ├── main.yml # 主要任务列表
│ ├── install.yml # 安装任务
│ └── configure.yml # 配置任务
├── handlers/
│ └── main.yml # 处理程序(重启服务)
├── templates/
│ ├── nginx.conf.j2 # Jinja2 模板
│ └── site.conf.j2
├── files/
│ └── custom_module.sh # 静态文件
├── meta/
│ └── main.yml # 角色依赖项
└── README.md # 文档
角色示例:nginx
defaults/main.yml:
---
nginx_port: 80
nginx_user: www-data
nginx_worker_processes: auto
nginx_worker_connections: 1024
tasks/main.yml:
---
- name: 包含特定于操作系统的变量
include_vars: "{{ ansible_os_family }}.yml"
- name: 安装 Nginx
include_tasks: install.yml
- name: 配置 Nginx
include_tasks: configure.yml
- name: 确保 Nginx 正在运行
service:
name: nginx
state: started
enabled: yes
tasks/install.yml:
---
- name: 安装 Nginx (Debian)
apt:
name: nginx
state: present
update_cache: yes
when: ansible_os_family == "Debian"
- name: 安装 Nginx (RedHat)
yum:
name: nginx
state: present
when: ansible_os_family == "RedHat"
handlers/main.yml:
---
- name: 重启 nginx
service:
name: nginx
state: restarted
- name: 重新加载 nginx
service:
name: nginx
state: reloaded
templates/nginx.conf.j2:
user {{ nginx_user }};
worker_processes {{ nginx_worker_processes }};
events {
worker_connections {{ nginx_worker_connections }};
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
server {
listen {{ nginx_port }};
server_name {{ ansible_fqdn }};
location / {
root /var/www/html;
index index.html;
}
}
}
使用角色:
---
- name: 使用 Nginx 角色配置 Web 服务器
hosts: webservers
become: yes
roles:
- role: nginx
vars:
nginx_port: 8080
nginx_worker_processes: 4
Ansible 高级概念 (2026)
Ansible 集合
集合是 Ansible 内容的分发格式,包括模块、角色和插件。
安装集合
# 从 Ansible Galaxy 安装
ansible-galaxy collection install community.general
ansible-galaxy collection install amazon.aws
ansible-galaxy collection install ansible.windows
# 从 requirements 文件安装
# requirements.yml:
collections:
- name: community.general
version: ">=6.0.0"
- name: amazon.aws
version: "5.2.0"
ansible-galaxy collection install -r requirements.yml
使用集合模块
---
- name: 使用集合模块
hosts: localhost
tasks:
- name: 使用 community.general 中的 Docker 模块
community.general.docker_container:
name: webapp
image: nginx:latest
- name: 使用 AWS EC2 模块
amazon.aws.ec2_instance:
name: webserver
instance_type: t3.micro
Ansible Tower / AWX (企业功能)
Ansible Tower (商业版) 和 AWX (开源版) 提供:
- 用于执行 Playbook 的 Web 界面
- 基于角色的访问控制 (RBAC)
- 作业调度和工作流
- 集中式日志和审计
- 用于集成的 REST API
- 凭据管理
- 清单同步
使用 Docker Compose 安装 AWX:
# 克隆 AWX 仓库
git clone https://github.com/ansible/awx.git
cd awx
# 使用 docker-compose 部署
cd tools/docker-compose
docker-compose -f docker-compose.yml up -d
# 访问 http://localhost:80
# 默认登录:admin / password
使用 Ansible 自动化 Windows
2026 年的 Ansible 显著改进了 Windows 支持:
Windows 前提条件:
# 在 Windows 主机上启用 WinRM
$url = "https://raw.githubusercontent.com/ansible/ansible/devel/examples/scripts/ConfigureRemotingForAnsible.ps1"
$file = "$env:temp\ConfigureRemotingForAnsible.ps1"
(New-Object -TypeName System.Net.WebClient).DownloadFile($url, $file)
powershell.exe -ExecutionPolicy ByPass -File $file
Windows 清单:
[windows]
win1.example.com
[windows:vars]
ansible_user=Administrator
ansible_password=SecurePassword123
ansible_connection=winrm
ansible_winrm_transport=ntlm
ansible_winrm_server_cert_validation=ignore
ansible_port=5986
Windows 自动化 Playbook:
---
- name: Windows 服务器配置
hosts: windows
gather_facts: yes
tasks:
- name: 安装 IIS
win_feature:
name: Web-Server
state: present
include_management_tools: yes
- name: 部署网站
win_copy:
src: ./website/
dest: C:\inetpub\wwwroot\
- name: 配置防火墙
win_firewall_rule:
name: 允许 HTTP
localport: 80
protocol: tcp
action: allow
direction: in
enabled: yes
- name: 通过 Chocolatey 安装软件
win_chocolatey:
name:
- googlechrome
- 7zip
- notepadplusplus
state: present
使用 Ansible 自动化网络
Ansible 支持主要网络设备供应商,包括 Cisco、Juniper、Arista 和 F5。
网络设备 Playbook:
---
- name: 配置 Cisco 交换机
hosts: cisco_switches
gather_facts: no
connection: network_cli
vars:
ansible_network_os: ios
ansible_user: admin
ansible_password: "{{ vault_switch_password }}"
tasks:
- name: 配置 VLAN
cisco.ios.ios_vlans:
config:
- vlan_id: 10
name: DATA
- vlan_id: 20
name: VOICE
- vlan_id: 30
name: GUEST
state: merged
- name: 配置接口
cisco.ios.ios_interfaces:
config:
- name: GigabitEthernet1/0/1
description: Uplink to Core
enabled: true
- name: 保存配置
cisco.ios.ios_config:
save_when: always
实际应用场景和示例
场景 1:完整部署 Web 应用
项目结构:
webapp-deployment/
├── ansible.cfg
├── inventory/
│ ├── production
│ └── staging
├── group_vars/
│ ├── all.yml
│ ├── production.yml
│ └── staging.yml
├── roles/
│ ├── common/
│ ├── nginx/
│ ├── postgresql/
│ └── application/
└── playbooks/
├── site.yml
└── deploy.yml
site.yml (全栈部署):
---
- name: 配置所有服务器
hosts: all
become: yes
roles:
- common
- name: 配置数据库服务器
hosts: databases
become: yes
roles:
- postgresql
- name: 配置 Web 服务器
hosts: webservers
become: yes
roles:
- nginx
- application
- name: 配置负载均衡器
hosts: loadbalancers
become: yes
roles:
- haproxy
场景 2:搭建 Kubernetes 集群
---
- name: 设置 Kubernetes 集群
hosts: k8s_cluster
become: yes
tasks:
- name: 安装 Docker
apt:
name: docker.io
state: present
- name: 添加 Kubernetes apt 密钥
apt_key:
url: https://packages.cloud.google.com/apt/doc/apt-key.gpg
state: present
- name: 添加 Kubernetes 仓库
apt_repository:
repo: deb https://apt.kubernetes.io/ kubernetes-xenial main
state: present
- name: 安装 Kubernetes 组件
apt:
name:
- kubelet=1.28.0-00
- kubeadm=1.28.0-00
- kubectl=1.28.0-00
state: present
- name: 初始化 Kubernetes 主节点
hosts: k8s_master
become: yes
tasks:
- name: 初始化集群
command: kubeadm init --pod-network-cidr=10.244.0.0/16
args:
creates: /etc/kubernetes/admin.conf
- name: 创建 .kube 目录
file:
path: /home/{{ ansible_user }}/.kube
state: directory
owner: "{{ ansible_user }}"
- name: 复制 admin.conf
copy:
src: /etc/kubernetes/admin.conf
dest: /home/{{ ansible_user }}/.kube/config
remote_src: yes
owner: "{{ ansible_user }}"
场景 3:自动化安全加固
---
- name: 强化安全性
hosts: all
become: yes
tasks:
- name: 更新所有软件包
apt:
upgrade: dist
update_cache: yes
- name: 安装安全工具
apt:
name:
- fail2ban
- ufw
- aide
state: present
- name: 配置 fail2ban
copy:
src: jail.local
dest: /etc/fail2ban/jail.local
notify: restart fail2ban
- name: 配置防火墙
ufw:
rule: "{{ item.rule }}"
port: "{{ item.port }}"
proto: "{{ item.proto }}"
loop:
- { rule: 'allow', port: '22', proto: 'tcp' }
- { rule: 'allow', port: '80', proto: 'tcp' }
- { rule: 'allow', port: '443', proto: 'tcp' }
- { rule: 'deny', port: '3306', proto: 'tcp' }
- name: 启用防火墙
ufw:
state: enabled
- name: 禁用 root 登录
lineinfile:
path: /etc/ssh/sshd_config
regexp: '^PermitRootLogin'
line: 'PermitRootLogin no'
notify: restart sshd
- name: 设置密码策略
lineinfile:
path: /etc/login.defs
regexp: '^PASS_MAX_DAYS'
line: 'PASS_MAX_DAYS 90'
handlers:
- name: 重启 fail2ban
service:
name: fail2ban
state: restarted
- name: 重启 sshd
service:
name: sshd
state: restarted
Ansible 最佳实践 (2026 年版)
1. 使用版本控制 (Git)
仓库结构:
ansible-infrastructure/
├── .gitignore
├── README.md
├── ansible.cfg
├── inventory/
├── group_vars/
├── host_vars/
├── roles/
├── playbooks/
└── files/
.gitignore:
*.retry
*.pyc
.vault_pass
secrets.yml
*.log
2. 实现幂等性
错误做法 (非幂等):
- name: 向文件添加一行
shell: echo "new line" >> /etc/myconfig
正确做法 (幂等):
- name: 确保文件中存在该行
lineinfile:
path: /etc/myconfig
line: "new line"
state: present
3. 使用 Ansible Lint 检查代码质量
# 安装 ansible-lint
pip install ansible-lint
# 运行代码检查器
ansible-lint playbook.yml
# 自动修复常见问题
ansible-lint --fix playbook.yml
4. 使用 Molecule 进行测试
# 安装 Molecule
pip install molecule molecule-docker
# 初始化带测试的新角色
molecule init role my_role --driver-name docker
# 运行测试
cd my_role
molecule test
# 测试阶段:
# - 创建测试环境
# - 收敛(运行 playbook)
# - 验证(运行测试)
# - 销毁环境
molecule.yml 示例:
---
dependency:
name: galaxy
driver:
name: docker
platforms:
- name: ubuntu2204
image: geerlingguy/docker-ubuntu2204-ansible:latest
pre_build_image: true
provisioner:
name: ansible
verifier:
name: ansible
5. 使用标签选择要执行的任务
---
- name: 完整服务器设置
hosts: webservers
become: yes
tasks:
- name: 安装软件包
apt:
name: nginx
state: present
tags:
- packages
- nginx
- name: 配置 Nginx
template:
src: nginx.conf.j2
dest: /etc/nginx/nginx.conf
tags:
- configuration
- nginx
- name: 部署应用程序
copy:
src: app/
dest: /var/www/html/
tags:
- deployment
- application
# 仅运行 nginx 任务
# ansible-playbook play.yml --tags nginx
# 跳过部署
# ansible-playbook play.yml --skip-tags deployment
6. 错误处理和调试
---
- name: 可靠地执行任务
hosts: webservers
tasks:
- name: 尝试高风险操作
command: /usr/bin/risky_command
register: result
failed_when: false
changed_when: result.rc == 0
- name: 调试输出
debug:
var: result
verbosity: 2
- name: 处理故障
debug:
msg: "命令失败,但仍继续执行……"
when: result.rc != 0
- name: 带恢复操作的块
block:
- name: 先尝试此操作
command: /may/fail
- name: 成功时执行此操作
debug:
msg: "成功!"
rescue:
- name: 失败时改为执行此操作
debug:
msg: "已从失败中恢复"
always:
- name: 始终执行此操作
debug:
msg: "清理操作"
7. 优化性能
启用 pipelining
# ansible.cfg
[defaults]
pipelining = True
使用 Mitogen (性能提升 3-7 倍)
# 安装 Mitogen
pip install mitogen
# ansible.cfg
[defaults]
strategy_plugins = /path/to/mitogen/ansible_mitogen/plugins/strategy
strategy = mitogen_linear
并行执行
---
- name: 并行更新
hosts: all
strategy: free # 不要等待所有主机
serial: 10 # 每次处理 10 台主机
tasks:
- name: 更新软件包
apt:
upgrade: dist
常见问题排查
问题 1:SSH 连接失败
问题:无法连接到受管主机
解决方案:
# 测试原始 SSH 连接
ssh -vvv user@host
# 测试 Ansible 连接
ansible host -m ping -vvv
# 添加到清单
[all:vars]
ansible_ssh_common_args='-o StrictHostKeyChecking=no'
问题 2:权限被拒绝 (sudo)
问题:任务因权限错误而失败
解决方案:
---
- hosts: webservers
become: yes
become_method: sudo
become_user: root
tasks:
- name: 需要 root 权限的任务
file:
path: /etc/myconfig
state: touch
问题 3:未找到模块
问题:出现模块未找到错误
解决方案:
# 确保集合已安装
ansible-galaxy collection install community.general
# 验证集合
ansible-galaxy collection list
# 使用 FQCN(完全限定集合名称)
community.general.docker_container:
name: mycontainer
问题 4:Playbook 执行缓慢
解决方案:
- 启用 pipelining (参见“性能”部分)
- 使用 Mitogen 策略插件
- 如果不需要,关闭事实收集:
- hosts: all gather_facts: no - 对长时间运行的任务使用异步执行:
- name: 长时间运行的任务 command: /usr/bin/long_task async: 3600 poll: 0
Ansible 职业发展和认证
技能进阶
初级 (0-6 个月):
- 编写基本的 Playbook
- 理解清单和变量
- 使用常用模块
- 执行 Playbook
中级 (6-18 个月):
- 创建可重复使用的角色
- 实现错误处理
- 使用 Ansible Vault
- 与 CI/CD 集成
- 管理多个环境
高级 (18 个月以上):
- 设计复杂的自动化框架
- 创建自定义模块和插件
- 设计 Tower/AWX 部署
- 优化性能
- 指导他人
认证
Red Hat Certified Specialist in Ansible Automation (EX407)
- 证明 Ansible 技能
- 基于实际操作的考试
- 有效期为 3 年
- 获得行业认可
备考资源:
- Red Hat Learning Subscription
- Ansible 文档
- 实践实验室 (A Cloud Guru、Udemy)
- 社区论坛
就业市场 (2026)
美国平均薪资:
- 初级 Ansible 工程师:$75,000-$95,000
- 中级 DevOps 工程师 (Ansible):$110,000-$140,000
- 高级自动化架构师:$150,000-$190,000
热门技能:
- Ansible 与 Kubernetes
- Ansible 与 Terraform (混合 IaC)
- Ansible 与 CI/CD (Jenkins、GitLab)
- Ansible 与云平台 (AWS、Azure、GCP)
- Ansible Tower/AWX 管理
学习资源和社区
官方文档
在线课程
- Red Hat Learning Subscription
- Udemy:
Ansible 绝对初学者指南 - A Cloud Guru:
Ansible 实战 - Linux Academy:
Ansible 快速入门
书籍
- Jeff Geerling 的
Ansible for DevOps(2025 年更新) - Lorin Hochstein 的
Ansible: Up and Running - James Freeman 的
Mastering Ansible
社区
- Ansible 邮件列表
- Reddit 上的 Ansible
- Ansible Discord 服务器
- 本地和线上 Ansible 聚会
练习平台
- Katacoda Ansible 场景
- Ansible Playground
- 使用 Vagrant 或 Docker 搭建本地实验环境
总结:开始你的 Ansible 学习之旅
Ansible 已成为全球组织优先选择的自动化工具。2026 年的版本提供更多功能、更简单的 Windows 管理、更完善的云集成,以及 AI 辅助的 Playbook 生成。无论你管理传统基础设施、云环境还是混合部署,Ansible 技能都能在整个 IT 职业生涯中发挥作用。
要点:
- 从简单开始:先用基本 Playbook 管理几台服务器
- 定期练习:搭建家庭实验环境并动手尝试
- 边做边学:自动化日常工作中的实际任务
- 遵循最佳实践:使用版本控制、测试和文档
- 加入社区:向他人学习并分享知识
- 跟进变化:Ansible 更新很快,持续学习
- 组合工具:Ansible、Terraform 和 Kubernetes 可共同实现广泛的自动化
后续步骤:
- 在你的系统上安装 Ansible
- 为测试服务器、虚拟机或容器创建简单清单
- 编写第一个 Playbook,例如安装软件包
- 逐步增加模板、变量和角色
- 为 Ansible Galaxy 社区角色贡献内容
- 考虑获取认证以验证技能
从 Ansible 初学者成长为专家很有意义,也能打开 DevOps、SRE 和云工程领域的职业机会。掌握本指南中的基础知识后,你便能开始自动化基础设施,并培养适用于现代 IT 行业的技能。
欢迎进入 Ansible 基础设施自动化领域!
参考资料
- Ansible 官方文档
- Ansible Galaxy - 角色仓库
- GitHub 上的 Ansible 仓库
- Red Hat Ansible Automation Platform
- AWX - 开源 Ansible Tower
- Ansible 最佳实践
- Molecule 测试框架
- Ansible Lint
- Jeff Geerling 的 Ansible for DevOps
- Ansible 集合索引






