amazon.aws.aws_ec2 库存 – EC2 库存源

注意

此库存插件是 amazon.aws 集合(版本 9.0.0)的一部分。

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

要安装它,请使用:ansible-galaxy collection install amazon.aws。您需要其他要求才能使用此库存插件,有关详细信息,请参阅 要求

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

概要

  • 从 Amazon Web Services EC2 获取库存主机。

  • 库存文件是一个 YAML 配置文件,必须以 aws_ec2.{yml|yaml} 结尾。示例:my_inventory.aws_ec2.yml

要求

以下要求需要在执行此库存的本地控制器节点上满足。

  • 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_keyprofile 选项互斥。

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

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

配置

  • 环境变量:AWS_ACCESS_KEY_ID

  • 环境变量:AWS_ACCESS_KEY

  • 环境变量:EC2_ACCESS_KEY

    在以下版本中删除:2024-12-01 之后的重大版本

    原因:名称中的 EC2 暗示它仅限于 EC2 资源。但是,它用于所有连接。

    替代方案:AWS_ACCESS_KEY_ID

allow_duplicated_hosts

布尔值

在 amazon.aws 5.0.0 中添加

默认情况下,返回与 hostnames 列表的条目匹配的第一个名称。

如果您不介意库存中存在重复条目,并且想要获取所有匹配的主机名,请打开此标志。

选项

  • false ← (默认)

  • true

assume_role_arn

别名:iam_role_arn

字符串

要执行查找而要承担的 IAM 角色的 ARN。

您仍然应该提供具有足够权限执行 AssumeRole 操作的 AWS 凭证。

cache

布尔值

切换以启用/禁用库存源数据的缓存,需要设置缓存插件才能工作。

选项

  • false ← (默认)

  • true

配置

cache_connection

字符串

缓存连接数据或路径,有关详细信息,请阅读缓存插件文档。

配置

cache_plugin

字符串

用于库存源数据的缓存插件。

默认值: "memory"

配置

cache_prefix

字符串

用于缓存插件文件/表的前缀。

默认值: "ansible_inventory_"

配置

cache_timeout

整数

缓存持续时间,以秒为单位。

默认值: 3600

配置

compose

字典

从 jinja2 表达式创建变量。

默认值: {}

endpoint_url

别名: aws_endpoint_url, endpoint

字符串

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

endpoint 别名已被弃用,将在 2024-12-01 之后的版本中删除。

配置

  • 环境变量:AWS_URL

  • 环境变量:EC2_URL

    在以下版本中删除:2024-12-01 之后的重大版本

    原因:名称中的 EC2 暗示它仅限于 EC2 资源。但是,它用于所有连接。

    替代项:AWS_URL

exclude_filters

列表 / 元素=字典

添加于 amazon.aws 1.5.0

一个过滤器列表。任何匹配其中一个过滤器的实例都将从结果中排除。

exclude_filters 中的过滤器优先级高于 include_filtersfilters 键。

此处列出了可用的过滤器 http://docs.aws.amazon.com/cli/latest/reference/ec2/describe-instances.html#options

此列表中的每个条目都会触发一个搜索查询。因此,从性能的角度来看,最好保持列表尽可能短。

默认值: []

filters

字典

过滤器值对的字典。

此处列出了可用的过滤器 http://docs.aws.amazon.com/cli/latest/reference/ec2/describe-instances.html#options

默认值: {}

groups

字典

根据 Jinja2 条件将主机添加到组。

默认值: {}

hostnames

列表 / 元素=任意

主机名变量的优先级列表。

列表的元素可以是具有下面提到的键的字典,也可以是字符串。

可以是 http://docs.aws.amazon.com/cli/latest/reference/ec2/describe-instances.html#options 中指定的选项之一。

如果提供的值在上述选项中不存在,它将用作文字字符串。

要使用标签作为主机名,请使用语法 tag:Name=Value 来使用主机名 Name_Value,或 tag:Name 来使用 Name 标签的值。

默认值: []

name

字符串 / 必选

主机的名称。

prefix

字符串

要添加到 name 前面的前缀。与 name 相同的选项。

如果指定了 prefix,则最终主机名将为 prefix + separator + name

默认值: ""

separator

字符串

当指定 prefix 时,用于分隔 prefixname 的值。

默认值: "_"

hostvars_prefix

字符串

添加于 amazon.aws 3.1.0

来自 AWS 的主机变量名称的前缀。

hostvars_suffix

字符串

添加于 amazon.aws 3.1.0

来自 AWS 的主机变量名称的后缀。

include_filters

列表 / 元素=字典

添加于 amazon.aws 1.5.0

一个过滤器列表。任何匹配至少一个过滤器的实例都将包含在结果中。

此处列出了可用的过滤器 http://docs.aws.amazon.com/cli/latest/reference/ec2/describe-instances.html#options

此列表中的每个条目都会触发一个搜索查询。因此,从性能的角度来看,最好保持列表尽可能短。

