amazon.aws.ec2_vpc_nat_gateway 模块 – 管理 AWS VPC NAT 网关

注意

此模块是 amazon.aws 集合 (版本 9.0.0) 的一部分。

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

要安装它,请使用: ansible-galaxy collection install amazon.aws。您需要其他要求才能使用此模块,请参阅 需求了解详情。

要在 playbook 中使用它,请指定: amazon.aws.ec2_vpc_nat_gateway

amazon.aws 1.0.0 中的新增功能

概要

  • 根据其 ID、分配和子网 ID 确保 AWS VPC NAT 网关的状态。

需求

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

  • python >= 3.6

  • boto3 >= 1.28.0

  • botocore >= 1.31.0

参数

参数

注释

access_key

别名:aws_access_key_id,aws_access_key,ec2_access_key

字符串

AWS 访问密钥 ID。

有关访问令牌的更多信息,请参阅 AWS 文档 https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html#access-keys-and-secret-access-keys

还可以按优先级递减的顺序使用 AWS_ACCESS_KEY_IDAWS_ACCESS_KEYEC2_ACCESS_KEY 环境变量。

aws_access_keyprofile 选项是互斥的。

为了与 AWS botocore SDK 保持一致,在 5.1.0 版本中添加了 aws_access_key_id 别名。

ec2_access_key 别名已被弃用,并将在 2024-12-01 之后的一个版本中移除。

EC2_ACCESS_KEY 环境变量的支持已被弃用,并将在 2024-12-01 之后的一个版本中移除。

allocation_id

字符串

弹性 IP 分配的 ID。如果不传递此参数且未传递 eip_address,则会为此 NAT 网关生成一个 EIP。

aws_ca_bundle

路径

验证 SSL 证书时要使用的 CA 证书包的位置。

还可以使用 AWS_CA_BUNDLE 环境变量。

aws_config

字典

用于修改 botocore 配置的字典。

可在 AWS 文档中找到参数 https://botocore.amazonaws.com/v1/documentation/api/latest/reference/config.html#botocore.config.Config

client_token

字符串

用于在创建期间确保幂等的可选唯一令牌。指定此选项时,请确保也指定 eip_address 参数,否则任何后续运行都将失败。

connectivity_type

字符串

amazon.aws 5.5.0 中新增

指示 NAT 网关是否支持公共或私有连接。

选项

  • "public" ← (默认)

  • "private"

debug_botocore_endpoint_logs

布尔值

使用 botocore.endpoint 日志记录器来解析在任务期间进行的唯一(而不是总计)"resource:action" API 调用,并将集合输出到任务结果中的 resource_actions 密钥。使用 aws_resource_action 回调将输出到 playbook 期间进行的总列表。

还可以使用 ANSIBLE_DEBUG_BOTOCORE_LOGS 环境变量。

选项

  • false ← (默认)

  • true

default_create

布尔值

amazon.aws 6.2.0 中新增

default_create=trueeip_address 已设置但尚未分配时,将创建 NAT 网关并自动分配新的 EIP。

default_create=falseeip_address 已设置但尚未分配时,模块将失败。

如果未设置 eip_address,则此参数无效。

选项

  • false ← (默认)

  • true

eip_address

字符串

要附加到此 NAT 网关的 EIP 的弹性 IP 地址。如果不传递此参数且未传递 allocation_id,则会为此 NAT 网关生成一个 EIP。

endpoint_url

别名:ec2_url,aws_endpoint_url,s3_url

字符串

连接到非默认 AWS 端点的 URL。虽然这可以用于连接到其他兼容 AWS 的服务,但 amazon.aws 和 community.aws 集合仅针对 AWS 进行过测试。

也可以使用 AWS_URLEC2_URL 环境变量,优先级递减。

ec2_urls3_url 别名已弃用,将在 2024-12-01 之后的一个版本中移除。

EC2_URL 环境变量的支持已弃用,将在 2024-12-01 之后的一个版本中移除。

if_exist_do_not_create

布尔值

如果 NAT 网关已存在于 subnet_id 中,则不要创建新的网关。

选项

  • false ← (默认)

  • true

nat_gateway_id

字符串

AWS 在创建时动态分配给 NAT 网关的 ID。当 absent 选项存在时,这是必需的。

profile

别名:aws_profile

字符串

用于身份验证的命名 AWS 配置文件。

有关命名配置文件的更多信息,请参见 AWS 文档 https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-profiles.html

也可以使用 AWS_PROFILE 环境变量。

profile 选项与 aws_access_keyaws_secret_keysecurity_token 选项互斥。

purge_tags

布尔值

如果 purge_tags=true 并且设置了 tags,则将从资源中清除现有标签,以完全匹配 tags 参数定义的内容。

如果没有设置 tags 参数,则即使 purge_tags=True,也不会修改标签。

