community.general.packet_device 模块 – 管理 Packet 主机中的裸金属服务器

注意

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

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

要安装它,请使用:ansible-galaxy collection install community.general。您需要进一步的要求才能使用此模块,请参阅 要求 了解详细信息。

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

概要

  • 管理 Packet 主机中的裸金属服务器(API 术语中的“设备”)。

  • 创建机器时,可以选择等待公共 IP 地址或等待活动状态。

  • 此模块依赖于 packet >= 1.0。

  • API 文档位于 https://www.packet.net/developers/api/devices

要求

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

  • packet-python >= 1.35

参数

参数

注释

always_pxe

布尔值

将 PXE 保留为第一个启动选项。

通常,PXE 进程仅在第一次启动时发生。设置此参数可使您的设备持续启动到 iPXE。

选择

  • false ← (默认)

  • true

auth_token

字符串

Packet API 令牌。您也可以在环境变量 PACKET_API_TOKEN 中提供它。

count

整数

要创建的设备数量。计数编号可以通过 %d 字符串格式化程序包含在主机名中。

默认值: 1

count_offset

整数

从哪个数字开始计数。

默认值: 1

device_ids

列表 / 元素=字符串

要操作的设备 ID 列表。

facility

字符串

用于设备创建的设施别名。有关当前列表,请参阅 Packet API - https://www.packet.net/developers/api/facilities/

features

字典

用于设备创建的包含“功能”的字典。有关详细信息,请参阅 Packet API 文档。

hostnames

别名:name

列表 / 元素=字符串

设备的 hostname 或 hostname 列表。

如果给定字符串或单项列表,您可以使用 "%d" Python 字符串格式来扩展 count 中的数字。

如果只有一个 hostname,如果 count>1,则可能会扩展到列表。

ipxe_script_url

字符串

用于配置的自定义 iPXE 脚本的 URL。

有关 Packet 设备自定义 iPXE 的更多信息,请访问 https://help.packet.net/technical/infrastructure/custom-ipxe

默认值: ""

locked

别名:lock

布尔值

是否锁定创建的设备。

选择

  • false ← (默认)

  • true

operating_system

字符串

用于设备创建的操作系统别名。有关当前列表,请参阅 Packet API - https://www.packet.net/developers/api/operatingsystems/

plan

字符串

用于设备创建的计划别名。有关当前列表,请参阅 Packet API - https://www.packet.net/developers/api/plans/

project_id

字符串 / 必需

设备的 Project ID。

state

字符串

设备的所需状态。

如果设置为 present (默认值),则模块调用将在设备创建 HTTP 请求成功返回后立即返回。

如果设置为 active,则模块调用将阻塞,直到所有指定的设备都由于 Packet API 而处于活动状态,或者直到 wait_timeout

选择

  • "present" ← (默认)

  • "absent"

  • "active"

  • "inactive"

  • "rebooted"

tags

列表 / 元素=字符串

在 community.general 0.2.0 中添加

设备标签列表。

目前仅在设备创建时实现。

user_data

字符串

提供给机器的用户数据 Blob

wait_for_public_IPv

整数

是否等待实例分配到公共 IPv4/IPv6 地址。

如果设置为 4,它将等待直到 IPv4 被分配给实例。

如果设置为 6,则等待直到公共 IPv6 被分配给实例。

选择

  • 4

  • 6

wait_timeout

整数

等待自动分配 IP 地址或设备达到 active 状态的等待时间(秒)。

如果设置了 wait_for_public_IPv 并且 state=active,则模块将依次等待这两个事件,超时时间将应用两次。

默认值: 900

属性

属性

支持

描述

check_mode

支持:

可以在 check_mode 中运行,并返回更改状态预测,而无需修改目标。

diff_mode

支持:

在差异模式下,将返回已更改(或可能需要在 check_mode 中更改)的详细信息。

示例

# All the examples assume that you have your Packet API token in environment variable PACKET_API_TOKEN.
# You can also pass it to the auth_token parameter of the module instead.

# Creating devices

- name: Create 1 device
  hosts: localhost
  tasks:
  - community.general.packet_device:
      project_id: 89b497ee-5afc-420a-8fb5-56984898f4df
      hostnames: myserver
      tags: ci-xyz
      operating_system: ubuntu_16_04
      plan: baremetal_0
      facility: sjc1

# Create the same device and wait until it is in state "active", (when it's
# ready for other API operations). Fail if the device is not "active" in
# 10 minutes.

- name: Create device and wait up to 10 minutes for active state
  hosts: localhost
  tasks:
  - community.general.packet_device:
      project_id: 89b497ee-5afc-420a-8fb5-56984898f4df
      hostnames: myserver
      operating_system: ubuntu_16_04
      plan: baremetal_0
      facility: sjc1
      state: active
      wait_timeout: 600

- name: Create 3 ubuntu devices called server-01, server-02 and server-03
  hosts: localhost
  tasks:
  - community.general.packet_device:
      project_id: 89b497ee-5afc-420a-8fb5-56984898f4df
      hostnames: server-%02d
      count: 3
      operating_system: ubuntu_16_04
      plan: baremetal_0
      facility: sjc1

- name: Create 3 coreos devices with userdata, wait until they get IPs and then wait for SSH
  hosts: localhost
  tasks:
  - name: Create 3 devices and register their facts
    community.general.packet_device:
      hostnames: [coreos-one, coreos-two, coreos-three]
      operating_system: coreos_stable
      plan: baremetal_0
      facility: ewr1
      locked: true
      project_id: 89b497ee-5afc-420a-8fb5-56984898f4df
      wait_for_public_IPv: 4
      user_data: |
        #cloud-config
        ssh_authorized_keys:
          - {{ lookup('file', 'my_packet_sshkey') }}
        coreos:
          etcd:
            discovery: https://discovery.etcd.io/6a28e078895c5ec737174db2419bb2f3
            addr: $private_ipv4:4001
            peer-addr: $private_ipv4:7001
          fleet:
            public-ip: $private_ipv4
          units:
            - name: etcd.service
              command: start
            - name: fleet.service
              command: start
    register: newhosts

  - name: Wait for ssh
    ansible.builtin.wait_for:
      delay: 1
      host: "{{ item.public_ipv4 }}"
      port: 22
      state: started
      timeout: 500
    with_items: "{{ newhosts.devices }}"


# Other states of devices

- name: Remove 3 devices by uuid
  hosts: localhost
  tasks:
  - community.general.packet_device:
      project_id: 89b497ee-5afc-420a-8fb5-56984898f4df
      state: absent
      device_ids:
        - 1fb4faf8-a638-4ac7-8f47-86fe514c30d8
        - 2eb4faf8-a638-4ac7-8f47-86fe514c3043
        - 6bb4faf8-a638-4ac7-8f47-86fe514c301f

返回值

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

描述

changed

布尔值

如果设备以任何方式被更改(创建、修改或删除),则为 True

返回:成功

示例: true

devices

列表 / 元素=字符串

有关已处理的每个设备的信息

返回:成功

示例: [{"hostname": "my-server.com", "id": "2a5122b9-c323-4d5c-b53c-9ad3f54273e7", "locked": false, "private-ipv4": "10.0.15.12", "public_ipv4": "147.229.15.12", "public_ipv6": "2604:1380:2:5200::3", "state": "provisioning", "tags": []}]

作者

  • Tomas Karasek (@t0mk)

  • Matt Baldwin (@baldwinSPC)

  • Thibaud Morel l’Horset (@teebes)