1.磁盘分区并格式化挂载的实现
(1)首先给目标主机server2添加一块新的20G的虚拟硬盘/dev/vdc
(2)查看分区的帮助文档;编辑yml文件
- 磁盘分区(parted模块)
---
- hosts: web
tasks:
- name: Create a new primary partition with a size of 1GiB ##磁盘分区
parted:
device: /dev/vdc
number: 1
state: present
part_end: 1GiB
- 磁盘格式化(filesystem模块)
- name: Create a ext4 filesystem on /dev/sda1
filesystem:
fstype: ext4
dev: /dev/vdc1
- 文件系统的挂载(mount模块)
- name: Mount up device
mount:
path: /media
src: /dev/vdc
fstype: ext4
(3)执行playbook
- 目标主机查看
[root@server2 ~]# fdisk -l
2.判断磁盘设备是否存在,存在时创建分区,不存在时报错(debug块的应用)
block:正常的时候执行
rescure: 不正常的时候执行
always:无论如何都会执行
- 编写yml 文件
[devops@server1 ansible]$ cat part.yml
---
- hosts: web
tasks:
- shell: test -b /dev/sda ##shell模块判断磁盘设备是否存在
register: result
ignore_errors: True
- debug:
msg: "/dev/sda not exists" ##不存在报错
when: result.rc != 0
- name: create partations
block:
- name: Create a new primary partition with a size of 1GiB
parted:
device: /dev/sda
number: 1
state: present
part_end: 1GiB
- name: Create a ext4 filesystem on /dev/sda1
filesystem:
fstype: ext4
dev: /dev/sda1
- name: Mount up device
mount:
path: /media
src: /dev/sda1
fstype: ext4
opts: noatime
state: mounted
when: result.rc == 0 ##存在创建
- 执行playbook