| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2015, Russell Teague <rteague2 () csc.com> |
| 5 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 7 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
|
| 12 |
DOCUMENTATION = r''' |
| 13 |
--- |
| 14 |
module: vmware_vsan_cluster |
| 15 |
short_description: Configure VSAN clustering on an ESXi host |
| 16 |
description: |
| 17 |
- This module can be used to configure VSAN clustering on an ESXi host |
| 18 |
author: |
| 19 |
- Russell Teague (@mtnbikenc) |
| 20 |
options: |
| 21 |
cluster_uuid: |
| 22 |
description: |
| 23 |
- Desired cluster UUID |
| 24 |
required: false |
| 25 |
type: str |
| 26 |
extends_documentation_fragment: |
| 27 |
- vmware.vmware.base_options |
| 28 |
|
| 29 |
''' |
| 30 |
|
| 31 |
EXAMPLES = r''' |
| 32 |
- name: Configure VMware VSAN Cluster |
| 33 |
hosts: deploy_node |
| 34 |
tags: |
| 35 |
- vsan |
| 36 |
tasks: |
| 37 |
- name: Configure VSAN on first host |
| 38 |
community.vmware.vmware_vsan_cluster: |
| 39 |
hostname: "{{ groups['esxi'][0] }}" |
| 40 |
username: "{{ esxi_username }}" |
| 41 |
password: "{{ site_password }}" |
| 42 |
delegate_to: localhost |
| 43 |
register: vsan_cluster |
| 44 |
|
| 45 |
- name: Configure VSAN on remaining hosts |
| 46 |
community.vmware.vmware_vsan_cluster: |
| 47 |
hostname: "{{ item }}" |
| 48 |
username: "{{ esxi_username }}" |
| 49 |
password: "{{ site_password }}" |
| 50 |
cluster_uuid: "{{ vsan_cluster.cluster_uuid }}" |
| 51 |
delegate_to: localhost |
| 52 |
loop: "{{ groups['esxi'][1:] }}" |
| 53 |
''' |
| 54 |
|
| 55 |
try: |
| 56 |
from pyVmomi import vim, vmodl |
| 57 |
HAS_PYVMOMI = True |
| 58 |
except ImportError: |
| 59 |
HAS_PYVMOMI = False |
| 60 |
|
| 61 |
from ansible.module_utils.basic import AnsibleModule |
| 62 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import (get_all_objs, wait_for_task) |
| 63 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 64 |
from ansible_collections.vmware.vmware.plugins.module_utils.clients.pyvmomi import PyvmomiClient |
| 65 |
|
| 66 |
|
| 67 |
def create_vsan_cluster(host_system, new_cluster_uuid): |
| 68 |
host_config_manager = host_system.configManager |
| 69 |
vsan_system = host_config_manager.vsanSystem |
| 70 |
|
| 71 |
vsan_config = vim.vsan.host.ConfigInfo() |
| 72 |
vsan_config.enabled = True |
| 73 |
|
| 74 |
if new_cluster_uuid is not None: |
| 75 |
vsan_config.clusterInfo = vim.vsan.host.ConfigInfo.ClusterInfo() |
| 76 |
vsan_config.clusterInfo.uuid = new_cluster_uuid |
| 77 |
|
| 78 |
vsan_config.storageInfo = vim.vsan.host.ConfigInfo.StorageInfo() |
| 79 |
vsan_config.storageInfo.autoClaimStorage = True |
| 80 |
|
| 81 |
task = vsan_system.UpdateVsan_Task(vsan_config) |
| 82 |
changed, result = wait_for_task(task) |
| 83 |
|
| 84 |
host_status = vsan_system.QueryHostStatus() |
| 85 |
cluster_uuid = host_status.uuid |
| 86 |
|
| 87 |
return changed, result, cluster_uuid |
| 88 |
|
| 89 |
|
| 90 |
def main(): |
| 91 |
|
| 92 |
argument_spec = base_argument_spec() |
| 93 |
argument_spec.update(dict(cluster_uuid=dict(required=False, type='str'))) |
| 94 |
|
| 95 |
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=False) |
| 96 |
|
| 97 |
if not HAS_PYVMOMI: |
| 98 |
module.fail_json(msg='pyvmomi is required for this module') |
| 99 |
|
| 100 |
new_cluster_uuid = module.params['cluster_uuid'] |
| 101 |
|
| 102 |
try: |
| 103 |
pyvmomi_client = PyvmomiClient( |
| 104 |
hostname=module.params.get('hostname'), |
| 105 |
username=module.params.get('username'), |
| 106 |
password=module.params.get('password'), |
| 107 |
port=module.params.get('port'), |
| 108 |
validate_certs=module.params.get('validate_certs') |
| 109 |
) |
| 110 |
content = pyvmomi_client.content |
| 111 |
host = get_all_objs(content, [vim.HostSystem]) |
| 112 |
if not host: |
| 113 |
module.fail_json(msg="Unable to locate Physical Host.") |
| 114 |
host_system = list(host)[0] |
| 115 |
changed, result, cluster_uuid = create_vsan_cluster(host_system, new_cluster_uuid) |
| 116 |
module.exit_json(changed=changed, result=result, cluster_uuid=cluster_uuid) |
| 117 |
|
| 118 |
except vmodl.RuntimeFault as runtime_fault: |
| 119 |
module.fail_json(msg=runtime_fault.msg) |
| 120 |
except vmodl.MethodFault as method_fault: |
| 121 |
module.fail_json(msg=method_fault.msg) |
| 122 |
except Exception as e: |
| 123 |
module.fail_json(msg=str(e)) |
| 124 |
|
| 125 |
|
| 126 |
if __name__ == '__main__': |
| 127 |
main() |