Ansible

Ansible

简介

本文档以「命令行实操」为核心,搭配高频场景Playbook示例,帮助运维/开发人员快速掌握Ansible的基础使用、主机管理、模块调用及Playbook编写,可直接复制命令和示例进行实操,降低入门成本。 Ansible是一款开源的自动化运维工具,基于SSH协议(无需在被控主机安装客户端),可实现批量主机管理、配置部署、任务执行、服务启停等自动化操作,核心优势:轻量、无客户端、配置简单、可扩展性强。

安装

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

# 2. 安装Ansible
yum install -y ansible

# 3. 验证安装(查看版本)
ansible --version
# 正常输出示例:ansible [core 2.14.2]

主机配置

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

编辑默认主机清单(简单场景)

/etc/ansible/hosts
# 格式1:单主机(IP)
192.168.1.100

# 格式2:单主机(IP+端口+用户名,默认端口22可省略)
192.168.1.101:22 ansible_user=root ansible_ssh_pass=123456

# 格式3:主机组(批量管理,推荐)
[webservers]  # 主机组名称(自定义,如web服务器组)
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

# 格式4: 参数复用
[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 指定该文件即可。

在当前目录创建自定义清单

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

验证主机连通性(核心命令) 使用 ansible ping 模块,测试控制节点与被控主机的连通性,这是Ansible实操的第一步。

# 场景1:测试默认清单(/etc/ansible/hosts)中所有主机
ansible all -m ping

# 场景2:测试默认清单中webservers主机组
ansible webservers -m ping

# 场景3:测试自定义清单(inventory.ini)中web组
ansible web -i inventory.ini -m ping

# 场景4:测试自定义清单中单个主机(web1)
ansible web1 -i inventory.ini -m ping

# 场景5:密码认证,临时指定密码(避免明文写在清单)
ansible web -i inventory.ini -m ping -k
# 执行后会提示输入被控主机密码(ansible_ssh_pass)

成功提示:被控主机返回 “pong”: “pong”,表示连通正常;失败请检查SSH服务、网络、密码/密钥是否正确。

ansible示例

执行shell命令, 默认shell模块

ansible webservers -a "ls"

拷贝文件

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

修改文件

# 修改文件内容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"

服务操作

# 批量安装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"

# 批量启动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"

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