community.aws.elb_target_info 模块 – 收集目标与哪些目标组关联。
注意
此模块是 community.aws 集合 (版本 9.0.0) 的一部分。
如果您使用的是 ansible 包,则可能已安装此集合。它不包含在 ansible-core 中。要检查它是否已安装,请运行 ansible-galaxy collection list。
要安装它,请使用: ansible-galaxy collection install community.aws。您需要其他要求才能使用此模块,有关详细信息,请参阅 要求。
要在 playbook 中使用它,请指定: community.aws.elb_target_info。
community.aws 1.0.0 中的新增功能
概要
- 此模块将搜索区域中的每个目标组,以查找哪些目标组已注册给定的实例 ID 或 IP。 
要求
执行此模块的主机需要以下要求。
- python >= 3.6 
- boto3 >= 1.28.0 
- botocore >= 1.31.0 
参数
| 参数 | 注释 | 
|---|---|
| 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 和 profile 选项是互斥的。 为与 AWS botocore SDK 保持一致,在 5.1.0 版本中添加了 aws_access_key_id 别名。 ec2_access_key 别名已弃用,将在 2024-12-01 后的某个版本中删除。 
 | |
| 验证 SSL 证书时要使用的 CA 捆绑包的位置。 还可以使用  | |
| 用于修改 botocore 配置的字典。 可以在 AWS 文档中找到参数 https://botocore.amazonaws.com/v1/documentation/api/latest/reference/config.html#botocore.config.Config。 | |
| 使用  还可以使用  选项 
 | |
| 连接到的 URL,而不是默认的 AWS 端点。虽然这可以用于连接到其他与 AWS 兼容的服务,但 amazon.aws 和 community.aws 集合仅针对 AWS 进行测试。 还可以按优先级递减的顺序使用  ec2_url 和 s3_url 别名已弃用,将在 2024-12-01 后的某个版本中删除。 
 | |
| 是否获取任何负载均衡器未使用目标组。 选项 
 | |
| 获取哪些实例 ID 的信息。 | |
| 用于身份验证的命名 AWS 配置文件。 有关命名配置文件的更多信息,请参阅 AWS 文档 https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-profiles.html。 还可以使用  profile 选项与 aws_access_key、aws_secret_key 和 security_token 选项是互斥的。 | |
| 要使用的 AWS 区域。 对于诸如 IAM、Route53 和 CloudFront 之类的全球服务,将忽略 region。 还可以使用  有关更多信息,请参阅 Amazon AWS 文档 http://docs.aws.amazon.com/general/latest/gr/rande.html#ec2_region。 ec2_region 别名已弃用,将在 2024-12-01 后的某个版本中删除。 对环境变量 | |
| AWS 密钥访问密钥。 有关访问令牌的更多信息,请参阅 AWS 文档 https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html#access-keys-and-secret-access-keys。 也可以使用环境变量 secret_key 和 profile 选项互斥。 为了与 AWS botocore SDK 保持一致,aws_secret_access_key 别名在 5.1.0 版本中添加。 ec2_secret_key 别名已弃用,并将在2024年12月1日后的某个版本中移除。 对环境变量 | |
| 用于临时凭证的 AWS STS 会话令牌。 有关访问令牌的更多信息,请参阅 AWS 文档 https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html#access-keys-and-secret-access-keys。 也可以使用环境变量 security_token 和 profile 选项互斥。 别名 aws_session_token 和 session_token 在 3.2.0 版本中添加,参数名称在 6.0.0 版本中从 security_token 重命名为 session_token。 security_token、aws_security_token 和 access_token 别名已弃用,并将在2024年12月1日后的某个版本中移除。 对环境变量 | |
| 设置为 强烈建议不要设置validate_certs=false,作为替代方案,请考虑设置aws_ca_bundle。 选项 
 | 
注释
注意
- 警告:对于模块,环境变量和配置文件是从 Ansible 的“主机”上下文而不是“控制器”上下文读取的。因此,可能需要将文件显式复制到“主机”。对于查找和连接插件,环境变量和配置文件是从 Ansible 的“控制器”上下文而不是“主机”上下文读取的。 
- Ansible 使用的 AWS SDK (boto3) 也可能从 Ansible “主机”上下文中的配置文件(通常为 - ~/.aws/credentials)读取凭据和其他设置的默认值,例如区域。有关更多信息,请参见https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html。
示例
# practical use case - dynamically de-registering and re-registering nodes
- name: Get EC2 Metadata
  amazon.aws.ec2_metadata_facts:
