community.digitalocean.digital_ocean_droplet 模块 – 创建和删除 DigitalOcean Droplet

注意

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

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

要安装它,请使用:ansible-galaxy collection install community.digitalocean

要在 playbook 中使用它,请指定:community.digitalocean.digital_ocean_droplet

概要

  • 在 DigitalOcean 中创建和删除 droplet,并可选择等待其变为活动状态。

参数

参数

注释

backups

布尔值

指示是否应启用自动备份。

选择

  • false ← (默认)

  • true

baseurl

字符串

DigitalOcean API 基本 URL。

默认: "https://api.digitalocean.com/v2"

firewall

列表 / 元素=字符串

要应用到 Droplet 的防火墙名称数组。

省略当前应用于 droplet 的防火墙名称将删除它。

id

别名: droplet_id

整数

您要操作的 Droplet ID。

image

别名: image_id

字符串

这是您要使用其创建 Droplet 的镜像的 slug。

ipv6

布尔值

为 Droplet 启用 IPv6。

选择

  • false ← (默认)

  • true

monitoring

布尔值

指示是否安装 DigitalOcean 代理进行监控。

选择

  • false ← (默认)

  • true

name

字符串

这是 Droplet 的名称。

必须按照主机名规则格式化。

oauth_token

别名: api_token

字符串

DigitalOcean OAuth 令牌。

还有几个其他的环境变量可以用来提供这个值。

例如,- DO_API_TOKENDO_API_KEYDO_OAUTH_TOKENOAUTH_TOKEN

private_networking

布尔值

向 Droplet 添加额外的私有网络接口(用于 Droplet 间的通信)。

选择

  • false ← (默认)

  • true

project_name

别名: project

字符串

将资源分配到的项目(项目名称,而不是 UUID)。

默认为帐户的默认项目(空字符串)。

目前仅在创建时受支持。

默认: ""

region

别名: region_id

字符串

这是您希望在其中创建 Droplet 的区域的 slug。

resize_disk

布尔值

是否在调整大小时增加磁盘大小。

仅当 unique_nametrue 时才会进行咨询。

Droplet 的 size 必须指示增加。

选择

  • false ← (默认)

  • true

size

别名: size_id

字符串

这是您要使用其创建 Droplet 的大小的 slug。

有关当前的 slug,请参见 https://slugs.do-api.dev/

sleep_interval

整数

在操作和状态检查之间 sleep 的时间长度。

默认值为 10 秒;该值应小于 wait_timeout 且不为零。

默认: 10

ssh_keys

列表 / 元素=字符串

您希望添加到 Droplet 的 SSH 密钥指纹数组。

state

字符串

指示目标所需的state。

present 将创建指定的 droplet;请注意 unique_name 参数。

absent 将删除指定的 droplet(如果存在)。

active 将创建指定的 droplet(除非它已存在)并确保其已开机。

inactive 将创建指定的 droplet(除非它已存在)并确保其已关机。

选择

  • "present" ← (默认)

  • "absent"

  • "active"

  • "inactive"

tags

列表 / 元素=字符串

一个字符串列表,表示在创建 Droplet 后要应用到 Droplet 的标签名称。

标签名称可以是已存在的标签,也可以是新标签。

timeout

整数

用于轮询 DigitalOcean API 的超时时间,以秒为单位。

默认值: 30

unique_name

布尔值

要求唯一的主机名。

默认情况下,DigitalOcean 允许存在多个同名主机。

将此项设置为 true 时,每个名称只允许有一个主机。

对幂等性很有用。

选择

  • false ← (默认)

  • true

user_data

字符串

提供给 Droplet 的不透明数据块。

validate_certs

布尔值

如果设置为 no,则不会验证 SSL 证书。

只有在使用自签名证书的个人控制站点上,才应将此项设置为 no

选择

  • false

  • true ← (默认)

volumes

列表 / 元素=字符串

一个列表,其中包含要附加到 Droplet 的每个块存储卷的唯一字符串标识符。

vpc_uuid

字符串

在 community.digitalocean 0.1.0 中添加

一个字符串,指定 Droplet 将被分配到的 VPC 的 UUID。

如果排除此项,Droplet 将被分配到该区域帐户的默认 VPC。

wait

布尔值

在返回之前等待 Droplet 变为活动状态。

