From 508ebf295b8155c0a04e9303090fab050fe6be37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ne=C4=8Das?= Date: Sat, 11 Jan 2020 03:40:10 +0100 Subject: [PATCH] backport of ovirt_vm template clone check (#65702) * backport of ovirt_vm template clone check * update change log * update change log 2 --- .../65317-ovirt_vm-udpate-template.yml | 2 + lib/ansible/modules/cloud/ovirt/ovirt_vm.py | 47 ++++++++++--------- 2 files changed, 26 insertions(+), 23 deletions(-) create mode 100644 changelogs/fragments/65317-ovirt_vm-udpate-template.yml diff --git a/changelogs/fragments/65317-ovirt_vm-udpate-template.yml b/changelogs/fragments/65317-ovirt_vm-udpate-template.yml new file mode 100644 index 00000000000..42eadde4444 --- /dev/null +++ b/changelogs/fragments/65317-ovirt_vm-udpate-template.yml @@ -0,0 +1,2 @@ +bugfixes: +- "When cloning vm from the template it assigned Blank template to it and when rerun playbook it failed." diff --git a/lib/ansible/modules/cloud/ovirt/ovirt_vm.py b/lib/ansible/modules/cloud/ovirt/ovirt_vm.py index cf1bf7d2d33..036c943ef4f 100644 --- a/lib/ansible/modules/cloud/ovirt/ovirt_vm.py +++ b/lib/ansible/modules/cloud/ovirt/ovirt_vm.py @@ -1271,30 +1271,31 @@ class VmsModule(BaseModule): """ template = None templates_service = self._connection.system_service().templates_service() - if self.param('template'): - clusters_service = self._connection.system_service().clusters_service() - cluster = search_by_name(clusters_service, self.param('cluster')) - data_center = self._connection.follow_link(cluster.data_center) - templates = templates_service.list( - search='name=%s and datacenter=%s' % (self.param('template'), data_center.name) - ) - if self.param('template_version'): - templates = [ - t for t in templates - if t.version.version_number == self.param('template_version') - ] - if not templates: - raise ValueError( - "Template with name '%s' and version '%s' in data center '%s' was not found'" % ( - self.param('template'), - self.param('template_version'), - data_center.name - ) + if self._is_new: + if self.param('template'): + clusters_service = self._connection.system_service().clusters_service() + cluster = search_by_name(clusters_service, self.param('cluster')) + data_center = self._connection.follow_link(cluster.data_center) + templates = templates_service.list( + search='name=%s and datacenter=%s' % (self.param('template'), data_center.name) ) - template = sorted(templates, key=lambda t: t.version.version_number, reverse=True)[0] - elif self._is_new: - # If template isn't specified and VM is about to be created specify default template: - template = templates_service.template_service('00000000-0000-0000-0000-000000000000').get() + if self.param('template_version'): + templates = [ + t for t in templates + if t.version.version_number == self.param('template_version') + ] + if not templates: + raise ValueError( + "Template with name '%s' and version '%s' in data center '%s' was not found'" % ( + self.param('template'), + self.param('template_version'), + data_center.name + ) + ) + template = sorted(templates, key=lambda t: t.version.version_number, reverse=True)[0] + else: + # If template isn't specified and VM is about to be created specify default template: + template = templates_service.template_service('00000000-0000-0000-0000-000000000000').get() return template