默认值: []

keyed_groups

列表 / 元素=字典

根据变量的值将主机添加到组。

默认值: []

default_value

字符串

添加于 ansible-core 2.12

当主机变量的值为空字符串时,使用的默认值。

此选项与 keyed_groups[].trailing_separator 互斥。

key

字符串

用于生成组的输入字典中的键。

parent_group

字符串

键组的父组。

prefix

字符串

键组名称将以此前缀开头。

默认值: ""

separator

字符串

用于构建键组名称的分隔符。

默认值: "_"

trailing_separator

布尔值

添加于 ansible-core 2.12

将此选项设置为 false 以在值为空字符串时省略主机变量后的 keyed_groups[].separator

此选项与 keyed_groups[].default_value 互斥。

选项

  • false

  • true ← (默认)

leading_separator

布尔值

添加于 ansible-core 2.11

keyed_groups 结合使用。

默认情况下,没有提供前缀或分隔符的键组的名称将以下划线开头。

这是因为默认前缀是 "",默认分隔符是 "_"

如果未提供前缀,请将此选项设置为 false 以省略前导下划线(或其他分隔符)。

如果组名是从映射派生的,则分隔符仍然用于连接项目。

要完全不在组名中使用分隔符,请将键组的分隔符设置为空字符串。

选项

  • false

  • true ← (默认)

profile

别名:aws_profile, boto_profile

字符串

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

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

profile 选项与 aws_access_keyaws_secret_keysecurity_token 选项互斥。

boto_profile 别名已被弃用,将在 2024-12-01 之后的版本中删除。

配置

region

别名:aws_region, ec2_region

字符串

要使用的 AWS 区域。

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

配置

  • 环境变量:AWS_REGION

  • 环境变量:EC2_REGION

    在以下版本中删除:2024-12-01 之后的重大版本

    原因:名称中的 EC2 暗示它仅限于 EC2 资源,但它用于所有连接

    替代项:AWS_REGION

regions

列表 / 元素=字符串

要描述 EC2 实例的区域列表。

如果为空(默认),这将包括所有区域,但可能包括受限区域,如 us-gov-west-1 和 cn-north-1。

默认值: []

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

secret_keyprofile 选项互斥。

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

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

配置

  • 环境变量:AWS_SECRET_ACCESS_KEY

  • 环境变量:AWS_SECRET_KEY

  • 环境变量:EC2_SECRET_KEY

    在以下版本中删除:2024-12-01 之后的重大版本

    原因:名称中的 EC2 暗示它仅限于 EC2 资源。但是,它用于所有连接。

    替代项:AWS_SECRET_ACCESS_KEY

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

security_tokenprofile 选项互斥。

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

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

配置

  • 环境变量:AWS_SESSION_TOKEN

  • 环境变量:AWS_SECURITY_TOKEN

    在以下版本中删除:2024-12-01 之后的重大版本

    原因:AWS_SECURITY_TOKEN 用于与原始 boto SDK 兼容,该 SDK 的支持已删除

    替代项:AWS_SESSION_TOKEN

  • 环境变量:EC2_SECURITY_TOKEN

    在以下版本中删除:2024-12-01 之后的重大版本

    原因:名称中的 EC2 暗示它仅限于 EC2 资源。但是,它用于所有连接。

    替代项:AWS_SESSION_TOKEN

strict

布尔值

如果为 yes,则使无效条目成为致命错误,否则跳过并继续。

由于可以在表达式中使用事实,因此它们可能并不总是可用,默认情况下我们会忽略这些错误。

选项

  • false ← (默认)

  • true

strict_permissions

布尔值

默认情况下,如果遇到 403(禁止)错误代码,此插件将失败。

您可以在清单配置文件中将此选项设置为 False,这将允许优雅地跳过 403 错误。

选项

  • false

  • true ← (默认)

use_contrib_script_compatible_ec2_tag_keys

布尔值

添加于 amazon.aws 1.5.0

使用类似旧版 ec2.py 清单脚本的 ec2_tag_TAGNAME 键公开主机标签。

不建议使用此功能,我们建议迁移到新的 ``tags`` 结构。

选项

  • false ← (默认)

  • true

use_contrib_script_compatible_sanitization

布尔值

默认情况下,此插件使用通用的组名清理来创建安全且可用的组名,以便在 Ansible 中使用。此选项允许您覆盖该清理,以便从旧的清单脚本进行迁移,并匹配脚本的 ``replace_dash_in_groups`` 选项设置为 ``False`` 时的组清理。要使用构造的组复制 ``replace_dash_in_groups = True`` 的行为,您需要使用 regex_replace 过滤器将这些条目中的连字符替换为下划线。

要使其工作,您还应该关闭 TRANSFORM_INVALID_GROUP_CHARS 设置,否则核心引擎将在其基础上使用标准清理。

这不是默认设置,因为此类名称会破坏某些功能,因为并非所有字符都是有效的 Python 标识符,而组名称最终会作为 Python 标识符使用。

