Linux软件管理相关模块
yum_repository模块
用于管理被控节点YUM源配置文件(repo文件)
## 常用选项
file:指定repo文件名,如果不写则以name字段为标准
name:仓库唯一标识符
description:仓库描述信息
baseurl:仓库地址
enabled:是否启用该仓库
gpgcheck:是否校验秘钥
state:文件状态
## 样例
# 添加BaseOS源
[root@pubserver ~]# cd /root/ansible/
[root@pubserver ansible]# ansible webservers -m yum_repository -a "file=myrepo name='BaseOS' description='Rocky Linux BaseOS' baseurl='ftp://192.168.88.240/dvd/BaseOS/' gpgcheck=false enabled=true"
[root@pubserver ansible]# ansible webservers -a "cat /etc/yum.repos.d/myrepo.repo"
# 添加AppStream源,添加到同一个文件中,会追加到文件的最后
[root@pubserver ansible]# ansible webservers -m yum_repository -a "file=myrepo name='AppStream' description='Rocky Linux AppStream' baseurl='ftp://192.168.88.240/dvd/AppStream/' gpgcheck=false enabled=true"
[root@pubserver ansible]# ansible webservers -a "cat /etc/yum.repos.d/myrepo.repo"
# 清理指定文件中的源(如果清理指定name的源文件内还有其他源则文件保留,如果无其他源则同时删除文件)
[root@pubserver ansible]# ansible webservers -m yum_repository -a "file=myrepo name=BaseOS state=absent"
[root@pubserver ansible]# ansible webservers -m yum_repository -a "file=myrepo name=AppStream state=absent"
yum模块
用于管理被控节点的rpm包,如安装、升级、卸载等
## 常用参数
name:包名
state:状态
present表示安装,如果已安装则忽略
latest表示安装或升级到最新版本
absent表示卸载
## 样例
# 使用yum模块安装单个包,如已安装则忽略
[root@pubserver ansible]# ansible webservers -m yum -a "name=tar state=present"
# 使用yum模块安装多个包,包名之间使用,分隔
[root@pubserver ansible]# ansible webservers -m yum -a "name=wget,net-tools state=present"
# 使用yum模块卸载单个包
[root@pubserver ansible]# ansible webservers -m yum -a "name=wget state=absent"
Linux服务管理相关模块
service模块
用于管理Linux系统服务,如启动、关闭、重启、是否开机自启动
## 常用参数
name:控制的服务名
state:started表示启动;stopped表示关闭;restarted表示重启
enabled:yes表示设置开机自启;no表示设置开机不要自启
## 样例
# 使用yum模块安装nginx服务
[root@pubserver ansible]# ansible webservers -m yum -a "name=nginx state=latest"
# 使用service模块管理nginx服务,启动nginx,加入开机自启
[root@pubserver ansible]# ansible webservers -m service -a "name=nginx state=started enabled=true"
# 验证被控节点nginx服务是否启动且设置为开机自启动
[root@pubserver ansible]# ansible webservers -a "systemctl is-active nginx"
[root@pubserver ansible]# ansible webservers -a "systemctl is-enabled nginx"
Linux磁盘/逻辑卷/文件系统管理相关模块
parted模块
用于管理被控磁盘分区
## 常用参数
device:待分区的设备
number:分区编号
state:present表示创建,absent表示删除
part_start:分区的起始位置,不写表示从开头
part_end:表示分区的结束位置,不写表示到结尾
label:表示标签,要使用的磁盘标签类型,分区表
## 样例
# 将web1虚拟机关机,添加两块20G的虚拟磁盘(该步骤自行操作)
[root@pubserver ansible]# ansible web1 -a "lsblk"
web1 | CHANGED | rc=0 >>
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 253:0 0 20G 0 disk
└─vda1 253:1 0 20G 0 part /
vdb 253:16 0 20G 0 disk
vdc 253:32 0 20G 0 disk
# 磁盘分区知识点回顾
常用的分区表类型有:MBR(主引导记录)、GPT(GUID分区表)
MBR最多支持4个主分区,或3个主分区加1个扩展分区。最大支持2.2TB左右的硬盘
GPT最多支持128个主分区。支持大硬盘
# 使用parted模块对/dev/vdb磁盘进行分区操作
[root@pubserver ansible]# ansible web1 -m parted -a "device=/dev/vdb number=1 part_end=5GiB label=gpt state=present" #不指定part_start则从0%开始
[root@pubserver ansible]# ansible web1 -m parted -a "device=/dev/vdb number=2 part_start=5GiB label=gpt state=present" #不指定part_end则到100%为止
[root@pubserver ansible]# ansible web1 -a "lsblk"
web1 | CHANGED | rc=0 >>
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 253:0 0 20G 0 disk
└─vda1 253:1 0 20G 0 part /
vdb 253:16 0 20G 0 disk
├─vdb1 253:17 0 5G 0 part
└─vdb2 253:18 0 15G 0 part
vdc 253:32 0 20G 0 disk
lvg模块
用于管理卷组,包括创建、删除等
## 常用参数
vg:定义卷组名。vg:volume group
pvs:由哪些物理卷构成。pvs:physical volumes
pesize:指定物理区块大小,默认4M。pe:physical extent
state:present创建卷组;absent删除卷组
## 样例
# 使用yum模块安装lvm2相关软件包
[root@pubserver ansible]# ansible web1 -m yum -a "name=lvm2 state=present"
# 使用lvg模块创建myvg卷组,卷组由/dev/vdb1组成
[root@pubserver ansible]# ansible web1 -m lvg -a "vg=myvg pvs=/dev/vdb1"
[root@pubserver ansible]# ansible web1 -a "vgs"
web1 | CHANGED | rc=0 >>
VG #PV #LV #SN Attr VSize VFree
myvg 1 0 0 wz--n- <5.00g <5.00g
# 使用lvg模块扩容myvg卷组,卷组由/dev/vdb1和/dev/vdb2组成(扩容时需要指定所有物理卷)
[root@pubserver ansible]# ansible web1 -m lvg -a "vg=myvg pvs=/dev/vdb1,/dev/vdb2"
[root@pubserver ansible]# ansible web1 -a "vgs"
web1 | CHANGED | rc=0 >>
VG #PV #LV #SN Attr VSize VFree
myvg 2 0 0 wz--n- 19.99g 19.99g
lvol模块
用于管理逻辑卷,包括创建、删除、修改逻辑卷大小
## 常用参数
vg:指定在哪个卷组上创建逻辑卷
lv:创建的逻辑卷名。lv:logical volume
size:逻辑卷的大小,不写单位,以M为单位
state:present创建逻辑卷;absent删除逻辑卷
## 样例
# 使用lvol模块创建mylv逻辑卷,vg=myvg在哪个卷组创建,lv=mylv指定逻辑卷的名字,size=2G逻辑卷的大小
[root@pubserver ansible]# ansible web1 -m lvol -a "vg=myvg lv=mylv size=2G"
[root@pubserver ansible]# ansible web1 -a "lvs"
web1 | CHANGED | rc=0 >>
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
mylv myvg -wi-a----- 2.00g
# 使用lvol模块扩容mylv逻辑卷,扩容直接更改大小即可,如size=4G
[root@pubserver ansible]# ansible web1 -m lvol -a "vg=myvg lv=mylv size=4G"
[root@pubserver ansible]# ansible web1 -a "lvs"
web1 | CHANGED | rc=0 >>
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
mylv myvg -wi-a----- 4.00g
filesystem模块
用于格式化分区,即创建文件系统
## 常用参数
fstype:指定文件系统类型
dev:指定要格式化的设备,可以是分区,可以是逻辑卷
state:present用于创建文件系统;absent用于擦除文件系统
force:用于强制操作
## 样例
# 使用filesystem模块格式化mylv逻辑卷,文件系统类型为ext4
[root@pubserver ansible]# ansible web1 -m filesystem -a "dev=/dev/myvg/mylv fstype=ext4 state=present"
#查看结果
[root@pubserver ansible]# ansible web1 -a "blkid /dev/myvg/mylv"
web1 | CHANGED | rc=0 >>
/dev/myvg/mylv: UUID="3d2730be-1eaf-4ec1-9340-03f86deba8fc" BLOCK_SIZE="4096" TYPE="ext4"
# 使用filesystem模块强制格式化mylv逻辑卷,文件系统类型调整为xfs
[root@pubserver ansible]# ansible web1 -m filesystem -a "dev=/dev/myvg/mylv fstype=xfs force=yes"
#查看结果
[root@pubserver ansible]# ansible web1 -a "blkid /dev/myvg/mylv"
web1 | CHANGED | rc=0 >>
/dev/myvg/mylv: UUID="8d32db4d-3412-498e-94a9-82df5e0b9e50" BLOCK_SIZE="512" TYPE="xfs"
mount模块
用于挂载指定分区
## 常用参数
path:挂载点。如果挂载点不存在,自动创建。
src:待挂载的设备
fstype:文件系统类型
state:mounted表示永久挂载;unmounted表示临时卸载;present表示临时挂载;absent表示永久卸载
## 样例
# 使用mount模块实现mylv逻辑卷永久挂载
[root@pubserver ansible]# ansible web1 -m mount -a "src=/dev/myvg/mylv path=/data fstype=xfs state=mounted"
[root@pubserver ansible]# ansible web1 -a "df -hT | grep data" #查看结果
web1 | CHANGED | rc=0 >>
/dev/mapper/myvg-mylv xfs 4.0G 61M 4.0G 2% /data
[root@pubserver ansible]# ansible web1 -a "tail -1 /etc/fstab" #已经写入fstab文件
web1 | CHANGED | rc=0 >>
/dev/myvg/mylv /data xfs defaults 0 0
# 使用mount模块实现mylv逻辑卷永久卸载
[root@pubserver ansible]# ansible web1 -m mount -a "path=/data state=absent"
[root@pubserver ansible]# ansible web1 -a "ls /data" #挂载点已经直接删除,查看失败
[root@pubserver ansible]# ansible web1 -a "cat /etc/fstab" #已经清理fstab文件,挂载的mylv已经清理
# 使用lvol模块删除mylv逻辑卷,force=yes:这个参数用于强制执行,如在删除逻辑卷时,即使它处于活动状态也会被强制删除
[root@pubserver ansible]# ansible web1 -m lvol -a "vg=myvg lv=mylv state=absent force=yes"
# 使用lvg模块删除myvg卷组
[root@pubserver ansible]# ansible web1 -m lvg -a "vg=myvg state=absent"
[root@pubserver ansible]# ansible web1 -a "lvs" #没有内容显示
[root@pubserver ansible]# ansible web1 -a "vgs" #没有内容显示