Skip to content

Commit 134b779

Browse files
authored
Add inject_ovf_env functionality for vmware_deploy_ovf (ansible#51074)
* Add functionality to set hidden properties. Fixes ansible#50299 * Add inject_ovf_env functionality * Add xml declaration * Revert "Add functionality to set hidden properties. Fixes ansible#50299" This reverts commit 4b41bb7. * Add changelog fragment * Minor changes Signed-off-by: Abhijeet Kasurde <[email protected]>
1 parent 55f0cfb commit 134b779

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
minor_changes:
2+
- vmware_deploy_ovf - Add support for 'inject_ovf_env' for injecting user input properties in OVF environment.

lib/ansible/modules/cloud/vmware/vmware_deploy_ovf.py

+52-2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@
6565
description:
6666
- Absolute path of folder to place the virtual machine.
6767
- If not specified, defaults to the value of C(datacenter.vmFolder).
68+
inject_ovf_env:
69+
description:
70+
- Force the given properties to be inserted into an OVF Environment and injected through VMware Tools.
71+
version_added: "2.8"
72+
type: bool
6873
name:
6974
description:
7075
- Name of the VM to work with.
@@ -149,6 +154,8 @@
149154
import time
150155
import traceback
151156

157+
import xml.etree.ElementTree as ET
158+
152159
from threading import Thread
153160

154161
from ansible.module_utils._text import to_native
@@ -509,8 +516,47 @@ def upload(self):
509516
def complete(self):
510517
self.lease.HttpNfcLeaseComplete()
511518

512-
def power_on(self):
519+
def inject_ovf_env(self):
520+
attrib = {
521+
'xmlns': 'http://schemas.dmtf.org/ovf/environment/1',
522+
'xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance',
523+
'xmlns:oe': 'http://schemas.dmtf.org/ovf/environment/1',
524+
'xmlns:ve': 'http://www.vmware.com/schema/ovfenv',
525+
'oe:id': '',
526+
've:esxId': self.entity._moId
527+
}
528+
env = ET.Element('Environment', **attrib)
529+
530+
platform = ET.SubElement(env, 'PlatformSection')
531+
ET.SubElement(platform, 'Kind').text = self.si.about.name
532+
ET.SubElement(platform, 'Version').text = self.si.about.version
533+
ET.SubElement(platform, 'Vendor').text = self.si.about.vendor
534+
ET.SubElement(platform, 'Locale').text = 'US'
535+
536+
prop_section = ET.SubElement(env, 'PropertySection')
537+
for key, value in self.params['properties'].items():
538+
params = {
539+
'oe:key': key,
540+
'oe:value': str(value) if isinstance(value, bool) else value
541+
}
542+
ET.SubElement(prop_section, 'Property', **params)
543+
544+
opt = vim.option.OptionValue()
545+
opt.key = 'guestinfo.ovfEnv'
546+
opt.value = '<?xml version="1.0" encoding="UTF-8"?>' + to_native(ET.tostring(env))
547+
548+
config_spec = vim.vm.ConfigSpec()
549+
config_spec.extraConfig = [opt]
550+
551+
task = self.entity.ReconfigVM_Task(config_spec)
552+
wait_for_task(task)
553+
554+
def deploy(self):
513555
facts = {}
556+
557+
if self.params['inject_ovf_env']:
558+
self.inject_ovf_env()
559+
514560
if self.params['power_on']:
515561
task = self.entity.PowerOn()
516562
if self.params['wait']:
@@ -546,6 +592,10 @@ def main():
546592
'folder': {
547593
'default': None,
548594
},
595+
'inject_ovf_env': {
596+
'default': False,
597+
'type': 'bool',
598+
},
549599
'resource_pool': {
550600
'default': 'Resources',
551601
},
@@ -609,7 +659,7 @@ def main():
609659
deploy_ovf = VMwareDeployOvf(module)
610660
deploy_ovf.upload()
611661
deploy_ovf.complete()
612-
facts = deploy_ovf.power_on()
662+
facts = deploy_ovf.deploy()
613663

614664
module.exit_json(instance=facts, changed=True)
615665

0 commit comments

Comments
 (0)