aws: 开头的标签键由 Amazon 保留,无法修改。因此,在 purge_tags 参数中将忽略它们。有关更多信息,请参见 Amazon 文档 https://docs.aws.amazon.com/general/latest/gr/aws_tagging.html#tag-conventions

选项

  • false

  • true ← (默认)

region

别名:aws_region,ec2_region

字符串

要使用的 AWS 区域。

对于 IAM、Route53 和 CloudFront 等全球服务,将忽略 region

也可以使用 AWS_REGIONEC2_REGION 环境变量。

有关更多信息,请参见 Amazon AWS 文档 http://docs.aws.amazon.com/general/latest/gr/rande.html#ec2_region

ec2_region 别名已弃用,将在 2024-12-01 之后的一个版本中移除。

EC2_REGION 环境变量的支持已弃用,将在 2024-12-01 之后的一个版本中移除。

release_eip

布尔值

从 VPC 中释放 EIP。

此选项仅在 absent 状态下有效。

应将此选项与 wait 选项一起使用。因为在删除操作发生时无法释放地址。

选项

  • false ← (默认)

  • true

secret_key

别名:aws_secret_access_key,aws_secret_key,ec2_secret_key

字符串

AWS 密钥访问密钥。

有关访问令牌的更多信息,请参阅 AWS 文档 https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html#access-keys-and-secret-access-keys

也可以按优先级递减的顺序使用 AWS_SECRET_ACCESS_KEYAWS_SECRET_KEYEC2_SECRET_KEY 环境变量。

secret_keyprofile 选项互斥。

为了与 AWS botocore SDK 保持一致,在 5.1.0 版本中添加了 aws_secret_access_key 别名。

ec2_secret_key 别名已弃用,将在 2024-12-01 之后的一个版本中移除。

EC2_SECRET_KEY 环境变量的支持已弃用,将在 2024-12-01 之后的一个版本中移除。

session_token

别名:aws_session_token,security_token,aws_security_token,access_token

字符串

与临时凭证一起使用的 AWS STS 会话令牌。

有关访问令牌的更多信息,请参阅 AWS 文档 https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html#access-keys-and-secret-access-keys

也可以按优先级递减的顺序使用 AWS_SESSION_TOKENAWS_SECURITY_TOKENEC2_SECURITY_TOKEN 环境变量。

security_tokenprofile 选项互斥。

在 3.2.0 版本中添加了 aws_session_tokensession_token 别名,在 6.0.0 版本中将参数名称从 security_token 重命名为 session_token

security_tokenaws_security_tokenaccess_token 别名已弃用,将在 2024-12-01 之后的一个版本中移除。

EC2_SECRET_KEYAWS_SECURITY_TOKEN 环境变量的支持已弃用,将在 2024-12-01 之后的一个版本中移除。

state

字符串

确保 NAT 网关存在或不存在。

选项

  • "present" ← (默认)

  • "absent"

subnet_id

字符串

要在其中创建 NAT 网关的子网 ID。这在 present 选项中是必需的。

tags

别名:resource_tags

字典

表示要应用于资源的标签的字典。

如果没有设置 tags 参数,则不会修改标签。

validate_certs

布尔值

设置为 false 时,将不会验证与 AWS API 通信的 SSL 证书。

强烈建议不要设置 validate_certs=false,作为替代方案,请考虑改为设置 aws_ca_bundle

选项

  • false

  • true ← (默认)

wait

布尔值

等待操作完成后再返回。

选项

  • false ← (默认)

  • true

wait_timeout

整数

在超时前等待操作完成的秒数。

默认: 320

备注

注意

  • 在 1.4.0 版本中添加了对 tagspurge_tags 的支持。

  • 警告:对于模块,环境变量和配置文件是从 Ansible 的“主机”上下文而不是“控制器”上下文读取的。因此,可能需要将文件显式复制到“主机”。对于查找和连接插件,环境变量和配置文件是从 Ansible 的“控制器”上下文而不是“主机”上下文读取的。

  • Ansible 使用的 AWS SDK (boto3) 也可能从 Ansible“主机”上下文中的配置文件(通常为 ~/.aws/credentials)读取凭据和其他设置(如区域)的默认值。有关更多信息,请参见 https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html

示例

# Note: These examples do not set authentication details, see the AWS Guide for details.

- name: Create new nat gateway with client token.
  amazon.aws.ec2_vpc_nat_gateway:
    state: present
    subnet_id: subnet-12345678
    eip_address: 52.1.1.1
    region: ap-southeast-2
    client_token: abcd-12345678
  register: new_nat_gateway

- name: Create new nat gateway using an allocation-id and connectivity type.
  amazon.aws.ec2_vpc_nat_gateway:
    state: present
    subnet_id: subnet-12345678
    allocation_id: eipalloc-12345678
    connectivity_type: "private"
    region: ap-southeast-2
  register: new_nat_gateway

