2017-11-10 06:07:30 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2018-03-02 00:07:17 +00:00
|
|
|
# Copyright: (c) 2017, IBM Corp
|
2017-11-10 06:07:30 +00:00
|
|
|
# Author(s): Andreas Nafpliotis <nafpliot@de.ibm.com>
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
|
|
|
'status': ['preview'],
|
|
|
|
'supported_by': 'community'}
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: vmware_cfg_backup
|
|
|
|
short_description: Backup / Restore / Reset ESXi host configuration
|
|
|
|
description:
|
2018-02-05 19:42:49 +00:00
|
|
|
- This module can be used to perform various operations related to backup, restore and reset of ESXi host configuration.
|
2017-11-10 06:07:30 +00:00
|
|
|
version_added: "2.5"
|
|
|
|
author:
|
|
|
|
- Andreas Nafpliotis (@nafpliot-ibm)
|
|
|
|
notes:
|
|
|
|
- Tested on ESXi 6.0
|
|
|
|
- Works only for ESXi hosts
|
|
|
|
- For configuration save or reset, the host will be switched automatically to maintenance mode.
|
|
|
|
requirements:
|
|
|
|
- "python >= 2.6"
|
|
|
|
- PyVmomi installed
|
|
|
|
options:
|
2018-02-05 19:42:49 +00:00
|
|
|
esxi_hostname:
|
|
|
|
description:
|
2018-04-13 08:51:37 +00:00
|
|
|
- Name of ESXi server. This is required only if authentication against a vCenter is done.
|
2018-02-05 19:42:49 +00:00
|
|
|
required: False
|
2017-11-10 06:07:30 +00:00
|
|
|
dest:
|
|
|
|
description:
|
|
|
|
- The destination where the ESXi configuration bundle will be saved. The I(dest) can be a folder or a file.
|
|
|
|
- If I(dest) is a folder, the backup file will be saved in the folder with the default filename generated from the ESXi server.
|
|
|
|
- If I(dest) is a file, the backup file will be saved with that filename. The file extension will always be .tgz.
|
|
|
|
src:
|
|
|
|
description:
|
2018-02-05 19:42:49 +00:00
|
|
|
- The file containing the ESXi configuration that will be restored.
|
2017-11-10 06:07:30 +00:00
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- If C(saved), the .tgz backup bundle will be saved in I(dest).
|
2018-02-05 19:42:49 +00:00
|
|
|
- If C(absent), the host configuration will be reset to default values.
|
2017-11-10 06:07:30 +00:00
|
|
|
- If C(loaded), the backup file in I(src) will be loaded to the ESXi host rewriting the hosts settings.
|
|
|
|
choices: [saved, absent, loaded]
|
|
|
|
extends_documentation_fragment: vmware.documentation
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2018-08-08 11:44:27 +00:00
|
|
|
- name: Save the ESXi configuration locally by authenticating directly against the ESXi host
|
2018-08-01 03:40:57 +00:00
|
|
|
vmware_cfg_backup:
|
2018-08-08 11:44:27 +00:00
|
|
|
hostname: '{{ esxi_hostname }}'
|
|
|
|
username: '{{ esxi_username }}'
|
|
|
|
password: '{{ esxi_password }}'
|
2018-08-01 03:40:57 +00:00
|
|
|
state: saved
|
|
|
|
dest: /tmp/
|
|
|
|
delegate_to: localhost
|
2018-02-05 19:42:49 +00:00
|
|
|
|
2018-08-08 11:44:27 +00:00
|
|
|
- name: Save the ESXi configuration locally by authenticating against the vCenter and selecting the ESXi host
|
2018-08-01 03:40:57 +00:00
|
|
|
vmware_cfg_backup:
|
2018-08-08 11:44:27 +00:00
|
|
|
hostname: '{{ vcenter_hostname }}'
|
|
|
|
esxi_hostname: '{{ esxi_hostname }}'
|
|
|
|
username: '{{ esxi_username }}'
|
|
|
|
password: '{{ esxi_password }}'
|
2018-08-01 03:40:57 +00:00
|
|
|
state: saved
|
|
|
|
dest: /tmp/
|
|
|
|
delegate_to: localhost
|
2017-11-10 06:07:30 +00:00
|
|
|
'''
|
|
|
|
|
|
|
|
RETURN = '''
|
|
|
|
dest_file:
|
|
|
|
description: The full path of where the file holding the ESXi configurations was stored
|
|
|
|
returned: changed
|
|
|
|
type: string
|
|
|
|
sample: /tmp/configBundle-esxi.host.domain.tgz
|
|
|
|
'''
|
|
|
|
|
|
|
|
import os
|
|
|
|
try:
|
2018-08-09 05:04:55 +00:00
|
|
|
from pyVmomi import vim
|
2017-11-10 06:07:30 +00:00
|
|
|
except ImportError:
|
2018-02-05 19:42:49 +00:00
|
|
|
pass
|
2017-11-10 06:07:30 +00:00
|
|
|
|
2018-02-05 19:42:49 +00:00
|
|
|
from ansible.module_utils.vmware import vmware_argument_spec, get_all_objs, wait_for_task, PyVmomi
|
2017-11-10 06:07:30 +00:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
from ansible.module_utils.urls import open_url
|
|
|
|
from ansible.module_utils.six.moves.urllib.error import HTTPError
|
2018-02-05 19:42:49 +00:00
|
|
|
from ansible.module_utils._text import to_native
|
2017-11-10 06:07:30 +00:00
|
|
|
|
|
|
|
|
2018-02-05 19:42:49 +00:00
|
|
|
class VMwareConfigurationBackup(PyVmomi):
|
2017-11-10 06:07:30 +00:00
|
|
|
def __init__(self, module):
|
2018-02-05 19:42:49 +00:00
|
|
|
super(VMwareConfigurationBackup, self).__init__(module)
|
2017-11-10 06:07:30 +00:00
|
|
|
self.state = self.module.params['state']
|
|
|
|
self.dest = self.module.params['dest']
|
|
|
|
self.src = self.module.params['src']
|
|
|
|
self.hostname = self.module.params['hostname']
|
|
|
|
self.username = self.module.params['username']
|
|
|
|
self.password = self.module.params['password']
|
|
|
|
self.validate_certs = self.module.params['validate_certs']
|
2018-02-05 19:42:49 +00:00
|
|
|
self.esxi_hostname = self.module.params.get('esxi_hostname', None)
|
2017-11-10 06:07:30 +00:00
|
|
|
self.host = self.find_host_system()
|
|
|
|
|
|
|
|
def find_host_system(self):
|
2018-02-05 19:42:49 +00:00
|
|
|
if self.esxi_hostname:
|
|
|
|
host_system_obj = self.find_hostsystem_by_name(host_name=self.esxi_hostname)
|
|
|
|
if host_system_obj:
|
|
|
|
return host_system_obj
|
|
|
|
else:
|
|
|
|
self.module.fail_json(msg="Failed to find ESXi %s" % self.esxi_hostname)
|
|
|
|
|
2017-11-10 06:07:30 +00:00
|
|
|
host_system = get_all_objs(self.content, [vim.HostSystem])
|
|
|
|
return host_system.keys()[0]
|
|
|
|
|
|
|
|
def process_state(self):
|
|
|
|
if self.state == 'saved':
|
|
|
|
self.save_configuration()
|
|
|
|
|
|
|
|
if self.state == 'absent':
|
|
|
|
self.reset_configuration()
|
|
|
|
|
|
|
|
if self.state == 'loaded':
|
|
|
|
self.load_configuration()
|
|
|
|
|
|
|
|
def load_configuration(self):
|
|
|
|
if not os.path.isfile(self.src):
|
|
|
|
self.module.fail_json(msg="Source file {} does not exist".format(self.src))
|
|
|
|
|
|
|
|
url = self.host.configManager.firmwareSystem.QueryFirmwareConfigUploadURL()
|
2018-04-13 08:51:37 +00:00
|
|
|
url = url.replace('*', self.host.name)
|
2017-11-10 06:07:30 +00:00
|
|
|
# find manually the url if there is a redirect because urllib2 -per RFC- doesn't do automatic redirects for PUT requests
|
|
|
|
try:
|
|
|
|
request = open_url(url=url, method='HEAD', validate_certs=self.validate_certs)
|
|
|
|
except HTTPError as e:
|
|
|
|
url = e.geturl()
|
|
|
|
|
|
|
|
try:
|
|
|
|
with open(self.src, 'rb') as file:
|
|
|
|
data = file.read()
|
|
|
|
request = open_url(url=url, data=data, method='PUT', validate_certs=self.validate_certs,
|
|
|
|
url_username=self.username, url_password=self.password, force_basic_auth=True)
|
|
|
|
except Exception as e:
|
2018-02-05 19:42:49 +00:00
|
|
|
self.module.fail_json(msg=to_native(e))
|
2017-11-10 06:07:30 +00:00
|
|
|
|
|
|
|
if not self.host.runtime.inMaintenanceMode:
|
|
|
|
self.enter_maintenance()
|
|
|
|
try:
|
|
|
|
self.host.configManager.firmwareSystem.RestoreFirmwareConfiguration(force=True)
|
|
|
|
self.module.exit_json(changed=True)
|
|
|
|
except Exception as e:
|
|
|
|
self.exit_maintenance()
|
2018-02-05 19:42:49 +00:00
|
|
|
self.module.fail_json(msg=to_native(e))
|
2017-11-10 06:07:30 +00:00
|
|
|
|
|
|
|
def reset_configuration(self):
|
|
|
|
if not self.host.runtime.inMaintenanceMode:
|
|
|
|
self.enter_maintenance()
|
|
|
|
try:
|
|
|
|
self.host.configManager.firmwareSystem.ResetFirmwareToFactoryDefaults()
|
|
|
|
self.module.exit_json(changed=True)
|
|
|
|
except Exception as e:
|
|
|
|
self.exit_maintenance()
|
2018-02-05 19:42:49 +00:00
|
|
|
self.module.fail_json(msg=to_native(e))
|
2017-11-10 06:07:30 +00:00
|
|
|
|
|
|
|
def save_configuration(self):
|
|
|
|
url = self.host.configManager.firmwareSystem.BackupFirmwareConfiguration()
|
2018-04-13 08:51:37 +00:00
|
|
|
url = url.replace('*', self.host.name)
|
2017-11-10 06:07:30 +00:00
|
|
|
if os.path.isdir(self.dest):
|
|
|
|
filename = url.rsplit('/', 1)[1]
|
|
|
|
self.dest = os.path.join(self.dest, filename)
|
|
|
|
else:
|
|
|
|
filename, file_extension = os.path.splitext(self.dest)
|
|
|
|
if file_extension != ".tgz":
|
|
|
|
self.dest = filename + ".tgz"
|
|
|
|
try:
|
|
|
|
request = open_url(url=url, validate_certs=self.validate_certs)
|
|
|
|
with open(self.dest, "wb") as file:
|
|
|
|
file.write(request.read())
|
|
|
|
self.module.exit_json(changed=True, dest_file=self.dest)
|
|
|
|
except IOError as e:
|
2018-02-05 19:42:49 +00:00
|
|
|
self.module.fail_json(msg="Failed to write backup file. Ensure that "
|
|
|
|
"the dest path exists and is writable. Details : %s" % to_native(e))
|
2017-11-10 06:07:30 +00:00
|
|
|
except Exception as e:
|
2018-02-05 19:42:49 +00:00
|
|
|
self.module.fail_json(msg=to_native(e))
|
2017-11-10 06:07:30 +00:00
|
|
|
|
|
|
|
def enter_maintenance(self):
|
|
|
|
try:
|
|
|
|
task = self.host.EnterMaintenanceMode_Task(timeout=15)
|
|
|
|
success, result = wait_for_task(task)
|
|
|
|
except Exception as e:
|
2018-02-05 19:42:49 +00:00
|
|
|
self.module.fail_json(msg="Failed to enter maintenance mode."
|
|
|
|
" Ensure that there are no powered on machines on the host. %s" % to_native(e))
|
2017-11-10 06:07:30 +00:00
|
|
|
|
|
|
|
def exit_maintenance(self):
|
|
|
|
try:
|
|
|
|
task = self.host.ExitMaintenanceMode_Task(timeout=15)
|
|
|
|
success, result = wait_for_task(task)
|
2018-02-05 19:42:49 +00:00
|
|
|
except Exception as generic_exc:
|
|
|
|
self.module.fail_json(msg="Failed to exit maintenance mode due to %s" % to_native(generic_exc))
|
2017-11-10 06:07:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
argument_spec = vmware_argument_spec()
|
|
|
|
argument_spec.update(dict(dest=dict(required=False, type='path'),
|
2018-02-05 19:42:49 +00:00
|
|
|
esxi_hostname=dict(required=False, type='str'),
|
2017-11-10 06:07:30 +00:00
|
|
|
src=dict(required=False, type='path'),
|
|
|
|
state=dict(required=True, choices=['saved', 'absent', 'loaded'], type='str')))
|
|
|
|
required_if = [('state', 'saved', ['dest']),
|
|
|
|
('state', 'loaded', ['src'])]
|
|
|
|
|
2018-02-05 19:42:49 +00:00
|
|
|
module = AnsibleModule(argument_spec=argument_spec,
|
|
|
|
required_if=required_if,
|
|
|
|
supports_check_mode=False)
|
2017-11-10 06:07:30 +00:00
|
|
|
|
|
|
|
vmware_cfg_backup = VMwareConfigurationBackup(module)
|
|
|
|
vmware_cfg_backup.process_state()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|