1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Xen orchestra inventory: Added groups, keyed_groups and compose support (#3766)

* Xen orchestra inventory: Added groups, keyed_groups and compose support

* Update plugins/inventory/xen_orchestra.py

Remove extra params declaration

Co-authored-by: Felix Fontein <felix@fontein.de>

Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
Samori Gorse 2021-11-22 13:53:25 +01:00 committed by GitHub
parent 25e80762aa
commit 336f9465cb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 1 deletions

View file

@ -69,6 +69,11 @@ simple_config_file:
password: xo_pwd password: xo_pwd
validate_certs: true validate_certs: true
use_ssl: true use_ssl: true
groups:
kube_nodes: "'kube_node' in tags"
compose:
ansible_port: 2222
''' '''
import json import json
@ -165,6 +170,12 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
'hosts': self.get_object('host'), 'hosts': self.get_object('host'),
} }
def _apply_constructable(self, name, variables):
strict = self.get_option('strict')
self._add_host_to_composed_groups(self.get_option('groups'), variables, name, strict=strict)
self._add_host_to_keyed_groups(self.get_option('keyed_groups'), variables, name, strict=strict)
self._set_composite_vars(self.get_option('compose'), variables, name, strict=strict)
def _add_vms(self, vms, hosts, pools): def _add_vms(self, vms, hosts, pools):
for uuid, vm in vms.items(): for uuid, vm in vms.items():
group = 'with_ip' group = 'with_ip'
@ -213,6 +224,8 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
self.inventory.set_variable( self.inventory.set_variable(
entry_name, 'os_version', vm['os_version']) entry_name, 'os_version', vm['os_version'])
self._apply_constructable(entry_name, self.inventory.get_host(entry_name).get_vars())
def _add_hosts(self, hosts, pools): def _add_hosts(self, hosts, pools):
for host in hosts.values(): for host in hosts.values():
entry_name = host['uuid'] entry_name = host['uuid']

View file

@ -129,6 +129,19 @@ objects = {
} }
def get_option(option):
if option == 'groups':
return {}
elif option == 'keyed_groups':
return []
elif option == 'compose':
return {}
elif option == 'strict':
return False
else:
return None
def serialize_groups(groups): def serialize_groups(groups):
return list(map(str, groups)) return list(map(str, groups))
@ -144,7 +157,8 @@ def test_verify_file_bad_config(inventory):
assert inventory.verify_file('foobar.xen_orchestra.yml') is False assert inventory.verify_file('foobar.xen_orchestra.yml') is False
def test_populate(inventory): def test_populate(inventory, mocker):
inventory.get_option = mocker.MagicMock(side_effect=get_option)
inventory._populate(objects) inventory._populate(objects)
actual = sorted(inventory.inventory.hosts.keys()) actual = sorted(inventory.inventory.hosts.keys())
expected = sorted(['c96ec4dd-28ac-4df4-b73c-4371bd202728', '222d8594-9426-468a-ad69-7a6f02330fa3', expected = sorted(['c96ec4dd-28ac-4df4-b73c-4371bd202728', '222d8594-9426-468a-ad69-7a6f02330fa3',