利用ansible批量分区、格式化、挂载
时间: 2023-09-16 10:14:03 浏览: 96
可以使用Ansible的`disk_partition`和`filesystem`模块来批量分区、格式化和挂载磁盘。下面是一个示例playbook,可以将其用作参考:
```yaml
- hosts: all
become: true
vars:
partitions:
- name: /dev/sdb1
number: 1
size: 10GB
- name: /dev/sdc1
number: 1
size: 20GB
filesystems:
- name: /dev/sdb1
fstype: ext4
opts: defaults
- name: /dev/sdc1
fstype: ext4
opts: defaults
mountpoints:
- path: /mnt/disk1
device: /dev/sdb1
fstype: ext4
opts: defaults
- path: /mnt/disk2
device: /dev/sdc1
fstype: ext4
opts: defaults
tasks:
- name: Partition disks
disk_partition:
device: "{{ item.name }}"
number: "{{ item.number }}"
size: "{{ item.size }}"
state: present
with_items: "{{ partitions }}"
- name: Format disks
filesystem:
fstype: "{{ item.fstype }}"
dev: "{{ item.name }}"
opts: "{{ item.opts }}"
state: present
with_items: "{{ filesystems }}"
- name: Mount filesystems
mount:
path: "{{ item.path }}"
src: "{{ item.device }}"
fstype: "{{ item.fstype }}"
opts: "{{ item.opts }}"
state: mounted
with_items: "{{ mountpoints }}"
```
这个playbook的作用是对目标主机上的磁盘执行以下操作:
- 分区为指定大小的分区
- 格式化为指定文件系统类型的分区
- 挂载在指定的挂载点上
你需要将`partitions`、`filesystems`和`mountpoints`变量替换为你自己的值,以适应你的环境。然后使用`ansible-playbook`命令执行该playbook即可。
阅读全文