如果 wait 为 false,则可能不会返回 IP 地址。

选择

  • false

  • true ← (默认)

wait_timeout

整数

在创建 Droplet 时,wait 放弃等待前的时长,以秒为单位。

默认值: 120

示例

- name: Create a new Droplet
  community.digitalocean.digital_ocean_droplet:
    state: present
    oauth_token: "{{ lookup('ansible.builtin.env', 'DO_API_TOKEN') }}"
    name: mydroplet
    size: s-1vcpu-1gb
    region: sfo3
    image: ubuntu-20-04-x64
    wait_timeout: 500
    ssh_keys: [ .... ]
  register: my_droplet

- name: Show Droplet info
  ansible.builtin.debug:
    msg: |
      Droplet ID is {{ my_droplet.data.droplet.id }}
      First Public IPv4 is {{ (my_droplet.data.droplet.networks.v4 | selectattr('type', 'equalto', 'public')).0.ip_address | default('<none>', true) }}
      First Private IPv4 is {{ (my_droplet.data.droplet.networks.v4 | selectattr('type', 'equalto', 'private')).0.ip_address | default('<none>', true) }}

- name: Create a new Droplet (and assign to Project "test")
  community.digitalocean.digital_ocean_droplet:
    state: present
    oauth_token: "{{ lookup('ansible.builtin.env', 'DO_API_TOKEN') }}"
    name: mydroplet
    size: s-1vcpu-1gb
    region: sfo3
    image: ubuntu-20-04-x64
    wait_timeout: 500
    ssh_keys: [ .... ]
    project: test
  register: my_droplet

- name: Ensure a Droplet is present
  community.digitalocean.digital_ocean_droplet:
    state: present
    oauth_token: "{{ lookup('ansible.builtin.env', 'DO_API_TOKEN') }}"
    id: 123
    name: mydroplet
    size: s-1vcpu-1gb
    region: sfo3
    image: ubuntu-20-04-x64
    wait_timeout: 500

- name: Ensure a Droplet is present and has firewall rules applied
  community.digitalocean.digital_ocean_droplet:
    state: present
    oauth_token: "{{ lookup('ansible.builtin.env', 'DO_API_TOKEN') }}"
    id: 123
    name: mydroplet
    size: s-1vcpu-1gb
    region: sfo3
    image: ubuntu-20-04-x64
    firewall: ['myfirewall', 'anotherfirewall']
    wait_timeout: 500

- name: Ensure a Droplet is present with SSH keys installed
  community.digitalocean.digital_ocean_droplet:
    state: present
    oauth_token: "{{ lookup('ansible.builtin.env', 'DO_API_TOKEN') }}"
    id: 123
    name: mydroplet
    size: s-1vcpu-1gb
    region: sfo3
    ssh_keys: ['1534404', '1784768']
    image: ubuntu-20-04-x64
    wait_timeout: 500

返回值

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

描述

assign_status

字符串

分配状态(ok、not_found、assigned、already_assigned、service_down)

已返回: changed

示例: "assigned"

data

字典

一个 DigitalOcean Droplet

已返回: changed

示例: {"droplet": {"backup_ids": [], "created_at": "2014-11-14T16:36:31Z", "disk": 20, "features": ["virtio"], "id": 3164494, "image": {}, "kernel": {"id": 2233, "name": "Ubuntu 14.04 x64 vmlinuz-3.13.0-37-generic", "version": "3.13.0-37-generic"}, "locked": true, "memory": 512, "name": "example.com", "networks": {}, "region": {}, "size": {}, "size_slug": "512mb", "snapshot_ids": [], "status": "new", "tags": ["web"], "vcpus": 1, "volume_ids": []}, "ip_address": "104.248.118.172", "ipv6_address": "2604:a880:400:d1::90a:6001", "private_ipv4_address": "10.136.122.141"}

msg

字符串

执行期间遇到的信息或错误消息

已返回: changed

示例: "No project named test2 found"

resources

字典

项目分配中涉及的资源分配

已返回: changed

示例: {"assigned_at": "2021-10-25T17:39:38Z", "links": {"self": "https://api.digitalocean.com/v2/droplets/3164494"}, "status": "assigned", "urn": "do:droplet:3164494"}

作者

  • Gurchet Rai (@gurch101)

  • Mark Mercado (@mamercad)