user630702
user630702

Reputation: 3167

AWS Dynamic Inventory doesn't pull duplicate hostnames

I'm using Ansible inventory yaml file to get the list of running hosts but I get only unique named hosts. As you can see in AWS EC2 I have three servers named GamingServers, but I get only one of the hosts.

hosts in AWS console

When AWX Tower runs the Ansible inventory script it only gets one host. I have 14 hosts but some are duplicate names.

How can I get all running hosts even if the tag name is duplicate?

AWX output

AWX hosts

inventory.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"

Update: I get the following error when I try @β.εηοιτ.βε answer

[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'].

Upvotes: 3

Views: 759

Answers (1)

β.εηοιτ.βε
β.εηοιτ.βε

Reputation: 39304

This is caused by the usage of

hostnames:
  - tag:Name

But, in an Ansible inventory, a hostname is a unique identifier.
So, what really happens behind the scene is that the instances are overriding themselves, as they do have the same hostname.

What you can do here, if you want to keep the readability of having your tag:Name as the hostname and have those multiple instances in your inventory is to compose the hostname of the tag along with a unique value, like the instance ID of the host, something like:

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

This will give you hostnames like:

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

Upvotes: 3

Related Questions