From 1b0b33a1f7e4ed1aa62240fd91e493bd20d88b3b Mon Sep 17 00:00:00 2001 From: volapuk Date: Mon, 7 Feb 2022 15:13:02 +0100 Subject: [PATCH] Implemented function to process hostnames using filters Implemented function to be able to process imported hostnames in the inventory in order to make the list of hosts consistent when several inventory sources are used. For example if you have an inventory source with the hostnames in uppercase, you can convert the nutanix hostnames to uppercase using the following configuration: hostnames: - name.split('.')[0] | upper Function picked up in foreman's inventory. Translated with www.DeepL.com/Translator (free version) --- plugins/inventory/nutanix_vm_inventory.py | 37 ++++++++++++++++++++--- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/plugins/inventory/nutanix_vm_inventory.py b/plugins/inventory/nutanix_vm_inventory.py index a348ea9..50e06b1 100644 --- a/plugins/inventory/nutanix_vm_inventory.py +++ b/plugins/inventory/nutanix_vm_inventory.py @@ -54,6 +54,12 @@ type: boolean env: - name: VALIDATE_CERTS + hostnames: + description: + - A list of templates in order of precedence to compose inventory_hostname. + - If the template results in an empty string or None value it is ignored. + type: list + default: ['name'] ''' try: @@ -64,10 +70,11 @@ import json from ansible.errors import AnsibleError -from ansible.plugins.inventory import BaseInventoryPlugin +from ansible.plugins.inventory import BaseInventoryPlugin, Constructable +from ansible.module_utils._text import to_text -class InventoryModule(BaseInventoryPlugin): +class InventoryModule(BaseInventoryPlugin, Constructable): '''Nutanix VM dynamic invetory parser for ansible''' NAME = 'nutanix.nutanix.nutanix_vm_inventory' @@ -76,6 +83,26 @@ def __init__(self): super(InventoryModule, self).__init__() self.session = None + def _get_hostname(self, properties, hostnames): + hostname = None + errors = [] + + for preference in hostnames: + try: + hostname = self._compose(preference, properties) + except Exception as e: # pylint: disable=broad-except + 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]) + ) + ) + def _get_create_session(self): '''Create session''' if not self.session: @@ -103,14 +130,16 @@ def _build_inventory(self): vars_to_remove = ["disk_list", "vnuma_config", "nic_list", "power_state_mechanism", "host_reference", "serial_port_list", "gpu_list", "storage_config", "boot_config", "guest_customization"] vm_list_resp = self._get_vm_list() + hostnames = self.get_option('hostnames') for entity in vm_list_resp["entities"]: nic_count = 0 cluster = entity["status"]["cluster_reference"]["name"] # self.inventory.add_host(f"{vm_name}-{vm_uuid}") - vm_name = entity["status"]["name"] - vm_uuid = entity["metadata"]["uuid"] + vm_name = self._get_hostname(entity["status"], hostnames) + vm_uuid = entity["metadata"]["uuid"] + # Get VM IP for nics in entity["status"]["resources"]["nic_list"]: if nics["nic_type"] == "NORMAL_NIC" and nic_count == 0: