amazon-web-services - AWS 动态 list 不会提取重复的主机名

标签 amazon-web-services amazon-ec2 ansible ansible-inventory ansible-tower

我正在使用 Ansible list yaml 文件来获取正在运行的主机列表,但我只获得了唯一的命名主机。正如您在 AWS EC2 中看到的那样,我有三台名为 GamingServers 的服务器,但我只获得其中一台主机。

hosts in AWS console

当 AWX Tower 运行 Ansible list 脚本时,它只会获得一台主机。我有 14 个主机,但有些是重名。

即使标签名称重复,如何获取所有正在运行的主机?

AWX output

AWX hosts

库存.yml:

plugin: aws_ec2
regions:
  - us-east-1
  - us-west-1
  - us-west-2
  - ap-south-1
filters:
  "instance-state-name": running
strict_permissions: False
hostnames:
  - tag:Name
keyed_groups:
- prefix: tag_Name_
  key: tags.Name
  separator: ""
compose:
  ansible_host: public_ip_address
# Can use IAM role in future to avoid IAM users and static passwords
#iam_role_arn: "asdfas"

更新:当我尝试@β.εηοιτ.βε 回答时出现以下错误

[WARNING]:  * Failed to parse /tmp/bwrap_95_9mfzyo3u/awx_95_d5iml868/project/in
ventories/aws/all_running_hosts.aws_ec2.yml with auto plugin: Invalid filter
'{'name': 'instance-id', 'separator': '_', 'prefix': 'tag:Name'}' provided;
filter must be one of ['affinity', 'architecture', 'availability-zone', 'block-
device-mapping.attach-time', 'block-device-mapping.delete-on-termination',
'block-device-mapping.device-name', 'block-device-mapping.status', 'block-
device-mapping.volume-id', 'client-token', 'dns-name', 'group-id', 'group-
name', 'host-id', 'hypervisor', 'iam-instance-profile.arn', 'image-id',
'instance-id', 'instance-lifecycle', 'instance-state-code', 'instance-state-
name', 'instance-type', 'instance.group-id', 'instance.group-name', 'ip-
address', 'kernel-id', 'key-name', 'launch-index', 'launch-time', 'monitoring-
state', 'network-interface.addresses.association.ip-owner-id', 'network-
interface.addresses.association.public-ip', 'network-
interface.addresses.primary', 'network-interface.addresses.private-ip-address',
'network-interface.association.allocation-id', 'network-
interface.association.association-id', 'network-interface.association.ip-owner-
id', 'network-interface.association.public-ip', 'network-
interface.attachment.attach-time', 'network-interface.attachment.attachment-
id', 'network-interface.attachment.delete-on-termination', 'network-
interface.attachment.device-index', 'network-interface.attachment.instance-id',
'network-interface.attachment.instance-owner-id', 'network-
interface.attachment.status', 'network-interface.availability-zone', 'network-
interface.description', 'network-interface.group-id', 'network-interface.group-
name', 'network-interface.ipv6-addresses.ipv6-address', 'network-interface.mac-
address', 'network-interface.network-interface-id', 'network-interface.owner-
id', 'network-interface.private-dns-name', 'network-interface.requester-
managed', 'network-interface.source-dest-check', 'network-interface.status',
'network-interface.subnet-id', 'network-interface.vpc-id', 'owner-id',
'placement-group-name', 'platform', 'private-dns-name', 'private-ip-address',
'product-code', 'product-code.type', 'ramdisk-id', 'reason', 'requester-id',
'reservation-id', 'root-device-name', 'root-device-type', 'source-dest-check',
'spot-instance-request-id', 'state-reason-code', 'state-reason-message',
'subnet-id', 'tag', 'tag-key', 'tag-value', 'tenancy', 'virtualization-type',
'vpc-id'].

最佳答案

这是由于使用了

hostnames:
  - tag:Name

但是,在 Ansible list 中,主机名是唯一标识符。
所以,在幕后真正发生的是实例正在覆盖自己,因为它们确实具有相同的主机名。

如果您想保持将 tag:Name 作为主机名的可读性并在您的 list 中拥有这些多个实例,您可以在这里做的是将标签的主机名与唯一值,例如主机的实例 ID,例如:

hostnames:
  - name: 'instance-id'
    separator: '_'
    prefix: 'tag:Name'

这将为您提供如下主机名:

  • GamingServer_i-05245d11820c11f4b
  • GamingServer_i-01b11144ac8cec108
  • GamingServer_i-031df0231c429149a

关于amazon-web-services - AWS 动态 list 不会提取重复的主机名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70402903/

相关文章:

amazon-web-services - 使用 Boto3 客户端与资源时的约定是什么?

amazon-web-services - S3 "US Standard"区域与 EC2 中的 "us-east-1"区域相同吗?

ansible - 比较多个主机的 ansible 注册变量

vim - vim 启动时忽略 vimrc 中的所有错误

azure - 从 Azure HDInsight 迁移到 Amazon EMR?

linux - Amazon EC2 无法通过 Ubuntu 连接 SSH

Android Amazon AWS Cognito 工作流程确认

ssl - Amazon Elastic Load Balancer SSL 证书安装方法

mysql - amazon aws 非常非常慢,mysql 和 apache 占用大量内存

linux - Ansible smtp 邮件发送