在一个节点添加一块20G的硬盘
通过ansible剧本判断是否存在第二块硬盘,且硬盘的大小大于10G
满足条件:
在此硬盘创建一个分区,大小为10G
使用此分区创建一个卷组
从此卷组中创建一个逻辑卷
将此逻辑卷格式化为xfs
将此逻辑卷挂载至/mountdir目录(目录自己用模块创建)
挂载后向目录中写入任意字符串
使用shell模块查看文件内容
卸载逻辑卷
有缘者观之。
node1 添加一块硬盘,其名为nvme0n2,大小20G
[root@ansible ~]# cat blk.yml
---
- name:
hosts: node1
tasks:
- name: check blk #检查磁盘大小,小于10G就打印错误信息
debug:
msg: "the hard drive is too small"
when: ansible_devices.nvme0n2 is defined and ansible_devices.nvme0n2.size < "10G"
- name: stop playbook #磁盘小于10G,停止剧本执行
meta: end_play
when: ansible_devices.nvme0n2 is defined and ansible_devices.nvme0n2.size < "10G"
- name: part the nvme0n2 #磁盘分区
parted:
device: /dev/nvme0n2
number: 1
flags:
- lvm
state: present
part_end: 10GiB
- name: create vg #创建卷组
lvg:
vg: myvg
pvs:
- /dev/nvme0n2p1
state: present
- name: install lvm2 #创建逻辑卷需要安装lvm2
yum:
name: lvm2
state: present
- name: create lv #创建逻辑卷
lvol:
vg: myvg
lv: mylv
size: 100m
- name: make fs to xfs #格式化逻辑卷
filesystem:
fstype: xfs
dev: /dev/myvg/mylv
- name: create /mountdir #创建挂载点
file:
path: /mountdir
state: directory
- name: mount mylv on /mountdir #挂载逻辑卷
mount:
path: /mountdir
src: /dev/myvg/mylv
fstype: xfs
opts: rw
state: mounted
- name: create /mountdir/test.txt #创建文件
file:
path: /mountdir/test.txt
state: touch
- name: write test to /mountdir/test.txt #向文件中写入内容
copy:
content: "test!!!!"
dest: /mountdir/test.txt
- name: check content of /mountdir/test.txt #打印文件内容,结果存入result
shell: "cat /mountdir/test.txt"
register: result
- name: print the content of /mountdir/test.txt #打印上一步的结果
debug:
var: result.stdout
- name: unmount /dev/myvg/mylv #卸载逻辑卷
mount:
path: /mountdir
state: unmounted
[root@ansible ~]#