From 5bde2d7f1df2700c2458aa05aca402d9bd9572db Mon Sep 17 00:00:00 2001 From: mnecas Date: Thu, 10 Jun 2021 17:49:44 +0200 Subject: [PATCH 1/7] add placement_policy_dict --- plugins/modules/ovirt_vm.py | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/plugins/modules/ovirt_vm.py b/plugins/modules/ovirt_vm.py index 80593efb..d955d090 100644 --- a/plugins/modules/ovirt_vm.py +++ b/plugins/modules/ovirt_vm.py @@ -700,6 +700,22 @@ user_migratable: description: - "Allow manual migration only." + placement_policy_dict: + description: + - "The configuration of the virtual machine's placement policy." + - "If no value is passed, default value is set by oVirt/RHV engine." + - "Placement policy can be one of the following values:" + type: dict + suboptions: + affinity: + description: + - "affinity. Options: I(migratable), I(pinned) or I(user_migratable)." + type: str + 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 @@ -1579,8 +1595,14 @@ def build_entity(self): affinity=otypes.VmAffinity(self.param('placement_policy')), hosts=[ otypes.Host(name=self.param('host')), - ] if self.param('host') else None, + ] if self.param('host') and not self.param('placement_policy_dict') else None, ) if self.param('placement_policy') else None, + placement_policy=otypes.VmPlacementPolicy( + affinity=otypes.VmAffinity(self.param('placement_policy_dict').get('affinity')), + hosts=[ + otypes.Host(name=host) for host in self.param('placement_policy_dict').get('hosts'), + ] if self.param('placement_policy_dict').get('hosts') else None, + ) if self.param('placement_policy_dict') else None, soundcard_enabled=self.param('soundcard_enabled'), display=otypes.Display( smartcard_enabled=self.param('smartcard_enabled'), @@ -2509,6 +2531,13 @@ def main(): kvm=dict(type='dict'), cpu_mode=dict(type='str'), placement_policy=dict(type='str'), + placement_policy_dict=dict( + type='dict', + options=dict( + hosts=dict(type='list', elements='str'), + affinity=dict(type='str'), + ) + ), custom_compatibility_version=dict(type='str'), ticket=dict(type='bool', default=None), cpu_pinning=dict(type='list', elements='dict'), From d881fc19cad11175dece6cf5c626cb1a8373c2cd Mon Sep 17 00:00:00 2001 From: mnecas Date: Fri, 11 Jun 2021 13:13:16 +0200 Subject: [PATCH 2/7] add check placment_policy --- plugins/modules/ovirt_vm.py | 65 ++++++++++++++++++++++++++++--------- 1 file changed, 49 insertions(+), 16 deletions(-) diff --git a/plugins/modules/ovirt_vm.py b/plugins/modules/ovirt_vm.py index d955d090..5add53c0 100644 --- a/plugins/modules/ovirt_vm.py +++ b/plugins/modules/ovirt_vm.py @@ -704,12 +704,12 @@ description: - "The configuration of the virtual machine's placement policy." - "If no value is passed, default value is set by oVirt/RHV engine." - - "Placement policy can be one of the following values:" + - "This parameter replaces C(placement_policy), it allows to specify more hosts." type: dict suboptions: affinity: description: - - "affinity. Options: I(migratable), I(pinned) or I(user_migratable)." + - "Affinity name. Options: I(migratable), I(pinned) or I(user_migratable)." type: str hosts: description: @@ -1269,6 +1269,15 @@ - name: pci_0000_00_08_0 state: present +- name: Add placement policy with multiple hosts + @NAMESPACE@.@NAME@.ovirt_vm: + name: myvm + placement_policy_dict: + affinity: migratable + hosts: + - host1 + - host2 + - name: Export the VM as OVA @NAMESPACE@.@NAME@.ovirt_vm: name: myvm @@ -1457,6 +1466,26 @@ def __get_snapshot(self): ) return snap + def __get_placement_policy(self): + + if self.param('placement_policy_dict'): + return otypes.VmPlacementPolicy( + affinity=otypes.VmAffinity(self.param('placement_policy_dict').get('affinity')), + hosts=[ + otypes.Host(name=host) for host in self.param('placement_policy_dict').get('hosts',[]) + ] if self.param('placement_policy_dict').get('hosts') else None + ) + + if self.param('placement_policy'): + return otypes.VmPlacementPolicy( + affinity=otypes.VmAffinity(self.param('placement_policy')), + hosts=[ + otypes.Host(name=self.param('host')) + ] if self.param('host') else None + ) + + return None + def __get_cluster(self): if self.param('cluster') is not None: return self.param('cluster') @@ -1469,6 +1498,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) @@ -1591,18 +1621,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') and not self.param('placement_policy_dict') else None, - ) if self.param('placement_policy') else None, - placement_policy=otypes.VmPlacementPolicy( - affinity=otypes.VmAffinity(self.param('placement_policy_dict').get('affinity')), - hosts=[ - otypes.Host(name=host) for host in self.param('placement_policy_dict').get('hosts'), - ] if self.param('placement_policy_dict').get('hosts') else None, - ) if self.param('placement_policy_dict') else None, + placement_policy=placement_policy, soundcard_enabled=self.param('soundcard_enabled'), display=otypes.Display( smartcard_enabled=self.param('smartcard_enabled'), @@ -1670,6 +1689,20 @@ def check_custom_properties(): return sorted(current) == sorted(passed) return True + def check_placement_policy(): + hosts = sorted(map(lambda host: self._connection.follow_link(host).name, entity.placement_policy.hosts)) + + if self.param('placement_policy_dict'): + return ( + equal(self.param('placement_policy_dict').get('affinity'), str(entity.placement_policy.affinity) if entity.placement_policy else None) and + equal(sorted(self.param('placement_policy_dict').get('hosts')), hosts) + ) + if self.param('placement_policy'): + 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 []] @@ -1680,7 +1713,7 @@ def check_custom_compatibility_version(): return (self._get_minor(self.param('custom_compatibility_version')) == self._get_minor(entity.custom_compatibility_version) and self._get_major(self.param('custom_compatibility_version')) == self._get_major(entity.custom_compatibility_version)) return True - + check_placement_policy() cpu_mode = getattr(entity.cpu, 'mode') vm_display = entity.display provided_vm_display = self.param('graphical_console') or dict() @@ -1688,6 +1721,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 @@ -1726,7 +1760,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 From 451e4c4db526ea297a4e257cd600fb17c7c087ca Mon Sep 17 00:00:00 2001 From: mnecas Date: Fri, 11 Jun 2021 13:16:53 +0200 Subject: [PATCH 3/7] add changelog --- .../fragments/294-ovirt_vm-add-placement_policy_dict.yml | 3 +++ plugins/modules/ovirt_vm.py | 6 +----- 2 files changed, 4 insertions(+), 5 deletions(-) create mode 100644 changelogs/fragments/294-ovirt_vm-add-placement_policy_dict.yml diff --git a/changelogs/fragments/294-ovirt_vm-add-placement_policy_dict.yml b/changelogs/fragments/294-ovirt_vm-add-placement_policy_dict.yml new file mode 100644 index 00000000..77d3d3d6 --- /dev/null +++ b/changelogs/fragments/294-ovirt_vm-add-placement_policy_dict.yml @@ -0,0 +1,3 @@ +--- +minor_changes: + - ovirt_vm - Add placement_policy_dict (https://github.com/oVirt/ovirt-ansible-collection/pull/294). diff --git a/plugins/modules/ovirt_vm.py b/plugins/modules/ovirt_vm.py index 5add53c0..21cbcac0 100644 --- a/plugins/modules/ovirt_vm.py +++ b/plugins/modules/ovirt_vm.py @@ -1467,7 +1467,6 @@ def __get_snapshot(self): return snap def __get_placement_policy(self): - if self.param('placement_policy_dict'): return otypes.VmPlacementPolicy( affinity=otypes.VmAffinity(self.param('placement_policy_dict').get('affinity')), @@ -1475,7 +1474,6 @@ def __get_placement_policy(self): otypes.Host(name=host) for host in self.param('placement_policy_dict').get('hosts',[]) ] if self.param('placement_policy_dict').get('hosts') else None ) - if self.param('placement_policy'): return otypes.VmPlacementPolicy( affinity=otypes.VmAffinity(self.param('placement_policy')), @@ -1483,7 +1481,6 @@ def __get_placement_policy(self): otypes.Host(name=self.param('host')) ] if self.param('host') else None ) - return None def __get_cluster(self): @@ -1691,7 +1688,6 @@ def check_custom_properties(): def check_placement_policy(): hosts = sorted(map(lambda host: self._connection.follow_link(host).name, entity.placement_policy.hosts)) - if self.param('placement_policy_dict'): return ( equal(self.param('placement_policy_dict').get('affinity'), str(entity.placement_policy.affinity) if entity.placement_policy else None) and @@ -1713,7 +1709,7 @@ def check_custom_compatibility_version(): return (self._get_minor(self.param('custom_compatibility_version')) == self._get_minor(entity.custom_compatibility_version) and self._get_major(self.param('custom_compatibility_version')) == self._get_major(entity.custom_compatibility_version)) return True - check_placement_policy() + cpu_mode = getattr(entity.cpu, 'mode') vm_display = entity.display provided_vm_display = self.param('graphical_console') or dict() From 0512e1abe3a1ece7e52269a9871aae6c3bf53c0a Mon Sep 17 00:00:00 2001 From: mnecas Date: Fri, 11 Jun 2021 13:32:09 +0200 Subject: [PATCH 4/7] use placement_policy_hosts instead of dict --- ...294-ovirt_vm-add-placement_policy_dict.yml | 3 - ...94-ovirt_vm-add-placement_policy_hosts.yml | 3 + plugins/modules/ovirt_vm.py | 68 +++++++------------ 3 files changed, 26 insertions(+), 48 deletions(-) delete mode 100644 changelogs/fragments/294-ovirt_vm-add-placement_policy_dict.yml create mode 100644 changelogs/fragments/294-ovirt_vm-add-placement_policy_hosts.yml diff --git a/changelogs/fragments/294-ovirt_vm-add-placement_policy_dict.yml b/changelogs/fragments/294-ovirt_vm-add-placement_policy_dict.yml deleted file mode 100644 index 77d3d3d6..00000000 --- a/changelogs/fragments/294-ovirt_vm-add-placement_policy_dict.yml +++ /dev/null @@ -1,3 +0,0 @@ ---- -minor_changes: - - ovirt_vm - Add placement_policy_dict (https://github.com/oVirt/ovirt-ansible-collection/pull/294). diff --git a/changelogs/fragments/294-ovirt_vm-add-placement_policy_hosts.yml b/changelogs/fragments/294-ovirt_vm-add-placement_policy_hosts.yml new file mode 100644 index 00000000..0be81fbd --- /dev/null +++ b/changelogs/fragments/294-ovirt_vm-add-placement_policy_hosts.yml @@ -0,0 +1,3 @@ +--- +minor_changes: + - ovirt_vm - Add placement_policy_hosts (https://github.com/oVirt/ovirt-ansible-collection/pull/294). diff --git a/plugins/modules/ovirt_vm.py b/plugins/modules/ovirt_vm.py index 21cbcac0..a2915c28 100644 --- a/plugins/modules/ovirt_vm.py +++ b/plugins/modules/ovirt_vm.py @@ -700,22 +700,11 @@ user_migratable: description: - "Allow manual migration only." - placement_policy_dict: + placement_policy_hosts: description: - - "The configuration of the virtual machine's placement policy." - - "If no value is passed, default value is set by oVirt/RHV engine." - - "This parameter replaces C(placement_policy), it allows to specify more hosts." - type: dict - suboptions: - affinity: - description: - - "Affinity name. Options: I(migratable), I(pinned) or I(user_migratable)." - type: str - hosts: - description: - - "List of host names." - type: list - elements: str + - "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 @@ -1272,11 +1261,10 @@ - name: Add placement policy with multiple hosts @NAMESPACE@.@NAME@.ovirt_vm: name: myvm - placement_policy_dict: - affinity: migratable - hosts: - - host1 - - host2 + placement_policy: migratable + placement_policy_hosts: + - host1 + - host2 - name: Export the VM as OVA @NAMESPACE@.@NAME@.ovirt_vm: @@ -1467,19 +1455,15 @@ def __get_snapshot(self): return snap def __get_placement_policy(self): - if self.param('placement_policy_dict'): - return otypes.VmPlacementPolicy( - affinity=otypes.VmAffinity(self.param('placement_policy_dict').get('affinity')), - hosts=[ - otypes.Host(name=host) for host in self.param('placement_policy_dict').get('hosts',[]) - ] if self.param('placement_policy_dict').get('hosts') else None - ) - if self.param('placement_policy'): + hosts = None + if self.param('placement_policy_hosts'): + hosts = [otypes.Host(name=host) for host in self.param('placement_policy_hosts')] + elif if 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=[ - otypes.Host(name=self.param('host')) - ] if self.param('host') else None + hosts=hosts ) return None @@ -1687,16 +1671,16 @@ def check_custom_properties(): return True def check_placement_policy(): - hosts = sorted(map(lambda host: self._connection.follow_link(host).name, entity.placement_policy.hosts)) - if self.param('placement_policy_dict'): - return ( - equal(self.param('placement_policy_dict').get('affinity'), str(entity.placement_policy.affinity) if entity.placement_policy else None) and - equal(sorted(self.param('placement_policy_dict').get('hosts')), hosts) - ) if self.param('placement_policy'): + hosts = sorted(map(lambda host: self._connection.follow_link(host).name, entity.placement_policy.hosts)) + 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) + equal([self.param('host')], hosts) and ) def check_host(): @@ -2560,13 +2544,7 @@ def main(): kvm=dict(type='dict'), cpu_mode=dict(type='str'), placement_policy=dict(type='str'), - placement_policy_dict=dict( - type='dict', - options=dict( - hosts=dict(type='list', elements='str'), - affinity=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'), From 22e0122b8e303663006f669a85a4d77337321d8e Mon Sep 17 00:00:00 2001 From: mnecas Date: Fri, 11 Jun 2021 13:45:24 +0200 Subject: [PATCH 5/7] fix pep8 --- plugins/modules/ovirt_vm.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/plugins/modules/ovirt_vm.py b/plugins/modules/ovirt_vm.py index a2915c28..fe6c2c1d 100644 --- a/plugins/modules/ovirt_vm.py +++ b/plugins/modules/ovirt_vm.py @@ -1458,9 +1458,9 @@ 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 if self.param('host'): + elif self.param('host'): hosts = [otypes.Host(name=self.param('host'))] - if self.param('placement_policy') + if self.param('placement_policy'): return otypes.VmPlacementPolicy( affinity=otypes.VmAffinity(self.param('placement_policy')), hosts=hosts @@ -1672,7 +1672,7 @@ def check_custom_properties(): def check_placement_policy(): if self.param('placement_policy'): - hosts = sorted(map(lambda host: self._connection.follow_link(host).name, entity.placement_policy.hosts)) + 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 @@ -1680,7 +1680,7 @@ def check_placement_policy(): ) return ( equal(self.param('placement_policy'), str(entity.placement_policy.affinity) if entity.placement_policy else None) and - equal([self.param('host')], hosts) and + equal([self.param('host')], hosts) ) def check_host(): From 1c235942b3afebbbdfcd369a192c48056abab5e1 Mon Sep 17 00:00:00 2001 From: mnecas Date: Fri, 11 Jun 2021 13:57:18 +0200 Subject: [PATCH 6/7] fix pep8 --- plugins/modules/ovirt_vm.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/plugins/modules/ovirt_vm.py b/plugins/modules/ovirt_vm.py index fe6c2c1d..6bf07d65 100644 --- a/plugins/modules/ovirt_vm.py +++ b/plugins/modules/ovirt_vm.py @@ -1672,7 +1672,9 @@ def check_custom_properties(): 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 [])) + 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 From 024942db95f221c8a215f674bc9ca82df7ea4f2a Mon Sep 17 00:00:00 2001 From: mnecas Date: Fri, 11 Jun 2021 14:05:21 +0200 Subject: [PATCH 7/7] fix pep8 2 --- plugins/modules/ovirt_vm.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/plugins/modules/ovirt_vm.py b/plugins/modules/ovirt_vm.py index 6bf07d65..27667096 100644 --- a/plugins/modules/ovirt_vm.py +++ b/plugins/modules/ovirt_vm.py @@ -1672,8 +1672,9 @@ def check_custom_properties(): 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 []) + 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 (