- name: Create new nat gateway, using an EIP address  and wait for available status.
  amazon.aws.ec2_vpc_nat_gateway:
    state: present
    subnet_id: subnet-12345678
    eip_address: 52.1.1.1
    wait: true
    region: ap-southeast-2
  register: new_nat_gateway

- name: Create new nat gateway and allocate new EIP.
  amazon.aws.ec2_vpc_nat_gateway:
    state: present
    subnet_id: subnet-12345678
    wait: true
    region: ap-southeast-2
  register: new_nat_gateway

- name: Create new nat gateway and allocate new EIP if a nat gateway does not yet exist in the subnet.
  amazon.aws.ec2_vpc_nat_gateway:
    state: present
    subnet_id: subnet-12345678
    wait: true
    region: ap-southeast-2
    if_exist_do_not_create: true
  register: new_nat_gateway

- name: Delete nat gateway using discovered nat gateways from facts module.
  amazon.aws.ec2_vpc_nat_gateway:
    state: absent
    region: ap-southeast-2
    wait: true
    nat_gateway_id: "{{ item.NatGatewayId }}"
    release_eip: true
  register: delete_nat_gateway_result
  loop: "{{ gateways_to_remove.result }}"

- name: Delete nat gateway and wait for deleted status.
  amazon.aws.ec2_vpc_nat_gateway:
    state: absent
    nat_gateway_id: nat-12345678
    wait: true
    wait_timeout: 500
    region: ap-southeast-2

- name: Delete nat gateway and release EIP.
  amazon.aws.ec2_vpc_nat_gateway:
    state: absent
    nat_gateway_id: nat-12345678
    release_eip: true
    wait: true
    wait_timeout: 300
    region: ap-southeast-2

- name: Create new nat gateway using allocation-id and tags.
  amazon.aws.ec2_vpc_nat_gateway:
    state: present
    subnet_id: subnet-12345678
    allocation_id: eipalloc-12345678
    region: ap-southeast-2
    tags:
      Tag1: tag1
      Tag2: tag2
  register: new_nat_gateway

- name: Update tags without purge
  amazon.aws.ec2_vpc_nat_gateway:
    subnet_id: subnet-12345678
    allocation_id: eipalloc-12345678
    region: ap-southeast-2
    purge_tags: false
    tags:
      Tag3: tag3
    wait: true
  register: update_tags_nat_gateway

返回值

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

Key

描述

connectivity_type

字符串

指示 NAT 网关是否支持公共或私有连接。

返回:始终

示例: "public"

create_time

字符串

UTC 中的 ISO 8601 日期时间格式。

返回:始终

示例: "2016-03-05T05:19:20.282000+00:00'"

nat_gateway_addresses

复杂

包含 public_ip、network_interface_id、private_ip 和 allocation_id 的字典列表。

返回:始终

示例: [{"allocation_id": "eipalloc-08ec128d03629671d", "association_id": "eipassoc-0d6365c7eeb7d4932", "is_primary": true, "network_interface_id": "eni-095104e630881bad6", "private_ip": "10.1.0.250", "public_ip": "34.202.90.172", "status": "succeeded"}]

allocation_id

字符串

与 NAT 网关关联的弹性 IP 地址的分配 ID。

返回:始终

示例: "eipalloc-0853e66a40803da76"

association_id

字符串

与 NAT 网关关联的弹性 IP 地址的关联 ID。

返回:始终

示例: "eipassoc-0d6365c7eeb7d4932"

is_primary

布尔值

定义IP地址是否为主地址。

返回:始终

示例: true

network_interface_id

字符串

与NAT网关关联的网络接口ID。

返回:始终

示例: "eni-0a37acdbe306c661c"

private_ip

字符串

与弹性IP地址关联的私有IP地址。

返回:始终

示例: "10.0.238.227"

public_ip

字符串

与NAT网关关联的弹性IP地址。

返回:始终

示例: "34.204.123.52"

status

字符串

地址状态。

返回:始终

示例: "succeeded"

nat_gateway_id

字符串

VPC NAT网关的ID。

返回:始终

示例: "nat-0d1e3a878585988f8"

state

字符串

NAT网关的当前状态。

返回:始终

示例: "available"

subnet_id

字符串

子网ID。

返回:始终

示例: "subnet-12345"

tags

字典

与VPC NAT网关关联的标签。

返回: 当存在标签时。

示例: {"tags": {"Ansible": "Test"}}

vpc_id

字符串

VPC的ID。

返回:始终

示例: "vpc-12345"

作者

  • Allen Sanabria (@linuxdynasty)

  • Jon Hadfield (@jonhadfield)

  • Karen Cheng (@Etherdaemon)

  • Alina Buzachis (@alinabuzachis)