From 134b77961be61fd5189275f09847b2e3d0d7df5b Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Mon, 28 Jan 2019 09:55:21 -0600 Subject: [PATCH] Add inject_ovf_env functionality for vmware_deploy_ovf (#51074) * Add functionality to set hidden properties. Fixes #50299 * Add inject_ovf_env functionality * Add xml declaration * Revert "Add functionality to set hidden properties. Fixes #50299" This reverts commit 4b41bb75207b5f88573df556cf94cfd64c843e56. * Add changelog fragment * Minor changes Signed-off-by: Abhijeet Kasurde --- .../fragments/vmware-deploy-ovf-inject.yml | 2 + .../modules/cloud/vmware/vmware_deploy_ovf.py | 54 ++++++++++++++++++- 2 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/vmware-deploy-ovf-inject.yml diff --git a/changelogs/fragments/vmware-deploy-ovf-inject.yml b/changelogs/fragments/vmware-deploy-ovf-inject.yml new file mode 100644 index 00000000000..efaf1ffa5ba --- /dev/null +++ b/changelogs/fragments/vmware-deploy-ovf-inject.yml @@ -0,0 +1,2 @@ +minor_changes: +- vmware_deploy_ovf - Add support for 'inject_ovf_env' for injecting user input properties in OVF environment. diff --git a/lib/ansible/modules/cloud/vmware/vmware_deploy_ovf.py b/lib/ansible/modules/cloud/vmware/vmware_deploy_ovf.py index 6eb690baed8..815e7f14303 100644 --- a/lib/ansible/modules/cloud/vmware/vmware_deploy_ovf.py +++ b/lib/ansible/modules/cloud/vmware/vmware_deploy_ovf.py @@ -65,6 +65,11 @@ options: description: - Absolute path of folder to place the virtual machine. - If not specified, defaults to the value of C(datacenter.vmFolder). + inject_ovf_env: + description: + - Force the given properties to be inserted into an OVF Environment and injected through VMware Tools. + version_added: "2.8" + type: bool name: description: - Name of the VM to work with. @@ -149,6 +154,8 @@ import tarfile import time import traceback +import xml.etree.ElementTree as ET + from threading import Thread from ansible.module_utils._text import to_native @@ -509,8 +516,47 @@ class VMwareDeployOvf: def complete(self): self.lease.HttpNfcLeaseComplete() - def power_on(self): + def inject_ovf_env(self): + attrib = { + 'xmlns': 'http://schemas.dmtf.org/ovf/environment/1', + 'xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance', + 'xmlns:oe': 'http://schemas.dmtf.org/ovf/environment/1', + 'xmlns:ve': 'http://www.vmware.com/schema/ovfenv', + 'oe:id': '', + 've:esxId': self.entity._moId + } + env = ET.Element('Environment', **attrib) + + platform = ET.SubElement(env, 'PlatformSection') + ET.SubElement(platform, 'Kind').text = self.si.about.name + ET.SubElement(platform, 'Version').text = self.si.about.version + ET.SubElement(platform, 'Vendor').text = self.si.about.vendor + ET.SubElement(platform, 'Locale').text = 'US' + + prop_section = ET.SubElement(env, 'PropertySection') + for key, value in self.params['properties'].items(): + params = { + 'oe:key': key, + 'oe:value': str(value) if isinstance(value, bool) else value + } + ET.SubElement(prop_section, 'Property', **params) + + opt = vim.option.OptionValue() + opt.key = 'guestinfo.ovfEnv' + opt.value = '' + to_native(ET.tostring(env)) + + config_spec = vim.vm.ConfigSpec() + config_spec.extraConfig = [opt] + + task = self.entity.ReconfigVM_Task(config_spec) + wait_for_task(task) + + def deploy(self): facts = {} + + if self.params['inject_ovf_env']: + self.inject_ovf_env() + if self.params['power_on']: task = self.entity.PowerOn() if self.params['wait']: @@ -546,6 +592,10 @@ def main(): 'folder': { 'default': None, }, + 'inject_ovf_env': { + 'default': False, + 'type': 'bool', + }, 'resource_pool': { 'default': 'Resources', }, @@ -609,7 +659,7 @@ def main(): deploy_ovf = VMwareDeployOvf(module) deploy_ovf.upload() deploy_ovf.complete() - facts = deploy_ovf.power_on() + facts = deploy_ovf.deploy() module.exit_json(instance=facts, changed=True)