-
Notifications
You must be signed in to change notification settings - Fork 335
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
Make inventory_hostname configurable with hostvar_expressions #105
Make inventory_hostname configurable with hostvar_expressions #105
Conversation
I see some basic Also, is shippable even running? I don't see this PR (or anything else) running at https://app.shippable.com/github/ansible-collections/azure/dashboard |
Can a maintainer review this? I think this would solve a similar issue we are having where we want to be able to pull in hosts by ip address instead of the vm name. |
plugins/inventory/azure_rm.py
Outdated
@@ -138,6 +138,9 @@ | |||
# overrides the default ansible_host value with a custom Jinja2 expression, in this case, the first DNS hostname, or | |||
# if none are found, the first public IP address. | |||
ansible_host: (public_dns_hostnames + public_ipv4_addresses) | first | |||
# overrides the default inventory_hostname value with a custom Jinja2 expression, in this case, a tag called vm_name. | |||
# if the tag is not found, use the default_inventory_hostname (see plain_host_names). | |||
inventory_hostname: tags.vm_name | default(default_inventory_hostname) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not saying that this is a good idea at all, but if you do this, you should at least put an item in the documentation for hostvar_expressions
that explains this feature.
I know that Several collections have some sort of option for doing this (modifying the name as it appears to Ansible, but not the connection name). You'll notice that in every one of these cases, there is a top-level option for the inventory plugin called I would also suggest mimicking the implementation of those other collections to the point of outright copying and pasting where possible. If you want to make it "nicer", you can see my most recent PR to VMWare ansible-collections/community.vmware#253, which is for error handling, and will account for some differences with other examples. |
Thanks for the review. I'll take a look at the interface for changing hostnames in the other inventory plugins. This was the quick and dirty implementation, now let's see if we can get a better interface to provide this feature. Without it, I'm hard-coding my ansible inventory instead of using this plugin. |
6b60f10
to
8b9d554
Compare
K. I copied from the vmware inventory with a few adjustments. And I rebased. |
|
||
for preference in hostnames: | ||
if preference == 'default': | ||
return host.default_inventory_hostname |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 nice handling of existing behavior.
checked out your branch and tried running, error:
applied diff --git a/plugins/inventory/azure_rm.py b/plugins/inventory/azure_rm.py
index 899ba14..9e4050a 100644
--- a/plugins/inventory/azure_rm.py
+++ b/plugins/inventory/azure_rm.py
@@ -198,7 +198,7 @@ from ansible.module_utils.six import iteritems
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMAuth
from ansible.errors import AnsibleParserError, AnsibleError
from ansible.module_utils.parsing.convert_bool import boolean
-from ansible.module_utils._text import to_native, to_bytes
+from ansible.module_utils._text import to_native, to_bytes, to_text
from itertools import chain
from msrest import ServiceClient, Serializer, Deserializer
from msrestazure import AzureConfiguration With that, I applied something similar to your example, confirmed use of custom name from tags for 1 host, rest of hosts still using default. So with that patch 👍 from me. |
8b9d554
to
9a30456
Compare
57c6057
to
3e1c870
Compare
f81ee2d
to
77d83bd
Compare
Rebased on dev. |
I have |
@AlanCoding I think you meant "I have no objections" instead of "I have any objections". Right? |
yes, I have no objections. |
@cognifloyd Recently, I didn’t merge this PR because I was busy with other things. Can you help resolve the conflicting files? After the settlement, we will merge this PR as much as possible. Thank you! |
This allows for hostvar_expressions like: hostvar_expressions: inventory_hostname: tags.vm_name | default(default_inventory_hostname)
77d83bd
to
831b1ef
Compare
rebased |
I see this was merged into version 1.20. Is this actually using hostvar_expressions (inventory_hostname), or is it using hostnames: as a list var? Neither seems to work using latest 1.20:
|
Example given is hostnames:
- tags.vm_name
- default # special var that uses the default hashed name which is similar to your 2nd attempt but not quite the same. The "tags" key should be in hostvars, print with |
SUMMARY
Allow configuring inventory_hostname with a constructable/compose expression in hostvar_expressions.
Currently, you can add inventory_hostname to compose vars, but that is too late, as the hostname is already set. If you try, ansible with override it with a magic var to ensure that the var matches the hostname.
Resolves #104
ISSUE TYPE
COMPONENT NAME
azure_rm inventory plugin
ADDITIONAL INFORMATION
Example of how this could be used with tags: