文章目录
磁盘管理模块
parted、lvg、lvol、filesystem模块管理磁盘及文件系统
1.parted
示例:
1、事先添加一块10G新磁盘
[root@master ~]# lsblk | grep nvme0n2
nvme0n2 259:3 0 10G 0 disk
2、创建主分区
[root@master ~]# ansible localhost -m parted -a 'device=/dev/nvme0n2 number=1 part_end=1GiB state=present'
[root@master ~]# ansible localhost -m parted -a 'device=/dev/nvme0n2 number=2 flags=lvm part_start=1GiB part_end=2GiB state=present'
3、移除1号分区
[root@master ~]# ansible localhost -m parted -a 'device=/dev/nvme0n2 number=2 state=absent'
2.lvg
示例
1、给以上分区创建卷组
[root@master ~]# ansible localhost -m lvg -a 'pvs=/dev/nvme0n2p1 vg=vg1'
localhost | CHANGED => { "changed": true }
[root@master ~]# vgs vg1
VG #PV #LV #SN Attr VSize VFree
vg1 1 0 0 wz--n- 1020.00m 1020.00m
2、在/dev/nvme0n2p1上创建一个具有物理扩展区大小的卷组16M
[root@master ~]# ansible localhost -m parted -a 'device=/dev/nvme0n2 number=2 flags=lvm part_start=1GiB part_end=2GiB state=present' [root@master ~]# ansible localhost -m lvg -a 'pvs=/dev/nvme0n2p2 vg=vg2 pesize=16'
localhost | CHANGED => { "changed": true }
3、在/dev/nvme0n2p3和/dev/nvme0n2p4上创建卷组或调整卷组大小先创建两个分区
[root@master ~]# ansible localhost -m parted -a 'device=/dev/nvme0n2 number=3 flags=lvm part_start=2GiB part_end=3GiB state=present'
[root@master ~]# ansible localhost -m parted -a 'device=/dev/nvme0n2 number=4 flags=lvm part_start=3GiB part_end=4GiB state=present' 重新创建vg
[root@master ~]# ansible localhost -m lvg -a 'pvs=/dev/nvme0n2p3,/dev/nvme0n2p4 vg=vg1'
[root@master ~]# vgs vg1
VG #PV #LV #SN Attr VSize VFree vg1 2 0 0 wz--n- 1.99g 1.99g
3.lvol
示例
1、创建逻辑卷512M
[root@master ~]# ansible localhost -m lvol -a 'vg=vg1 lv=lv1 size=512'
[root@master ~]# lvs /dev/vg1/lv1 LV VG
Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lv1 vg1 -wi-a----- 512.00m
2、对/dev/nvme0n2p3创建512M的逻辑卷
[root@master ~]# ansible localhost -m lvol -a 'vg=vg1 lv=lv2 size=512 pvs=/dev/nvme0n2p3'
localhost | CHANGED => { "changed": true, "msg": "" }
[root@master ~]# lvs /dev/vg1/lv2
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lv2 vg1 -wi-a----- 512.00m
3、创建一个逻辑卷,其大小等于卷组中所有剩余空间的大小 -l 100%VG
[root@master ~]# ansible localhost -m lvol -a 'vg=vg2 lv=lv3 size=100%FREE'
localhost | CHANGED => { "changed": true, "msg": "" }
[root@master ~]# lvs /dev/vg2/lv3
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lv3 vg2 -wi-a----- 1008.00m
4、将逻辑卷扩展到1024m
[root@master ~]# ansible localhost -m lvol -a 'vg=vg1 lv=lv1 size=1024'
[root@master ~]# lvs /dev/vg1/lv1
[root@master ~]# ansible localhost -m lvol -a 'vg=vg2 lv=lv3 state=absent force=yes'
4.fillesystem
选项
- dev:目标块设备
- force:在一个已有文件系统的设备上强制创建
- fstype:文件系统的类型
- opts:传递给mkfs命令的选项
示例
1、创建ext4文件系统 /dev/sdb1,删除vg2
[root@master ~]# ansible localhost -m lvg -a 'vg=vg2 state=absent'
[root@master ~]# ansible localhost -m filesystem -a 'dev=/dev/nvme0n2p2 fstype=ext4 force=yes'
2、对逻辑卷lv1创建xfs文件系统
[root@master ~]# ansible localhost -m filesystem -a 'dev=/dev/vg1/lv1 fstype=xfs force=yes'
mount模块
配置挂载点
选项:
-
fstype:必选项,挂载文件的类型
-
path:必选项,挂载点
-
opts:传递给mount命令的参数
-
src:必选项,要挂载的文件系统
-
state:必选项present:只处理fstab中的配置absent
挂载点mounted
unmounted:卸载
name=/mnt/dvd src=/dev/sr0 fstype=iso9660 opts=ro state=present
name=/srv/disk src='LABEL=SOME_LABEL' state=present
name=/home src='UUID=b3e48f45-f933-4c8e-a700-22a159ec9077' opts=noatime state=present
示例
1、挂载光盘到/mnt/dvd
[root@master ~]# ansible localhost -m file -a 'path=/mnt/dvd state=directory'
[root@master ~]# ansible localhost -m mount -a 'src=/dev/sr0 path=/mnt/dev fstype=iso9660 state=present'
[root@master ~]# tail -1 /etc/fstab
/dev/sr0 /mnt/dev iso9660 defaults 0 0
2、挂载/dev/vg1/lv1到/tools
[root@master ~]# ansible localhost -m file -a 'path=/tools state=directory'
[root@master ~]# ansible localhost -m mount -a 'src=/dev/vg1/lv1 path=/tools fstype=xfs state=mounted'
[root@master ~]# df -h | grep /tools /dev/mapper/vg1-lv1
507M 30M 478M 6% /tools
[root@master ~]# tail -1 /etc/fstab
/dev/vg1/lv1 /tools xfs defaults 0 0
3、使用UUID挂载
[root@master ~]# ansible localhost -m file -a 'path=/data state=directory' [root@master ~]# ansible localhost -m shell -a 'blkid /dev/nvme0n2p2' [root@master ~]# ansible localhost -m mount -a 'src=UUID=ee73dd09-6d24-4d01-b2ba-00da1305229a path=/data fstype=ext4 state=mounted'
[root@master ~]# tail -1 /etc/fstab
UUID=ee73dd09-6d24-4d01-b2ba-00da1305229a /data ext4 defaults 0 0
[root@master ~]# df -h | grep /data
/dev/nvme0n2p2 976M 2.6M 907M 1% /data
任务成功。但执行显示failed
[root@server ~]# ansible disk -m mount -a 'name=/mnt state=absent'
node2 | FAILED! => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"msg": "Error rmdir /mnt: [Errno 39] Directory not empty: '/mnt'"
}
node1 | FAILED! => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"msg": "Error rmdir /mnt: [Errno 39] Directory not empty: '/mnt'"
}