0%

RHCE (EX294) - 创建和使用分区(NEW)

创建和使用分区

创建一个名为 /home/greg/ansible/partition.yml 的 playbook ,它将在 所有受管节点 上创建分区:

vdb 创建一个 1500M 主分区,分区号 1,并格式化 ext4

prod 组将分区永久挂载到 /data

如果磁盘空间不够,

给出提示信息 Could not create partition of that size

创建 800MiB 分区

如果 vdb不存在,则给出提示信息 this disk is not exist

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
[greg@control ansible]$ vim /home/greg/ansible/partition.yml
---
- name: 创建和使用分区
hosts: all
tasks:
- block:
- name: Create a new primary partition with a size of 1500MiB
parted:
device: /dev/vdb
number: 1
state: present
part_end: 1500MiB
- name: Create a ext4 filesystem on /dev/vdb1
filesystem:
fstype: ext4
dev: /dev/vdb1
- name: Mount /data
mount:
path: /data
src: /dev/vdb1
fstype: ext4
state: mounted
when: inventory_hostname in groups.prod
rescue:
- debug:
msg: Could not create partition of that size
- name: Create a new primary partition with a size of 800MiB
parted:
device: /dev/vdb
number: 1
state: present
part_end: 800MiB
when: ansible_facts['devices']['vdb'] is defined
- debug:
msg: this disk is not exist
when: ansible_facts['devices']['vdb'] is undefined
[greg@control ansible]$ ansible-playbook partition.yml

使用魔法变量

最常用的有四个:

hostvars

包含受管主机的变量,可以用于获取另一台受管主机的变量的值。

group_names

列出当前受管主机所属的所有组。

groups

列出清单中的所有组和主机。

inventory_hostname

包含清单中配置的当前受管主机的主机名称。

ansible localhost -m debug -a 'var=hostvars["localhost"]'