Ansible自动化运维

Ansible自动化运维

概述

Ansible 是一款开源的自动化运维工具,基于 SSH 协议(无需在被控主机安装客户端),可实现批量主机管理、配置部署、任务执行、服务启停等自动化操作。

核心优势:轻量、无客户端、配置简单、可扩展性强。

本文档以「命令行实操」为核心,搭配高频场景 Playbook 示例,帮助运维/开发人员快速掌握 Ansible 的基础使用,可直接复制命令和示例进行实操。


安装

安装步骤

# 1. 安装 EPEL 源(CentOS 默认无 Ansible 仓库)
yum install -y epel-release

# 2. 安装 Ansible
yum install -y ansible

# 3. 验证安装
ansible --version
# 正常输出示例:ansible [core 2.14.2]

主机配置

Ansible 通过「主机清单」(Inventory)识别被控主机,默认路径为 /etc/ansible/hosts,也支持自定义清单文件(推荐,避免修改系统默认文件)。

方式一:编辑默认清单

路径:/etc/ansible/hosts

# 格式1:单主机(IP)
192.168.1.100

# 格式2:单主机(IP + 端口 + 用户)
192.168.1.101:22 ansible_user=root ansible_ssh_pass=123456

# 格式3:主机组(批量管理,推荐)
[webservers]
192.168.1.102 ansible_user=root ansible_ssh_pass=123456
192.168.1.103 ansible_user=root ansible_ssh_pass=123456

[dbservers]
192.168.1.104 ansible_user=root ansible_ssh_pass=123456

# 格式4:主机组嵌套
[myhost:children]
webservers
dbservers

# 格式5:组变量复用
[myhost:vars]
ansible_ssh_user=root
ansible_ssh_pass=123456
ansible_ssh_port=22
ansible_ssh_common_args='-o ProxyCommand="ssh -W %h:%p -q root@111.111.111.111"'  # 跳板机

方式二:自定义清单(推荐)

创建自定义清单文件(如 inventory.ini),通过 -i 参数指定。

# 1. 创建清单文件
vim inventory.ini
# 2. 写入内容(推荐使用密钥认证,更安全)
[web]
web1 ansible_host=192.168.1.102 ansible_user=root ansible_ssh_private_key_file=/root/.ssh/id_rsa
web2 ansible_host=192.168.1.103 ansible_user=root ansible_ssh_private_key_file=/root/.ssh/id_rsa

[db]
db1 ansible_host=192.168.1.104 ansible_user=root ansible_ssh_private_key_file=/root/.ssh/id_rsa

验证连通性

使用 ping 模块测试控制节点与被控主机的连通性,这是 Ansible 实操的第一步。

# 测试默认清单中所有主机
ansible all -m ping

# 测试默认清单中 webservers 组
ansible webservers -m ping

# 测试自定义清单中 web 组
ansible web -i inventory.ini -m ping

# 测试自定义清单中单台主机
ansible web1 -i inventory.ini -m ping

# 密码认证方式(临时指定密码)
ansible web -i inventory.ini -m ping -k
# 执行后会提示输入被控主机密码

成功标志:返回 "pong": "pong",表示连通正常。

失败排查:请检查 SSH 服务状态、网络连通性、密码/密钥是否正确。


常用模块示例

1. 执行 Shell 命令(默认模块)

# 批量执行 ls 命令
ansible webservers -a "ls"

# 批量执行自定义命令
ansible webservers -a "df -h"

2. 拷贝文件(copy 模块)

ansible webservers -m copy -a "src=/root/nginx.conf dest=/etc/nginx/nginx.conf mode=644"

3. 修改文件内容(lineinfile 模块)

# 将文件中匹配 '^111' 的行替换为 '123'
ansible webservers -m lineinfile -a "path=/root/hxq.txt regexp='^111' line='123'"

# 在文件末尾追加配置(防止重复)
ansible webservers -m lineinfile -a "path=/root/hxq.txt line='222' regexp='^222$' state=present"

# 在文件开头插入配置(防止重复)
ansible webservers -m lineinfile -a "path=/root/hxq.txt line='555' regexp='^555$' insertafter=BOF state=present"

4. 软件包管理(yum 模块)

# 批量安装 nginx
ansible webservers -m yum -a "name=nginx state=present"

# 批量卸载 nginx
ansible webservers -m yum -a "name=nginx state=absent"

# 批量升级 nginx 到最新版本
ansible webservers -m yum -a "name=nginx state=latest"

5. 服务管理(service 模块)

# 批量启动 nginx 并设置开机自启
ansible webservers -m service -a "name=nginx state=started enabled=yes"

# 批量停止 nginx
ansible webservers -m service -a "name=nginx state=stopped"

# 批量重启 nginx
ansible webservers -m service -a "name=nginx state=restarted"

# 批量重新加载配置(不中断服务)
ansible webservers -m service -a "name=nginx state=reloaded"

常用模块速查表

模块 用途 示例
ping 测试主机连通性 ansible all -m ping
command 执行命令(默认) ansible all -a "uptime"
shell 执行 Shell 命令(支持管道) ansible all -m shell -a "ps aux | grep nginx"
copy 拷贝文件到远程主机 ansible all -m copy -a "src=... dest=..."
file 管理文件和目录属性 ansible all -m file -a "path=/tmp/test state=directory"
lineinfile 修改文件中的行 ansible all -m lineinfile -a "path=... regexp=... line=..."
yum 软件包管理(CentOS) ansible all -m yum -a "name=nginx state=present"
service 服务管理 ansible all -m service -a "name=nginx state=started"
user 用户管理 ansible all -m user -a "name=test state=present"
group 用户组管理 ansible all -m group -a "name=test state=present"

常见问题

Q:ansible 命令找不到?

确认 Ansible 是否安装成功:

which ansible

如未安装,请重新执行安装步骤。

Q:ping 模块返回 UNREACHABLE

检查以下几点:

  1. 被控主机 SSH 服务是否正常运行:systemctl status sshd
  2. 网络是否互通:ping 192.168.1.102
  3. 用户名/密码/密钥是否正确
  4. 防火墙是否放行 SSH 端口(默认 22)

Q:如何查看所有可用模块?

ansible-doc -l

查看指定模块的详细用法:

ansible-doc copy

Q:如何实现跳板机代理?

在清单文件中配置 ansible_ssh_common_args

[myhost:vars]
ansible_ssh_common_args='-o ProxyCommand="ssh -W %h:%p -q root@111.111.111.111"'

参考链接