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

ovirt_vm: add placement_policy_hosts #294

Merged
merged 7 commits into from
Jun 17, 2021
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
minor_changes:
- ovirt_vm - Add placement_policy_hosts (https://github.com/oVirt/ovirt-ansible-collection/pull/294).
53 changes: 46 additions & 7 deletions plugins/modules/ovirt_vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,11 @@
user_migratable:
description:
- "Allow manual migration only."
placement_policy_hosts:
description:
- "List of host names."
type: list
elements: str
ticket:
description:
- "If I(true), in addition return I(remote_vv_file) inside I(vm) dictionary, which contains compatible
Expand Down Expand Up @@ -1253,6 +1258,14 @@
- name: pci_0000_00_08_0
state: present

- name: Add placement policy with multiple hosts
@NAMESPACE@.@[email protected]_vm:
name: myvm
placement_policy: migratable
placement_policy_hosts:
- host1
- host2

- name: Export the VM as OVA
@NAMESPACE@.@[email protected]_vm:
name: myvm
Expand Down Expand Up @@ -1441,6 +1454,19 @@ def __get_snapshot(self):
)
return snap

def __get_placement_policy(self):
hosts = None
if self.param('placement_policy_hosts'):
hosts = [otypes.Host(name=host) for host in self.param('placement_policy_hosts')]
elif self.param('host'):
hosts = [otypes.Host(name=self.param('host'))]
if self.param('placement_policy'):
return otypes.VmPlacementPolicy(
affinity=otypes.VmAffinity(self.param('placement_policy')),
hosts=hosts
)
return None

def __get_cluster(self):
if self.param('cluster') is not None:
return self.param('cluster')
Expand All @@ -1453,6 +1479,7 @@ def build_entity(self):
template = self.__get_template_with_version()
cluster = self.__get_cluster()
snapshot = self.__get_snapshot()
placement_policy = self.__get_placement_policy()
display = self.param('graphical_console') or dict()

disk_attachments = self.__get_storage_domain_and_all_template_disks(template)
Expand Down Expand Up @@ -1575,12 +1602,7 @@ def build_entity(self):
self.param('serial_policy') is not None or
self.param('serial_policy_value') is not None
) else None,
placement_policy=otypes.VmPlacementPolicy(
affinity=otypes.VmAffinity(self.param('placement_policy')),
hosts=[
otypes.Host(name=self.param('host')),
] if self.param('host') else None,
) if self.param('placement_policy') else None,
placement_policy=placement_policy,
soundcard_enabled=self.param('soundcard_enabled'),
display=otypes.Display(
smartcard_enabled=self.param('smartcard_enabled'),
Expand Down Expand Up @@ -1648,6 +1670,22 @@ def check_custom_properties():
return sorted(current) == sorted(passed)
return True

def check_placement_policy():
if self.param('placement_policy'):
hosts = sorted(
map(lambda host: self._connection.follow_link(host).name,
entity.placement_policy.hosts if entity.placement_policy.hosts else [])
)
if self.param('placement_policy_hosts'):
return (
equal(self.param('placement_policy'), str(entity.placement_policy.affinity) if entity.placement_policy else None) and
equal(sorted(self.param('placement_policy_hosts')), hosts)
)
return (
equal(self.param('placement_policy'), str(entity.placement_policy.affinity) if entity.placement_policy else None) and
equal([self.param('host')], hosts)
)

def check_host():
if self.param('host') is not None:
return self.param('host') in [self._connection.follow_link(host).name for host in getattr(entity.placement_policy, 'hosts', None) or []]
Expand All @@ -1666,6 +1704,7 @@ def check_custom_compatibility_version():
check_cpu_pinning() and
check_custom_properties() and
check_host() and
check_placement_policy() and
check_custom_compatibility_version() and
not self.param('cloud_init_persist') and
not self.param('kernel_params_persist') and
Expand Down Expand Up @@ -1704,7 +1743,6 @@ def check_custom_compatibility_version():
equal(self.param('timezone'), getattr(entity.time_zone, 'name', None)) and
equal(self.param('serial_policy'), str(getattr(entity.serial_number, 'policy', None))) and
equal(self.param('serial_policy_value'), getattr(entity.serial_number, 'value', None)) and
equal(self.param('placement_policy'), str(entity.placement_policy.affinity) if entity.placement_policy else None) and
equal(self.param('numa_tune_mode'), str(entity.numa_tune_mode)) and
equal(self.param('rng_device'), str(entity.rng_device.source) if entity.rng_device else None) and
equal(provided_vm_display.get('monitors'), getattr(vm_display, 'monitors', None)) and
Expand Down Expand Up @@ -2509,6 +2547,7 @@ def main():
kvm=dict(type='dict'),
cpu_mode=dict(type='str'),
placement_policy=dict(type='str'),
placement_policy_hosts=dict(type='list', elements='str'),
custom_compatibility_version=dict(type='str'),
ticket=dict(type='bool', default=None),
cpu_pinning=dict(type='list', elements='dict'),
Expand Down