community.general.parted 模块 – 配置块设备分区

注意

此模块是 community.general 集合 (版本 10.1.0) 的一部分。

如果您使用的是 ansible 包,则可能已经安装了此集合。它不包含在 ansible-core 中。要检查它是否已安装,请运行 ansible-galaxy collection list

要安装它,请使用:ansible-galaxy collection install community.general。您需要其他要求才能使用此模块,有关详细信息,请参阅 要求

要在剧本中使用它,请指定:community.general.parted

概要

  • 此模块允许使用 parted 命令行工具配置块设备分区。有关字段和选项的完整说明,请查看 GNU parted 手册。

要求

执行此模块的主机需要以下要求。

  • 此模块需要 parted 1.8.3 或更高版本。

  • 选项 alignundefined 除外)需要 parted 2.1 或更高版本。

  • 如果 parted 版本低于 3.1,则需要运行 sysfs 文件系统 /sys/ 的 Linux 版本。

  • 使用 resize 参数时,需要 resizepart 命令。

参数

参数

注释

align

字符串

设置新创建分区的对齐方式。使用 undefined 表示 parted 默认对齐方式。

选项

  • "cylinder"

  • "minimal"

  • "none"

  • "optimal" ← (默认)

  • "undefined"

device

字符串 / 必填

要操作的块设备(磁盘)。

也可以对常规文件进行分区,但建议使用 losetup 创建回环设备以便轻松访问其分区。

flags

列表 / 元素=字符串

必须在分区上设置的标志列表。

fs_type

字符串

在 community.general 0.2.0 中添加

如果指定且分区不存在,则会将文件系统类型设置为给定分区。

参数可选,但请参阅下面关于负 part_start 值的说明。

label

字符串

要使用的磁盘标签类型或分区表。

如果 device 已包含不同的标签,它将更改为 label,任何以前的分区都将丢失。

对于 gpt 分区表,必须指定 name

选项

  • "aix"

  • "amiga"

  • "bsd"

  • "dvh"

  • "gpt"

  • "loop"

  • "mac"

  • "msdos" ← (默认)

  • "pc98"

  • "sun"

name

字符串

设置分区编号的名称(仅限 GPT、Mac、MIPS 和 PC98)。

number

整数

受影响的分区号。

在对磁盘执行任何操作时(获取信息除外)都需要。

part_end

字符串

分区结束位置相对于磁盘起始位置的偏移量,也就是距离磁盘起始位置的“距离”。负数表示距离磁盘末尾的距离。

距离可以使用 parted 支持的所有单位(compat 除外),并且区分大小写,例如 10GiB15%

默认值: "100%"

part_start

字符串

分区起始位置相对于磁盘起始位置的偏移量,也就是距离磁盘起始位置的“距离”。负数表示距离磁盘末尾的距离。

距离可以使用 parted 支持的所有单位(compat 除外),并且区分大小写,例如 10GiB15%

使用负值可能需要设置 fs_type(参见注释)。

默认值: "0%"

part_type

字符串

只能与 label=msdoslabel=dvh 一起使用。

part_typename 都不能与 label=sun 一起使用。

选项

  • "extended"

  • "logical"

  • "primary" ← (默认)

resize

布尔值

community.general 1.3.0 版本中添加

对现有分区调用 resizepart 以匹配由 part_end 指定的大小。

选项

  • false ← (默认)

  • true

state

字符串

创建或删除分区。

如果设置为 info,模块将只返回设备信息。

选项

  • "absent"

  • "present"

  • "info" ← (默认)

unit

字符串

选择 Parted 用于显示磁盘位置和容量以及解释用户提供的(如果没有单位后缀)位置和容量的当前默认单位。

获取有关磁盘的信息时,建议始终指定单位。

选项

  • "s"

  • "B"

  • "KB"

  • "KiB" ← (默认)

  • "MB"

  • "MiB"

  • "GB"

  • "GiB"

  • "TB"

  • "TiB"

  • "%"

  • "cyl"

  • "chs"

  • "compact"

属性

属性

支持

描述

check_mode

支持:完全支持

可以在 check_mode 下运行,并在不修改目标的情况下返回更改状态预测。

diff_mode

支持:不支持

处于差异模式时,将返回有关已更改内容(或可能需要在 check_mode 中更改的内容)的详细信息。

注释

注意

  • 获取有关新磁盘的信息以及系统上安装的 parted 版本早于 3.1 版本时,模块将通过 /sys/ 查询内核以获取磁盘信息。在这种情况下,不支持 CHS 和 CYL 单位。

  • 如果未给出 fs_type,则会拒绝负 part_start 起始值。此错误已在 parted 3.2.153 中修复。如果要使用负 part_start,也请指定 fs_type 或确保您的系统包含较新的 parted。

示例

- name: Create a new ext4 primary partition
  community.general.parted:
    device: /dev/sdb
    number: 1
    state: present
    fs_type: ext4

- name: Remove partition number 1
  community.general.parted:
    device: /dev/sdb
    number: 1
    state: absent

- name: Create a new primary partition with a size of 1GiB
  community.general.parted:
    device: /dev/sdb
    number: 1
    state: present
    part_end: 1GiB

- name: Create a new primary partition for LVM
  community.general.parted:
    device: /dev/sdb
    number: 2
    flags: [ lvm ]
    state: present
    part_start: 1GiB

- name: Create a new primary partition with a size of 1GiB at disk's end
  community.general.parted:
    device: /dev/sdb
    number: 3
    state: present
    fs_type: ext3
    part_start: -1GiB

# Example on how to read info and reuse it in subsequent task
- name: Read device information (always use unit when probing)
  community.general.parted: device=/dev/sdb unit=MiB
  register: sdb_info

- name: Remove all partitions from disk
  community.general.parted:
    device: /dev/sdb
    number: '{{ item.num }}'
    state: absent
  loop: '{{ sdb_info.partitions }}'

- name: Extend an existing partition to fill all available space
  community.general.parted:
    device: /dev/sdb
    number: "{{ sdb_info.partitions | length }}"
    part_end: "100%"
    resize: true
    state: present

返回值

常见返回值已在此处记录,以下是此模块独有的字段

描述

partition_info

复杂类型

当前分区信息

返回:成功

示例: {"disk": {"dev": "/dev/sdb", "logical_block": 512, "model": "VMware Virtual disk", "physical_block": 512, "size": 5.0, "table": "msdos", "unit": "gib"}, "partitions": [{"begin": 0.0, "end": 1.0, "flags": ["boot", "lvm"], "fstype": "", "name": "", "num": 1, "size": 1.0}, {"begin": 1.0, "end": 5.0, "flags": [], "fstype": "", "name": "", "num": 2, "size": 4.0}], "script": "unit KiB print "}

disk

字典

通用设备信息。

返回:成功

partitions

列表 / 元素=字符串

设备分区的列表。

返回:成功

script

字符串

模块执行的 parted 脚本

返回:成功

作者

  • Fabrizio Colonna (@ColOfAbRiX)