Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ec2 inventory hostnames creation by combining instance data #231

Closed
vivanov-dp opened this issue Jan 8, 2021 · 3 comments
Closed

ec2 inventory hostnames creation by combining instance data #231

vivanov-dp opened this issue Jan 8, 2021 · 3 comments
Labels
feature This issue/PR relates to a feature request has_pr inventory inventory plugin

Comments

@vivanov-dp
Copy link

SUMMARY

Allow combining different bits of instance data to form the hostname, for example:

hostnames:
  - tag:ServerType+instance-id
  - dns-name+private-ip+image-id
  - dns-name+tag:Group
  etc ...
ISSUE TYPE
  • Feature Idea
COMPONENT NAME

aws_ec2 inventory plugin

ADDITIONAL INFORMATION

I'm using tags that group my instances in certain ways, and I prefer seeing the hosts in ansible logs named by these tags, for example - log_server, backups, etc, but since there are multiple hosts in each group, they override each other in the inventory, so I need a token to distinguish them. Adding the instance ID to the tag name seems the most rational way to do it, however I can't find anything in the docs about the aws_ec2 plugin that would allow me to achieve this.

I did a quick patch to the plugin, which enables the syntax in the example above:

--- aws_ec2.py     2021-01-08 11:07:41.259602467 +0000
+++ aws_ec2.py  2021-01-08 11:18:14.415506707 +0000
@@ -503,14 +503,21 @@
         if not hostnames:
             hostnames = ['dns-name', 'private-dns-name']

-        hostname = None
+        hostname = ''
         for preference in hostnames:
-            if 'tag' in preference:
-                if not preference.startswith('tag:'):
-                    raise AnsibleError("To name a host by tags name_value, use 'tag:name=value'.")
-                hostname = self._get_tag_hostname(preference, instance)
-            else:
-                hostname = self._get_boto_attr_chain(preference, instance)
+            bits = preference.split('+')
+            result = None
+            for bit in bits:
+                if 'tag' in bit:
+                    if not bit.startswith('tag:'):
+                        raise AnsibleError("To name a host by tags name_value, use 'tag:name=value'.")
+                    result = self._get_tag_hostname(bit, instance)
+                else:
+                    result = self._get_boto_attr_chain(bit, instance)
+                if result:
+                    if hostname:
+                        hostname += '-'
+                    hostname += result
             if hostname:
                 break
         if hostname:

Using it, I can get an inventory looking like this:

  ...
  |  |--b12tf-dev-efs-backups-i-0782...
  |  |--b12tf-gold-efs-backups-i-0573...
  |--@tag_gamesrv_host:
  |  |--b12tf-alpha-gamesrv-host-i-08d3...
  |  |--b12tf-dev-gamesrv-host-i-0b81...
  |  |--b12tf-gold-gamesrv-host-i-004a...
  |  |--b12tf-gold-gamesrv-host-i-0174...
  |  |--b12tf-gold-gamesrv-host-i-0235...
  |  |--b12tf-gold-gamesrv-host-i-02e4...
  |  |--b12tf-gold-gamesrv-host-i-0324...
  |  |--b12tf-gold-gamesrv-host-i-0389...
  |  | ....
  |--@tag_gamesrv_template:
  |  |--b12tf-alpha-gamesrv-template-i-0b35...
  |  |--b12tf-dev-gamesrv-template-i-0e5a...
  ...

I can't find the time to learn the project's requirements and actually check if it is conforming to everything that it needs to, so I'm not filling a pull request, but if you find it useful, feel free to modify&include it as you see fit.

I would be very grateful if some form of this makes it to the next release, so I wouldn't have to maintain the patch on my side.

@gravesm
Copy link
Member

gravesm commented Jan 8, 2021

@vivanov-dp Thank you for posting this. We think #25 may solve this problem but it is not released yet in the current version. If you want you can try this before then and let us know if it solves your problem.

We try to minimize the amount of changes we make to inventory plugins.

waiting_on_info

@gravesm gravesm added inventory inventory plugin and removed needs_triage labels Jan 8, 2021
@vivanov-dp
Copy link
Author

@gravesm Thanks, #25 solves my problem - even though it only allows 2 options to be concatenated, it's enough. I can use it once it gets released.

However, I see somebody commenting in an older linked issue, that they need more than 2 - ansible/ansible#55911 (comment)

@goneri
Copy link
Member

goneri commented Mar 3, 2021

Hi @vivanov-dp,

Thank you for your feedback. I close the issue.

@goneri goneri closed this as completed Mar 3, 2021
abikouo pushed a commit to abikouo/amazon.aws that referenced this issue Sep 18, 2023
* Fully support mixed instance policy

Previously, setting instances_distribution was not supported.
instances_distribution should be supported, to allow users to enable
spot instances within their mixed instance ASGs.

Note: The type and significance of the mixed_instance_policy has
changed. It now captures all of the mixed_instance_policy configuration
parameters, rather than just a list of instance types.

Fixes ansible-collections#231

* Restore mixed_instances_policy backwards-compat

Restore mixed_instances_policy backwards compatibility by using
mixed_instances_policy_full to return full dictionary.

Also, fix some doc typos, add CHANGELOG fragment, and split into
separate test case.

Addresses feedback in ansible-collections#232

* Only return mixed_instances_policy_full if set
abikouo pushed a commit to abikouo/amazon.aws that referenced this issue Sep 18, 2023
* Fully support mixed instance policy

Previously, setting instances_distribution was not supported.
instances_distribution should be supported, to allow users to enable
spot instances within their mixed instance ASGs.

Note: The type and significance of the mixed_instance_policy has
changed. It now captures all of the mixed_instance_policy configuration
parameters, rather than just a list of instance types.

Fixes ansible-collections#231

* Restore mixed_instances_policy backwards-compat

Restore mixed_instances_policy backwards compatibility by using
mixed_instances_policy_full to return full dictionary.

Also, fix some doc typos, add CHANGELOG fragment, and split into
separate test case.

Addresses feedback in ansible-collections#232

* Only return mixed_instances_policy_full if set
abikouo pushed a commit to abikouo/amazon.aws that referenced this issue Oct 24, 2023
* Fully support mixed instance policy

Previously, setting instances_distribution was not supported.
instances_distribution should be supported, to allow users to enable
spot instances within their mixed instance ASGs.

Note: The type and significance of the mixed_instance_policy has
changed. It now captures all of the mixed_instance_policy configuration
parameters, rather than just a list of instance types.

Fixes ansible-collections#231

* Restore mixed_instances_policy backwards-compat

Restore mixed_instances_policy backwards compatibility by using
mixed_instances_policy_full to return full dictionary.

Also, fix some doc typos, add CHANGELOG fragment, and split into
separate test case.

Addresses feedback in ansible-collections#232

* Only return mixed_instances_policy_full if set
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature This issue/PR relates to a feature request has_pr inventory inventory plugin
Projects
None yet
Development

No branches or pull requests

4 participants