mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-08 06:12:51 +00:00
Reformat everything.
This commit is contained in:
@@ -100,20 +100,20 @@ import os
|
||||
|
||||
|
||||
class InventoryModule(BaseInventoryPlugin, Constructable):
|
||||
NAME = 'community.general.opennebula'
|
||||
NAME = "community.general.opennebula"
|
||||
|
||||
def verify_file(self, path):
|
||||
valid = False
|
||||
if super().verify_file(path):
|
||||
if path.endswith(('opennebula.yaml', 'opennebula.yml')):
|
||||
if path.endswith(("opennebula.yaml", "opennebula.yml")):
|
||||
valid = True
|
||||
return valid
|
||||
|
||||
def _get_connection_info(self):
|
||||
url = self.get_option('api_url')
|
||||
username = self.get_option('api_username')
|
||||
password = self.get_option('api_password')
|
||||
authfile = self.get_option('api_authfile')
|
||||
url = self.get_option("api_url")
|
||||
username = self.get_option("api_username")
|
||||
password = self.get_option("api_password")
|
||||
authfile = self.get_option("api_authfile")
|
||||
|
||||
if not username and not password:
|
||||
if authfile is None:
|
||||
@@ -127,31 +127,31 @@ class InventoryModule(BaseInventoryPlugin, Constructable):
|
||||
except Exception:
|
||||
raise AnsibleError(f"Error occurs when reading ONE_AUTH file at '{authfile}'")
|
||||
|
||||
auth_params = namedtuple('auth', ('url', 'username', 'password'))
|
||||
auth_params = namedtuple("auth", ("url", "username", "password"))
|
||||
|
||||
return auth_params(url=url, username=username, password=password)
|
||||
|
||||
def _get_vm_ipv4(self, vm):
|
||||
nic = vm.TEMPLATE.get('NIC')
|
||||
nic = vm.TEMPLATE.get("NIC")
|
||||
|
||||
if isinstance(nic, dict):
|
||||
nic = [nic]
|
||||
|
||||
for net in nic:
|
||||
if net.get('IP'):
|
||||
return net['IP']
|
||||
if net.get("IP"):
|
||||
return net["IP"]
|
||||
|
||||
return False
|
||||
|
||||
def _get_vm_ipv6(self, vm):
|
||||
nic = vm.TEMPLATE.get('NIC')
|
||||
nic = vm.TEMPLATE.get("NIC")
|
||||
|
||||
if isinstance(nic, dict):
|
||||
nic = [nic]
|
||||
|
||||
for net in nic:
|
||||
if net.get('IP6_GLOBAL'):
|
||||
return net['IP6_GLOBAL']
|
||||
if net.get("IP6_GLOBAL"):
|
||||
return net["IP6_GLOBAL"]
|
||||
|
||||
return False
|
||||
|
||||
@@ -159,7 +159,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable):
|
||||
auth = self._get_connection_info()
|
||||
|
||||
if not (auth.username and auth.password):
|
||||
raise AnsibleError('API Credentials missing. Check OpenNebula inventory file.')
|
||||
raise AnsibleError("API Credentials missing. Check OpenNebula inventory file.")
|
||||
else:
|
||||
one_client = pyone.OneServer(auth.url, session=f"{auth.username}:{auth.password}")
|
||||
|
||||
@@ -181,72 +181,74 @@ class InventoryModule(BaseInventoryPlugin, Constructable):
|
||||
server = vm.USER_TEMPLATE
|
||||
|
||||
labels = []
|
||||
if vm.USER_TEMPLATE.get('LABELS'):
|
||||
labels = [s for s in vm.USER_TEMPLATE.get('LABELS') if s == ',' or s == '-' or s.isalnum() or s.isspace()]
|
||||
labels = ''.join(labels)
|
||||
labels = labels.replace(' ', '_')
|
||||
labels = labels.replace('-', '_')
|
||||
labels = labels.split(',')
|
||||
if vm.USER_TEMPLATE.get("LABELS"):
|
||||
labels = [
|
||||
s for s in vm.USER_TEMPLATE.get("LABELS") if s == "," or s == "-" or s.isalnum() or s.isspace()
|
||||
]
|
||||
labels = "".join(labels)
|
||||
labels = labels.replace(" ", "_")
|
||||
labels = labels.replace("-", "_")
|
||||
labels = labels.split(",")
|
||||
|
||||
# filter by label
|
||||
if label_filter is not None:
|
||||
if label_filter not in labels:
|
||||
continue
|
||||
|
||||
server['name'] = vm.NAME
|
||||
server['id'] = vm.ID
|
||||
if hasattr(vm.HISTORY_RECORDS, 'HISTORY') and vm.HISTORY_RECORDS.HISTORY:
|
||||
server['host'] = vm.HISTORY_RECORDS.HISTORY[-1].HOSTNAME
|
||||
server['LABELS'] = labels
|
||||
server['v4_first_ip'] = self._get_vm_ipv4(vm)
|
||||
server['v6_first_ip'] = self._get_vm_ipv6(vm)
|
||||
server["name"] = vm.NAME
|
||||
server["id"] = vm.ID
|
||||
if hasattr(vm.HISTORY_RECORDS, "HISTORY") and vm.HISTORY_RECORDS.HISTORY:
|
||||
server["host"] = vm.HISTORY_RECORDS.HISTORY[-1].HOSTNAME
|
||||
server["LABELS"] = labels
|
||||
server["v4_first_ip"] = self._get_vm_ipv4(vm)
|
||||
server["v6_first_ip"] = self._get_vm_ipv6(vm)
|
||||
|
||||
result.append(server)
|
||||
|
||||
return result
|
||||
|
||||
def _populate(self):
|
||||
hostname_preference = self.get_option('hostname')
|
||||
group_by_labels = self.get_option('group_by_labels')
|
||||
strict = self.get_option('strict')
|
||||
hostname_preference = self.get_option("hostname")
|
||||
group_by_labels = self.get_option("group_by_labels")
|
||||
strict = self.get_option("strict")
|
||||
|
||||
# Add a top group 'one'
|
||||
self.inventory.add_group(group='all')
|
||||
self.inventory.add_group(group="all")
|
||||
|
||||
filter_by_label = self.get_option('filter_by_label')
|
||||
filter_by_label = self.get_option("filter_by_label")
|
||||
servers = self._retrieve_servers(filter_by_label)
|
||||
for server in servers:
|
||||
server = make_unsafe(server)
|
||||
hostname = server['name']
|
||||
hostname = server["name"]
|
||||
# check for labels
|
||||
if group_by_labels and server['LABELS']:
|
||||
for label in server['LABELS']:
|
||||
if group_by_labels and server["LABELS"]:
|
||||
for label in server["LABELS"]:
|
||||
self.inventory.add_group(group=label)
|
||||
self.inventory.add_host(host=hostname, group=label)
|
||||
|
||||
self.inventory.add_host(host=hostname, group='all')
|
||||
self.inventory.add_host(host=hostname, group="all")
|
||||
|
||||
for attribute, value in server.items():
|
||||
self.inventory.set_variable(hostname, attribute, value)
|
||||
|
||||
if hostname_preference != 'name':
|
||||
self.inventory.set_variable(hostname, 'ansible_host', server[hostname_preference])
|
||||
if hostname_preference != "name":
|
||||
self.inventory.set_variable(hostname, "ansible_host", server[hostname_preference])
|
||||
|
||||
if server.get('SSH_PORT'):
|
||||
self.inventory.set_variable(hostname, 'ansible_port', server['SSH_PORT'])
|
||||
if server.get("SSH_PORT"):
|
||||
self.inventory.set_variable(hostname, "ansible_port", server["SSH_PORT"])
|
||||
|
||||
# handle construcable implementation: get composed variables if any
|
||||
self._set_composite_vars(self.get_option('compose'), server, hostname, strict=strict)
|
||||
self._set_composite_vars(self.get_option("compose"), server, hostname, strict=strict)
|
||||
|
||||
# groups based on jinja conditionals get added to specific groups
|
||||
self._add_host_to_composed_groups(self.get_option('groups'), server, hostname, strict=strict)
|
||||
self._add_host_to_composed_groups(self.get_option("groups"), server, hostname, strict=strict)
|
||||
|
||||
# groups based on variables associated with them in the inventory
|
||||
self._add_host_to_keyed_groups(self.get_option('keyed_groups'), server, hostname, strict=strict)
|
||||
self._add_host_to_keyed_groups(self.get_option("keyed_groups"), server, hostname, strict=strict)
|
||||
|
||||
def parse(self, inventory, loader, path, cache=True):
|
||||
if not HAS_PYONE:
|
||||
raise AnsibleError('OpenNebula Inventory plugin requires pyone to work!')
|
||||
raise AnsibleError("OpenNebula Inventory plugin requires pyone to work!")
|
||||
|
||||
super().parse(inventory, loader, path)
|
||||
self._read_config_data(path=path)
|
||||
|
||||
Reference in New Issue
Block a user