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

Hostnames key support in inventory #399

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion examples/inventory/nutanix.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ groups:
keyed_groups:
- prefix: "host"
separator: ':'
key: "ansible_host"
key: "ansible_host"
hostnames:
- status.name | lower # trasforms all inventory name to vm name in lowercase
38 changes: 37 additions & 1 deletion plugins/inventory/ntnx_prism_vm_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function

from ansible.module_utils._text import to_text, to_native
__metaclass__ = type

DOCUMENTATION = r"""
Expand Down Expand Up @@ -61,6 +61,14 @@
type: boolean
env:
- name: VALIDATE_CERTS
hostnames:
description:
- A list of templates in order of precedence to compose inventory_hostname.
- Ignores template if resulted in an empty string or None value.
- You can use property specified in I(properties) as variables in the template.
type: list
elements: string
default: ['status.name']
notes: "null"
requirements: "null"
extends_documentation_fragment:
Expand Down Expand Up @@ -122,6 +130,8 @@ def parse(self, inventory, loader, path, cache=True):
# Determines if composed variables or groups using nonexistent variables is an error
strict = self.get_option("strict")

hostnames = self.get_option('hostnames')

module = Mock_Module(
self.nutanix_hostname,
self.nutanix_port,
Expand All @@ -148,6 +158,8 @@ def parse(self, inventory, loader, path, cache=True):
for entity in resp["entities"]:
cluster = entity["status"]["cluster_reference"]["name"]
vm_name = entity["status"]["name"]
#print("The variable, entity is of type:", type(entity["status"]))
vm_name = self._get_hostname(entity,hostnames,strict=strict)
vm_uuid = entity["metadata"]["uuid"]
vm_ip = None

Expand Down Expand Up @@ -206,3 +218,27 @@ def parse(self, inventory, loader, path, cache=True):
vm_name,
strict=strict,
)

def _get_hostname(self, properties, hostnames, strict=False):
hostname = None
errors = []

for preference in hostnames:
try:
hostname = self._compose(preference, properties)

except Exception as e: # pylint: disable=broad-except
if strict:
raise AnsibleError("Could not compose %s as hostnames - %s" % (preference, to_native(e)))
else:
errors.append(
(preference, str(e))
)
if hostname:
return to_text(hostname)

raise AnsibleError(
'Could not template any hostname for host, errors for each preference: %s' % (
', '.join(['%s: %s' % (pref, err) for pref, err in errors])
)
)