1.创建一个分区
编写一个剧本位置在~/ansible/partition.yml,在所有主机上运行,需求如下:
在vdb中,创建一个主分区,编号是1,大小1500MiB
格式化成ext4文件系统,挂在到/newpart
如果无法创建请求的分区大小,则应使用错误消息"Could not create partition of that size", 应该显示,而应该使用大小为800Mib的
如果设备vdb不存在,则错误消息"Disk does not exist"应该显示
[student@workstation ansible]$ cat partition.yml
---
- name: Create partition
hosts: all
tasks:
- name: check vdb
shell: lsblk | grep -w vdb
register: res
ignore_errors: yes
- name: if vdb not exist
debug:
msg: "Disk does not exist"
when: res is failed
failed_when: res is failed
- name: Create
block:
- name: create 1500
parted:
device: /dev/vdb
number: 1
state: present
part_end: 1500MiB
rescue:
- name: dis msg
debug:
msg: "Could not create partition of that size"
- name: create 800
parted:
device: /dev/vdb
number: 1
state: present
part_end: 800MiB
always:
- name: ext4 filesystem
filesystem:
fstype: ext4
dev: /dev/vdb1
- name: create dir if it not exist
file:
path: /newpart
state: directory
- name: mount
mount:
path: /newpart
src: /dev/vdb1
fstype: ext4
state: mounted
when: res.rc == 0
[student@workstation ansible]$ ansible all -m shell -a 'df -h | grep newpart'
检测结果:
2.创建并应用一个逻辑卷
在~/ansible/创建一个lv.yml文件,让它在所有主机上跑,要求如下:
创建一个逻辑卷data大小1500MiB,从research卷组中来
使用ext4格式化这个逻辑卷
实现开机自动挂载到/data目录下
如果这个逻辑卷的大小不能够创建,则输出错误信息
Could not create logical volume of that size
并使用800MiB大小来创建逻辑卷
如果research这个卷组不存在,则输出错误消息:Vg does not exists
[student@workstation ansible]$ cat lv.yml
---
- name: Create lv
hosts: all
tasks:
- name: check research
shell: vgs research
register: res
ignore_errors: yes
- name: if research not exist
debug:
msg: "Vg does not exists"
when: res is failed
failed_when: res is failed
- name: Create
block:
- name: create 1500
lvol:
vg: research
lv: data
size: 1500
rescue:
- name: dis msg
debug:
msg: "Could not create logical volume of that size"
- name: create 800
lvol:
vg: research
lv: data
size: 800
always:
- name: ext4 filesystem
filesystem:
fstype: ext4
dev: /dev/research/data
- name: create dir if it not exist
file:
path: /data
state: directory
- name: mount
mount:
path: /data
src: /dev/research/data
fstype: ext4
state: mounted
when: res.rc == 0
因为不存在research卷组,所以执行失败
接下来只在servera主机上创建research卷组
[root@servera ~]# pvcreate /dev/vdb1
[root@servera ~]# vgcreate research /dev/vdb1
再次测试,servera主机成功创建逻辑卷
因为<1500M,所以创建了800M