|
65 | 65 | description:
|
66 | 66 | - Absolute path of folder to place the virtual machine.
|
67 | 67 | - 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 |
68 | 73 | name:
|
69 | 74 | description:
|
70 | 75 | - Name of the VM to work with.
|
|
149 | 154 | import time
|
150 | 155 | import traceback
|
151 | 156 |
|
| 157 | +import xml.etree.ElementTree as ET |
| 158 | + |
152 | 159 | from threading import Thread
|
153 | 160 |
|
154 | 161 | from ansible.module_utils._text import to_native
|
@@ -509,8 +516,47 @@ def upload(self):
|
509 | 516 | def complete(self):
|
510 | 517 | self.lease.HttpNfcLeaseComplete()
|
511 | 518 |
|
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): |
513 | 555 | facts = {}
|
| 556 | + |
| 557 | + if self.params['inject_ovf_env']: |
| 558 | + self.inject_ovf_env() |
| 559 | + |
514 | 560 | if self.params['power_on']:
|
515 | 561 | task = self.entity.PowerOn()
|
516 | 562 | if self.params['wait']:
|
@@ -546,6 +592,10 @@ def main():
|
546 | 592 | 'folder': {
|
547 | 593 | 'default': None,
|
548 | 594 | },
|
| 595 | + 'inject_ovf_env': { |
| 596 | + 'default': False, |
| 597 | + 'type': 'bool', |
| 598 | + }, |
549 | 599 | 'resource_pool': {
|
550 | 600 | 'default': 'Resources',
|
551 | 601 | },
|
@@ -609,7 +659,7 @@ def main():
|
609 | 659 | deploy_ovf = VMwareDeployOvf(module)
|
610 | 660 | deploy_ovf.upload()
|
611 | 661 | deploy_ovf.complete()
|
612 |
| - facts = deploy_ovf.power_on() |
| 662 | + facts = deploy_ovf.deploy() |
613 | 663 |
|
614 | 664 | module.exit_json(instance=facts, changed=True)
|
615 | 665 |
|
|
0 commit comments