在自动化运维的过程当中,咱们经常会经过命令行的形式使用Ansible模块,ansible自带了259各模块,咱们能够经过一些命令来查看ansible中所含的模块,也能够查看单一模块的信息。下面,我就为你们介绍一些经常使用的模块。
关于ansible的部署安装请参考:
部署自动化运维服务——Ansiblemysql
ansible-doc-l //显示全部自带模块 ansible-doc -s “模块名称” //查看具体模块的信息,使用‘q’退出介绍
Ah-Hoc经常使用可选项以下:web
command命令用于系统命令,不如ping、tail、date等命令,接下来我就以date、tail为例,介绍他的使用方法,接下来的全部操做中,都会使用“web“分类。sql
ansible web -m command -a 'date' //-a后的参数需使用单引号 ansible web -m command -a 'tail -1 /etc/passwd'
Ansible中的cron模块用于定义计划性任务。其中有两种状态(state):present表示添加(默认使用,可省略),absent表示移除。shell
ansible-doc -s cron //查看模块信息 ansible web -m cron -a 'minute="*/1" job="/bin/echo test" name="test cron job"' //每分钟执行一次输出test,name的名称能够自定义, ansible web -a 'crontab -l' //计划性任务,command为默认模块,-m可省略
ansible web -m cron -a 'name="test cron job" state=absent' //在建立时所使用的名称,取消是也要相对应,如果在建立时为设定名称,使用name=None便可
ping模块用来测试指定主机的联通性vim
ansible web -m ping
user模块用于建立新用户和更改、删除已存在的用户。其中name选项用于指明建立的用户名称bash
由于ansible user的passwd参数须要接收加密的值,因此须要利用openssl命令来生成密码,采用md5加密。并发
echo ansible | openssl passwd -1 -stdin //此处为“一”,不是“L”,明文为ansible
ansible web -m user -a 'name=zhang password="$1$zF5aYItH$MKXWT.NfH8kOV6OH3Y/Tp0"' -o
ssh 172.16.10.30 -l zhang //ssh远程登录
ansible web -m user -a 'name="zhang" state=absent'
group模块用于对用户组管理运维
ansible web -m group -a 'name=mysql gid=306 system=yes' //新建mysql组,gid=306 ansible web -m user -a 'name=lisi uid=306 system=yes group=mysql' //新建用户lisi,uid=306,并加入到mysql组
copy模块用于实现文件复制和批量下发文件。其中使用src来定义本地源文件路径,使用dest定义被管理主机文件路径,使用content则是经过指定信息内容来生成文件。注意,复制或者下发文件,必定时管理主机上有的文件,不要使用管理主机上不存在的文件或目录ssh
批量下发文件时,能够同时指定文件的属组属主,文件权限等属性ide
ansible web -m copy -a 'src=/etc/fstab dest=/opt/fstab.bak owner=lisi group=mysql mode=600 backup=yes' -o ansible web -m shell -a 'md5sum /opt/fstab.bak' -o //验证下发功能
ansible web -m copy -a 'content="hello word!" dest=/opt/a.txt' -o ansible web -m shell -a 'md5sum /opt/a.txt' -o
shell模块可在被管理主机上运行命令,并支持风管道符等功能的复杂命令
ansible zhangsan -m shell -a 'echo abc123 | passwd --stdin zhangsan' //免交互建立已有用户密码
file模块用来设置文件属性,其中使用path指定文件路径,使用src定义源文件路径,使用name或dest来替换建立文件的符号连接
ansible web -m file -a 'owner=lisi group=mysql mode=644 path=/opt/a.txt' -o ansible web -m shell -a 'ls -l /opt/a.txt'
ansible web -m file -a 'path=/opt/fstab.link src=/etc/fstab state=link' -o //src后的目录及文件都为和管理主机上全部
ansible web -m file -a "path=/opt/a.txt state=absent" -o
ansible web -m file -a "path=/opt/test state=touch" -o //建立一个文件
yum模块负责在被管理主机上安装与卸载软件包,可使用本地yum仓库,或是在线yum源,若是选用本地yum仓库,则需为被管理主机搭建yum仓库。如果使用在线,则直接安装,本次测试采用在线安装,不搭建本地yum仓库。
ansible web -m yum -a 'name=httpd state=latest' -o //在被管主机上安装最新版本httpd软件 ansible web -m shell -a 'rpm -qa httpd' -o //查看软件包安装状况
ansible web -m yum -a 'name=httpd state=absent' -o ansible web -m shell -a 'rpm -qa httpd' -o
service模块用来控制管理服务的运行状态,其中,使用enablesd表示是否开机自启,取值为true或false;使用name定义服务名称;使用state指定服务状态,分别取值startd、stoped、restarted。
ansible web -m service -a 'enabled=true name=httpd state=started' -o ansible web -m shell -a '/usr/bin/netstat -ntap | grep httpd' //查看服务启动是否成功 ansible web -a 'systemctl status httpd' //查看服务状态
script模块能够将本地脚本复制到被管理主机上进行运行。注意,使用相对路径来指定脚本
cd /opt vim test.sh #!/bin/bash echo this is ansible test > /opt/ansible.txt chmod +x test.sh
ansible web -m script -a 'test.sh' ansible web -m shell -a '/usr/bin/cat /opt/ansible.txt'
setup模块收集、查看被管主机的facts(facts是ansible采集被管理主机设备信息的一个功能)。每一个被管理主机在接收并运行管理命令以前。都会将本身的相关信息(操做系统、IP地址等)发送给控制主机。
ansible web -m setup //内容特别多就不贴图了