自动化运维之Ansible应用基础模块(超详细)
2018-10-19 00:23
1061 查看
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模块可以在被管理主机上运行命令,支持管道 5ae 符功能的复杂命令。
# 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是Ansib 7a52 le采集被管理主机设备信息的一个功能)。每个被管理主机在接受并运行管理命令之前,都鬼将自己的相关信息(操作系统版本、IP地址等)发送给控制主机。
# ansible-doc -s setup //查看setup模块信息 # ansible node3 -m setup //查看node3主机输出信息
相关文章推荐
- 自动化运维工具Ansible之Playbooks基础应用
- 自动化运维三贱客之 Ansible 的安装和命令应用基础
- 自动化运维工具ansible基础应用
- 自动化运维工具Ansible详细部署 - 人生理想在于坚持不懈 - 51CTO技术博客
- 自动化运维工具Ansible详细部署
- Ansible 自动化运维工具之inventory和常用模块介绍
- 自动化运维工具Ansible详细部署
- 自动化运维工具Ansible详细部署
- Ansible自动化运维之YAML、基础元素
- 自动化运维工具ansible详细介绍
- 自动化运维工具Ansible详细部署
- ansible 自动化运维工具应用之一
- 自动化运维工具Ansible详细部署
- 自动化运维工具Ansible详细部署
- 自动化运维工具Ansible实战(六)playbook常用的模块
- 自动化运维工具Ansible详细部署
- 自动化运维工具之ansible基础入门
- 自动化运维工具Ansible详细部署
- 自动化运维工具Ansible详细部署