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

Please allow me to generate the inventory_hostname #104

Closed
cognifloyd opened this issue May 5, 2020 · 3 comments · Fixed by #105
Closed

Please allow me to generate the inventory_hostname #104

cognifloyd opened this issue May 5, 2020 · 3 comments · Fixed by #105
Labels
inventory plugin/Inventory/azure_rm.py related issues medium_priority Medium priority

Comments

@cognifloyd
Copy link
Contributor

SUMMARY

I would like to define my own formula for generating the inventory_hostname. As Azure does not allow renaming VMs, I need to use a tag to get our names for the VM. I want to use our standardize tag name so that I can add vars in host_vars/ as required.

ISSUE TYPE
  • Feature Idea
COMPONENT NAME

azure_rm inventory plugin

ADDITIONAL INFORMATION

This is a continuation of ansible/ansible#54065 which only requested the ability to configure the legacy behavior.

I want to define my own inventory_hostname as mentiond here:

def _get_hostname(self, host):
# FUTURE: configurable hostname sources
return host.default_inventory_hostname

For reference, this is the section that currently defines the legacy/unique hostname definition:

if legacy_name:
self.default_inventory_hostname = vm_model['name']
else:
# Azure often doesn't provide a globally-unique filename, so use resource name + a chunk of ID hash
self.default_inventory_hostname = '{0}_{1}'.format(vm_model['name'], hashlib.sha1(to_bytes(vm_model['id'])).hexdigest()[0:4])

In both cases, this uses vm_model['name'] but I explicitly do NOT want to use vm_model['name'] because Azure doesn't let me rename VMs.

@cognifloyd
Copy link
Contributor Author

cognifloyd commented May 5, 2020

Maybe one way to do this, would be to allow adding inventory_hostname to hostvar_expressions:

constructable_config_compose = self.get_option('hostvar_expressions')

And do something like:

compose_inventory_hostname = constructable_config_compose.pop("inventory_hostname")

And pass that to get_hostname here:

inventory_hostname = self._get_hostname(h)

inventory_hostname = self._get_hostname(h, compose_inventory_hostname, strict=constructable_config_strict)

Then _get_hostname would change to something like this:

def _get_hostname(self, host, compose=None, strict=False):
    if not compose:
        return host.default_inventory_hostname
    try:
        return self._compose(compose, h.hostvars)
    except Exception as e:
        if strict:
            raise AnsibleParserError("Could not generate inventory_hostname for host %s from hostvar_expressions.inventory_hostname: %s" % (host.default_inventory_hostname, to_native(e)))
        return host.default_inventory_hostname

Any comments on doing it like this? Or would using something other than hostvar_expressions be preferrable?

@mwinfie
Copy link

mwinfie commented Jun 22, 2020

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.

cognifloyd added a commit to theatro/ansible-collections-azure that referenced this issue Jun 26, 2020
@Fred-sun
Copy link
Collaborator

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

This PR has been reviewed and will be merged soon. Thank you!

@Fred-sun Fred-sun added inventory plugin/Inventory/azure_rm.py related issues medium_priority Medium priority labels Sep 14, 2020
cognifloyd added a commit to theatro/ansible-collections-azure that referenced this issue Sep 14, 2020
cognifloyd added a commit to theatro/ansible-collections-azure that referenced this issue Sep 29, 2020
haiyuazhang pushed a commit that referenced this issue Sep 30, 2020
* Make inventory_hostname configurable with hostvar_expressions

Resolves #104

* inventory: add default_inventory_hostname to hostvars

This allows for hostvar_expressions like:

hostvar_expressions:
  inventory_hostname: tags.vm_name | default(default_inventory_hostname)

* inventory: document overriding default_inventory_hostname

* Use hostnames instead of hostvar_expressions

* Move azure_rm hostnames param documentation to doc_fragment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
inventory plugin/Inventory/azure_rm.py related issues medium_priority Medium priority
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants