Ansible 是一种基于模块进行工作的框架结构. 批量部署能力就是由 Ansible 所运行的模块实现的. 简而言之 Ansible 是基于 "模块" 完成各种 "任务" 的.
Ansible 常用的核心模块
模块的基本语法如下
ansible [主机] [-m 模块] [-a args] // 命令格式
- # ansible-doc -l // 列出所有已安装的模块 注: 按 q 退出
- # ansible-doc -s yum //-s 列出 yum 模块描述信息和操作动作
1.command 模块
command 模块用于被管理主机上运行命令.
- # ansible 192.168.92.156 -m command -a 'date' // 指定 IP 执行 date
- # ansible node2 -m command -a 'date' // 指定被管理主机中的分类执行 date
- # ansible all -m command -a 'date' // 所有主机清单上的主机执行 date
- # ansible all -a 'ls /etc' // 若省略 - m 选项, 默认运行 command 模块
2.cron 模块
cron 模块用于定义任务计划, 两种状态(state):present 表示添加(可以省略),absent 表示移除.
- # ansible-doc -s cron // 查看 cron 模块信息
- # ansible node2 -m cron -a 'minute="*/1"job="/bin/echo hello"name="test cron job"' // 添加计划任务
- # ansible node2 -a 'crontab -l' // 查看计划任务信息
- # ansible node2 -m cron -a 'name="test cron job"state=absent' // 移除计划任务
3.user 模块
user 模块用于创建新用户和更改, 删除已存在的用户. 其中 name 选项用来指明创建的用户名称.
- # ansible-doc -s user // 查看 user 模块信息
- # ansible node2 -m user -a 'name="test01"' // 创建用户 test01
- # ansible node2 -a 'tail /etc/passwd' // 查看 test01 用户
- # ansible node2 -m user -a 'name="test01"state=absent' // 删除用户 test01
4.group 模块
group 模块用于对用户组进行管理
- # ansible-doc -s group // 查看 group 模块信息
- # ansible node3 -m group -a 'name=node3 gid=306
- system=yes' // 创建 node3 系统组
- # ansible node3 -a 'tail /etc/group' // 查看组信息
- # ansible node3 -m user -a 'name=test01 uid=306 system=yes group=node3' // 将 test01 用户添加到 node3 组中
- # ansible node3 -a 'id test01' // 查看用户信息
5.copy 模块
copy 模块用于实现文件复制和批量下发文件, 其中用 src 定义本地文件路径, 使用 dest 定义被管理主机文件路径.
- # ansible-doc -s copy // 查看 copy 模块信息
- # ansible node3 -m copy -a 'src=/etc/fstab dest=/opt/fstab.bk owner=root mode=640'
- // 将本地文件 fstab 复制到被管理主机上, 所有者为 root, 权限为 640
- # ansible node3 -a 'ls -l /opt' // 查看文件信息
- # ansible node3 -a 'cat /opt/fstab.bk' // 查看文件内容
- # ansible node3 -m copy -a 'content="HELLO ansible"dest=/opt/fstab.bk'
- // 在 fstab.bk 文件中写入 HELLO ansible
- # ansible node3 -a 'cat /opt/fstab.bk' // 查看内容
6.file 模块
filem 模块是用来设置文件属性. 其中使用 path 指定文件路径, 使用 src 定义原文件路径, 使用 name 或 dest 来替换创建文件的符号链接.
- # ansible-doc -s file // 查看 file 模块信息
- # ansible node3 -m file -a 'owner=jerry group=jerry mode=644 path=/opt/fstab.bk'
- // 设置文件 fstab.bk 的所属主为 jerry, 所属组为 jerry, 权限 644
- # ansible node3 -m file -a 'path=/opt/fstab.link src=/opt/fstab.bk state=link'
- // 设置 / opt/fstab.link 为 / opt/fstab.bk 的链接文件
- # ansible node3 -m file -a 'path=/opt/fstab.link state=absent' // 删除一个文件
- # ansible node3 -m file -a 'path=/opt/abc state=touch' // 创建一个空文件
7.ping 模块
ping 模块用来检测指定主机的连通性.
# ansible all -m ping // 检测所有主机
8.yum 模块
yum 模块负责在被管理主机上安装与卸载软件包, 其中 name 指定安装的软件包, present,latest 用来表示安装, absent 表示卸载.
- # ansible-doc -s yum // 查看 yum 模块信息
- # ansible node3 -m yum -a 'name=httpd' // 安装 httpd 软件包
- # ansible node3 -m yum -a 'name=httpd state=absent' // 卸载 httpd 软件包
9.service 模块
service 模块用来控制被管理服务的运行状态, 其中 enabled 表示是否开机自启动, 取值 true 或 false; 使用 name 定义服务名称, 使用 state 指定服务状态, 取值分别为 started,stoped,restarted.
- # ansible-doc -s service // 查看 service 模块信息
- # ansible node3 -a 'systemctl status httpd' // 查看 httpd 服务状态
- # ansible node3 -m service -a 'enabled=true name=httpd state=started'
- // 启动 httpd 服务并设置开机自动启动
10.shell 模块
shell 模块可以在被管理主机上运行命令, 支持管道符功能的复杂命令.
- # ansible-doc -s shell // 查看 shell 模块信息
- # ansible node3 -m user -a 'name=tom' // 创建用户 tom
- # ansible node3 -m shell -a 'echo 123123|passwd --stdin tom'
- // 使用无交互模式给用户设置密码
11.script 模块
script 模块可以将本地脚本复制到被管理主机上进行运行, 需要注意的是, 使用相对路径来指定脚本.
# ansible-doc -s script // 查看 script 模块信息
编辑一个本地脚本 test.sh, 复制到被管理主机上运行.
- # VIM test.sh
- #!/bin/bash
- echo "this is test script"> /opt/test.txt
- chmod 666 /opt/test.txt
- # chmod +x test.sh
- # ansible node3 -m script -a 'test.sh' // 执行脚本
12.setup 模块
setup 模块用来收集, 查看被管理主机的 facts(facts 是 Ansible 采集被管理主机设备信息的一个功能). 每个被管理主机在接受并运行管理命令之前, 都鬼将自己的相关信息 (操作系统版本, IP 地址等) 发送给控制主机.
- # ansible-doc -s setup // 查看 setup 模块信息
- # ansible node3 -m setup // 查看 node3 主机输出信息
自动化运维之 Ansible 应用基础模块(超详细)
来源: http://www.bubuko.com/infodetail-2813305.html