- name: Get initial list of target groups
  delegate_to: localhost
  community.aws.elb_target_info:
    instance_id: "{{ ansible_ec2_instance_id }}"
    region: "{{ ansible_ec2_placement_region }}"
  register: target_info
- name: save fact for later
  ansible.builtin.set_fact:
    original_tgs: "{{ target_info.instance_target_groups }}"
- name: Deregister instance from all target groups
  delegate_to: localhost
  community.aws.elb_target:
    target_group_arn: "{{ item.0.target_group_arn }}"
    target_port: "{{ item.1.target_port }}"
    target_az: "{{ item.1.target_az }}"
    target_id: "{{ item.1.target_id }}"
    state: absent
    target_status: "draining"
    region: "{{ ansible_ec2_placement_region }}"
  with_subelements:
    - "{{ original_tgs }}"
    - "targets"
  # This avoids having to wait for 'elb_target' to serially deregister each
  # target group.  An alternative would be to run all of the 'elb_target'
  # tasks async and wait for them to finish.
- name: wait for all targets to deregister simultaneously
  delegate_to: localhost
  community.aws.elb_target_info:
    get_unused_target_groups: false
    instance_id: "{{ ansible_ec2_instance_id }}"
    region: "{{ ansible_ec2_placement_region }}"
  register: target_info
  until: (target_info.instance_target_groups | length) == 0
  retries: 60
  delay: 10
- name: reregister in elbv2s
  community.aws.elb_target:
    region: "{{ ansible_ec2_placement_region }}"
    target_group_arn: "{{ item.0.target_group_arn }}"
    target_port: "{{ item.1.target_port }}"
    target_az: "{{ item.1.target_az }}"
    target_id: "{{ item.1.target_id }}"
    state: present
    target_status: "initial"
  with_subelements:
    - "{{ original_tgs }}"
    - "targets"
# wait until all groups associated with this instance are 'healthy' or
# 'unused'
- name: wait for registration
  community.aws.elb_target_info:
    get_unused_target_groups: false
    instance_id: "{{ ansible_ec2_instance_id }}"
    region: "{{ ansible_ec2_placement_region }}"
  register: target_info
  until: (target_info.instance_target_groups |
          map(attribute='targets') |
          flatten |
          map(attribute='target_health') |
          rejectattr('state', 'equalto', 'healthy') |
          rejectattr('state', 'equalto', 'unused') |
          list |
          length) == 0
  retries: 61
  delay: 10
# using the target groups to generate AWS CLI commands to reregister the
# instance - useful in case the playbook fails mid-run and manual
#            rollback is required
- name: "reregistration commands: ELBv2s"
  ansible.builtin.debug:
    msg: >
           aws --region {{ansible_ec2_placement_region}} elbv2
           register-targets --target-group-arn {{item.target_group_arn}}
           --targets{%for target in item.targets%}
           Id={{target.target_id}},
           Port={{target.target_port}}{%if target.target_az%},AvailabilityZone={{target.target_az}}
           {%endif%}
           {%endfor%}
  loop: "{{target_info.instance_target_groups}}"
返回值
常用返回值已在此处记录,以下是此模块特有的字段
| 键 | 描述 | 
|---|---|
| 实例注册到的目标组列表 返回值:始终返回 | |
| 目标组的 ARN 返回值:始终返回 示例: | |
| 此组使用的目标类型 返回值:始终返回 示例: | |
| 指向此实例 ID 的目标列表 返回值:始终返回 | |
| 与此目标显式关联的可用区 返回值:当可用区与此实例关联时 示例: | |
| 目标健康状况描述。 所有可能的值,请参见以下链接:https://boto3.readthedocs.io/en/latest/reference/services/elbv2.html#ElasticLoadBalancingv2.Client.describe_target_health 返回值:始终返回 | |
| 目标健康状况的描述 返回值:如果 state!=present 示例: | |
| 目标健康状况的原因代码 返回值:如果 state!=healthy 示例: | |
| 健康状态 返回值:始终返回 示例: | |
| 指向此实例的目标 ID 返回值:始终返回 示例: | |
| 此目标侦听的端口 返回值:始终返回 示例: | 
