8、ansible的模块
1、ansible-hoc 临时命令
2、ansible-hoc 返回结果
绿色: 代表被管理端主机没有被修改
黄色: 代表被管理端主机发现变更
红色: 代表出现了故障,注意查看提示
8.1、command模块
command模块是ansible中默认模块。
参数:
chdir : 运行命令的目录
creates:在执行命令之前,判断是否已经存在该路径
案例:
使用 command 模块创建100个文件夹
[root@localhost test]# ansible web02 -m command -a 'mkdir /root/test/{1..100}'
8.2、shell模块
shell 模块跟 command 模块功能非常相似,都是执行命令的模块;但是shell模块支持特殊符号,性能没有 command 模块高。
参数:
chdir : 运行命令的目录
removes :判断一个文件是否存在,存在则运行。
[root@localhost 1]# ansible all -m shell -a 'mkdir abc; removes=/root/test/1'
creates:在执行命令之前,判断是否已经存在该路径
案例:
将 /etc 目录中文件名称包含 root 的文件打包到 /tmp 目录
[root@localhost ~]# ansible all -m shell -a "tar -czvPf /tmp/root.tar.gz `find /etc/ -name '*root*' | xargs`"
8.3、script(执行脚本)
script模块主要是用来执行脚本文件的。
参数:
chdir : 运行命令的目录
removes :判断一个文件是否存在,存在则运行。
creates :在执行命令之前,判断是否已经存在该路径
[root@localhost ~]# ansible all -m script -a './in.sh'
8.4、yum(安装软件包的模块)
安装软件包的模块。
参数:
name : 软件包名称
state :指定 yum 模块运行的状态
利用 yum 模块,安装部署mariadb
[root@localhost ~]# ansible all -m yum -a 'name=mariadb* state=present'
8.5、yum_repository(yum仓库模块)
yum仓库模块
参数:
baseurl : 仓库地址
description : 仓库描述
enabled : 是否启用
gpgcheck :是否验证gpg key
gpgkey :gpgkey 验证地址
name : 仓库名称
[root@localhost yum.repos.d]# ansible all -m yum_repository -a 'name=nginx-stable description="nginx stable repo" baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=yes gpgkey=https://nginx.org/keys/nginx_signing.key enabled=yes'
案例:
卸载所有版本的Nginx,使用官方仓库安装Nginx
[root@localhost yum.repos.d]# ansible all -m yum -a 'name=nginx state=absent'
[root@localhost yum.repos.d]# ansible all -m yum_repository -a 'name=nginx-stable description="nginx stable repo" baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=yes gpgkey=https://nginx.org/keys/nginx_signing.key enabled=yes'
[root@localhost yum.repos.d]# ansible all -m yum_repository -a 'name=epel baseurl=http://mirrors.aliyun.com/epel/7/$basearch description=epel enabled=no'
[root@localhost yum.repos.d]#\\192.168.13.28\linux14ansible all -m yum -a 'name=nginx state=latest'
8.6、copy(拷贝文件的模块。)
拷贝文件的模块。
参数:
src : 本地需要复制的文件的路径
dest : 复制到远程的某一个路径
owner :指定属主
group : 指定属组
mode : 设置权限的
backup : 是否备份
content : 指定文件内容
follow : 是否处理软连接
练习1:
[root@localhost ~]# ansible all -m copy -a 'src=/root/init.sh dest=/opt/'
练习2:
[root@localhost ~]# ansible all -m copy -a 'src=/root/anaconda-ks.cfg dest=/opt/ owner=root group=root mode=600 backup=yes'
练习3:
[root@localhost ~]# ansible all -m copy -a 'content="1234" dest=/opt/init.sh owner=root group=root mode=600 backup=yes'
8.7、file(创建文件)
创建文件。
参数:
path : 需要创建的文件路径
owner :指定属主
group : 指定属组
mode : 设置权限的
state :状态
link : 软连接
src : 源文件地址
dest: 软连接地址
touch : 创建文件
directory : 创建目录
absent : 删除
recurse :递归授权
练习1:用touch来创建文件2.txt
[root@localhost ~]# ansible all -m file -a 'path=/opt/2.txt owner=ftp group=ftp mode=660 state=touch'
练习2:用directory来创建目录test
[root@localhost ~]# ansible all -m file -a 'path=/opt/test owner=ftp group=ftp mode=664 state=directory'
练习3:用absent来删除目录test
[root@localhost ~]# ansible all -m file -a 'path=/opt/test owner=ftp group=ftp mode=664 state=absent'
练习4:用recurse来递归授权
[root@localhost ~]# ansible all -m file -a 'path=/opt/q owner=ftp group=ftp mode=664 state=directory recurse=yes'
8.8 get_url(下载文件)
参数:
url #文件下载地址
dest #文件存放路径
mode #文件下载后授权
checksum #验证文件
sha256 #加密方式
练习1使用 get_url(下载文件)
[root@localhost ~]# ansible all -m get_url -a 'url=http://nginx.org/download/nginx-1.18.0.tar.gz dest=/tmp mode=660'
练习2使用 checksum #验证文件
下载网站上的文件
[root@localhost ~]# ansible all -m get_url -a 'url=http://nginx.org/download/nginx-1.18.0.tar.gz dest=/tmp'
#下载时验证文件
[root@web01 ~]# md5sum /tmp/nginx-1.18.0.tar.gz
[root@localhost ~]# ansible all -m get_url -a 'url=http://nginx.org/download/nginx-1.18.0.tar.gz dest=/tmp mode=660 checksum=md5:b2d33d24d89b8b1f87ff5d251aa27eb8'
8.9、service(用来管理系统服务)
用来管理系统服务。
参数:
name : 指定需要操作服务的名称
state:
started #启动服务
stopped #停止服务
restarted #重启服务
reloaded #重载服务
enabled :开机自启动
练习1使用 started #启动服务
[root@localhost ~]# ansible all -m service -a 'name=nginx state=started'
练习2使用stopped #停止服务
[root@localhost ~]# ansible all -m service -a 'name=nginx state=stopped'
8.9.1、systemd
用来管理系统服务。但是systemd底层需要systemd服务来支撑,没有systemd的操作系统无法使用。
8.10、group(用来管理用户组)
用来管理用户组
参数:
name: somegroup #组名字
state:
present #创建组
absent #删除组
gid: 666 #指定组id
练习1使用group(用来管理用户组)
[root@localhost ~]# ansible all -m group -a 'name=test state=present gid=666'
8.11、user(用来管理用户)
用来管理用户
参数:
name #用户名字
comment #用户备注
uid #用户id
group #用户所在的组名字
shell
/bin/bash #用户可以登录
/sbin/nologin #用户不需要登录
state
absent #删除用户
present #创建用户
remove #移除家目录
create_home #是否创建家目录
true #创建家目录
false #不创建家目录
练习使用user创建用户和创建家目录
[root@localhost ~]# ansible all -m user -a 'name=test comment="备注" uid=666 shell=/sbin/nologin state=present create_home=true'
8.12、cron(定时任务管理模块)
定时任务管理模块。
参数:
name #定时任务的备注
minute #分钟
hour #小时
day #日
month #月
weekday #周
job #指定的定时任务内容
state
present #新建定时任务
absent #删除定时任务
disabled
yes #注释定时任务
no #取消注释
练习使用cron管理定时任务
[root@localhost ~]# ansible all -m cron -a 'name=test minute="*" hour="*" day="*" month="*" weekday="*" job="ls -l /root > /opt/1.txt"'
[root@localhost ~]# crontab -l
练习使用cron删除定时任务
[root@localhost ~]# ansible all -m cron -a 'name=test minute="*" hour="*" day="*" month="*" weekday="*" job="ls -l /root > /opt/1.txt" state=absent'
8.13、mount(挂载文件系统)
挂载文件系统。
参数:
path #本机准备挂载的目录
src #远端挂载点
fstype #指定挂载类型
opts #挂载参数(/etc/fstab中的内容)
state
present #配置开机挂载,将配置写入自动挂载文件,并没有直接挂载
unmounted #取消挂载,但是没有删除自动挂载配置
#常用配置
mounted #配置开机挂载,并且直接挂载上
absent #取消挂载,并且删除自动挂载配置
练习使用
[root@web01 ~]# vim /etc/ansible/hosts
[web01]
192.168.15.100 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass='1'
[web02]
192.168.15.7 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass='1'
[root@web01 ~]# ansible web01 -m mount -a 'path=/opt src=192.168.15.7:/backup fstype=nfs opts=default state=present'
开机自动挂载
[root@web01 ~]# ansible web01 -m mount -a 'path=/opt src=192.168.15.7:/backup fstype=nfs opts=defaults state=mounted'
取消挂载,并且删除自动挂载配置
[root@web01 ~]# ansible web01 -m mount -a 'path=/opt src=192.168.15.7:/backup fstype=nfs opts=defaults state=absent'
下载 nts-utils
[root@localhost ~]# yum install nts-utils -y
创建挂载目录
[root@web01 ~]# mkdir /backup
查看目录
[root@web01 ~]# cat /etc/passwd
[root@web01 ~]# chown nfsnobody.nfsnobody /backup/
[root@web01 ~]# vim /etc/exports
/backup 192.168.15.0/24(rw,sync,all_squash)
~
[root@web01 ~]# systemctl restart nfs-server
[root@web01 ~]# showmount -e
[root@localhost ~]# mount -t nfs 192.168.15.100:/backup /opt
卸掉挂载
[root@localhost ~]# umount /mnt
8.14、selinux(用来管理系统selinux)
用来管理系统selinux
参数:
state: disabled
练习使用selinux(用来管理系统selinux)
[root@localhost ~]# ansible web01 -m selinux -a 'state=disabled'
8.15、firewalld(操作firewalld防火墙模块)
firewalld永久关闭防火墙操作firewalld防火墙模块。
参数:
service:指定服务
permanent:是否永久生效
state:
enabled :允许通过
port:指定端口的
rich_rule:附加规则
source:指定网段
interface:指定网卡
masquerade:是否开启IP伪装
练习1使用firewalld永久关闭防火墙
[root@localhost ~]# ansible web01 -m firewalld -a 'service=http permanent=yes state=enabled'
8.16、archive(压缩模块)
压缩模块
参数:
path: /path/to/foo #要打包的内容
dest: /path/to/foo.tgz #打好的包与存放位置
format:gz
8.17、unarchive(解压模块)
解压模块
参数:
src #包的路径
dest #解压后的目标路径
remote_src
yes #包在受控端服务器上
no #包在控制端服务器上
8.18、setup(获取控制端信息的模块)
获取控制端信息的模块
习题
准备工作:
1.先把软件下载到虚拟机上
2.再把软件解压
[root@localhost ~]# unzip mario.zip
3.cd到html5-mario/
[root@localhost ~]# cd html5-mario/
4.在打包
[root@localhost html5-mario]# tar -czvf mario.tar.gz ./
5.移动到上层目录
[root@localhost html5-mario]# mv mario.tar.gz ..
6.cd到根目录
1、上传代码
[root@localhost ~]# ansible web01 -m unarchive -a 'src=/root/mario.tar.gz dest=/usr/share/nginx/html remote_src=no'
2、安装nginx
[root@web01 ~]# ansible web01 -m yum -a 'name=nginx state=present'
3、启动Nginx
[root@web01 ~]# ansible web01 -m service -a "name=nginx state=started"