Skip to content

Instantly share code, notes, and snippets.

@sivel
Last active January 15, 2019 17:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sivel/8f50a0e468504ba83d7f6cb032bacc9c to your computer and use it in GitHub Desktop.
Save sivel/8f50a0e468504ba83d7f6cb032bacc9c to your computer and use it in GitHub Desktop.
diff --git a/lib/ansible/modules/cloud/vmware/vmware_deploy_ovf.py b/lib/ansible/modules/cloud/vmware/vmware_deploy_ovf.py
index 6eb690baed..0fe301a301 100644
--- a/lib/ansible/modules/cloud/vmware/vmware_deploy_ovf.py
+++ b/lib/ansible/modules/cloud/vmware/vmware_deploy_ovf.py
@@ -56,6 +56,12 @@ options:
default: thin
description:
- Disk provisioning type.
+ enable_hidden_properties:
+ description:
+ - Enable source properties that are marked as ovf:userConfigurable=false
+ default: "no"
+ type: bool
+ version_added: "2.8"
fail_on_spec_warnings:
description:
- Cause the module to treat OVF Import Spec warnings as errors.
@@ -65,6 +71,10 @@ 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"
name:
description:
- Name of the VM to work with.
@@ -149,6 +159,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
@@ -377,6 +389,10 @@ class VMwareDeployOvf:
spec_params
)
+ if self.params['enable_hidden_properties']:
+ for prop in self.import_spec.importSpec.configSpec.vAppConfig.property:
+ prop.info.userConfigurable = True
+
errors = [to_native(e.msg) for e in getattr(self.import_spec, 'error', [])]
if self.params['fail_on_spec_warnings']:
errors.extend(
@@ -509,8 +525,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 = '<?xml version="1.0" encoding="UTF-8"?>' + 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']:
@@ -543,9 +598,17 @@ def main():
'deployment_option': {
'default': None,
},
+ 'enable_hidden_properties': {
+ 'default': False,
+ 'type': 'bool',
+ },
'folder': {
'default': None,
},
+ 'inject_ovf_env': {
+ 'default': False,
+ 'type': 'bool',
+ },
'resource_pool': {
'default': 'Resources',
},
@@ -609,7 +672,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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment