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

Implemented function to process hostnames using filters #40

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
37 changes: 33 additions & 4 deletions plugins/inventory/nutanix_vm_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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'
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down