选项

  • false ← (默认)

  • true

use_extra_vars

布尔值

添加于 ansible-core 2.11

将额外变量合并到可用于组合的变量中(优先级最高)。

选项

  • false ← (默认)

  • true

配置

use_ssm_inventory

布尔值

在 amazon.aws 6.0.0 中添加

启用从 AWS Systems Manager (SSM) 清单服务中获取额外的 EC2 实例信息到 hostvars 中。

通过利用 SSM 清单数据,use_ssm_inventory 选项提供了有关清单中 EC2 实例的额外详细信息和属性。这些详细信息可以包括操作系统信息、已安装的软件、网络配置以及在 SSM 中定义的自定义清单属性。

选项

  • false ← (默认)

  • true

注释

注意

  • 如果未提供任何凭据,并且控制节点具有关联的 IAM 实例配置文件,则将使用该角色进行身份验证。

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

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

示例

# Minimal example using environment vars or instance role credentials
# Fetch all hosts in us-east-1, the hostname is the public DNS if it exists, otherwise the private IP address
plugin: amazon.aws.aws_ec2
regions:
  - us-east-1

---

# Example using filters, ignoring permission errors, and specifying the hostname precedence
plugin: amazon.aws.aws_ec2
# The values for profile, access key, secret key and token can be hardcoded like:
profile: aws_profile
# or you could use Jinja as:
# profile: "{{ lookup('env', 'AWS_PROFILE') | default('aws_profile', true) }}"
# Populate inventory with instances in these regions
regions:
  - us-east-1
  - us-east-2
filters:
  ## All instances with their `Environment` tag set to `dev`
  # tag:Environment: dev

  # All dev and QA hosts
  tag:Environment:
    - dev
    - qa
  instance.group-id: sg-xxxxxxxx
# Ignores 403 errors rather than failing
strict_permissions: false
# Note: I(hostnames) sets the inventory_hostname. To modify ansible_host without modifying
# inventory_hostname use compose (see example below).
hostnames:
  - tag:Name=Tag1,Name=Tag2  # Return specific hosts only
  - tag:CustomDNSName
  - dns-name
  - name: 'tag:Name=Tag1,Name=Tag2'
  - name: 'private-ip-address'
    separator: '_'
    prefix: 'tag:Name'
  - name: 'test_literal' # Using literal values for hostname
    separator: '-'       # Hostname will be aws-test_literal
    prefix: 'aws'

# Returns all the hostnames for a given instance
allow_duplicated_hosts: false

---

# Example using constructed features to create groups and set ansible_host
plugin: amazon.aws.aws_ec2
regions:
  - us-east-1
  - us-west-1
# keyed_groups may be used to create custom groups
strict: false
keyed_groups:
  # Add e.g. x86_64 hosts to an arch_x86_64 group
  - prefix: arch
    key: 'architecture'
  # Add hosts to tag_Name_Value groups for each Name/Value tag pair
  - prefix: tag
    key: tags
  # Add hosts to e.g. instance_type_z3_tiny
  - prefix: instance_type
    key: instance_type
  # Create security_groups_sg_abcd1234 group for each SG
  - key: 'security_groups|json_query("[].group_id")'
    prefix: 'security_groups'
  # Create a group for each value of the Application tag
  - key: tags.Application
    separator: ''
  # Create a group per region e.g. aws_region_us_east_2
  - key: placement.region
    prefix: aws_region
  # Create a group (or groups) based on the value of a custom tag "Role" and add them to a metagroup called "project"
  - key: tags['Role']
    prefix: foo
    parent_group: "project"
# Set individual variables with compose
compose:
  # Use the private IP address to connect to the host
  # (note: this does not modify inventory_hostname, which is set via I(hostnames))
  ansible_host: private_ip_address

---

# Example using include_filters and exclude_filters to compose the inventory.
plugin: amazon.aws.aws_ec2
regions:
  - us-east-1
  - us-west-1
include_filters:
  - tag:Name:
      - 'my_second_tag'
  - tag:Name:
      - 'my_third_tag'
exclude_filters:
  - tag:Name:
      - 'my_first_tag'

---

# Example using groups to assign the running hosts to a group based on vpc_id
plugin: amazon.aws.aws_ec2
profile: aws_profile
# Populate inventory with instances in these regions
regions:
  - us-east-2
filters:
  # All instances with their state as `running`
  instance-state-name: running
keyed_groups:
  - prefix: tag
    key: tags
compose:
  ansible_host: public_dns_name
groups:
  libvpc: vpc_id == 'vpc-####'

---

# Define prefix and suffix for host variables coming from AWS.
plugin: amazon.aws.aws_ec2
regions:
  - us-east-1
hostvars_prefix: 'aws_'
hostvars_suffix: '_ec2'

作者

  • Sloane Hertel (@s-hertel)

提示

每种条目类型的配置条目具有从低到高的优先级顺序。例如,列表中较低的变量将覆盖较高的变量。