summaryrefslogtreecommitdiff
path: root/cloud/vmware/vmware_cluster.py
blob: 72f29e7dfadcbc0b04fcce01a85687c84266ec4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/python
# -*- coding: utf-8 -*-

# (c) 2015, Joseph Callen <jcallen () csc.com>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.

DOCUMENTATION = '''
---
module: vmware_cluster
short_description: Create VMware vSphere Cluster
description:
    - Create VMware vSphere Cluster
version_added: 2.0
author: Joseph Callen (@jcpowermac)
notes:
requirements:
    - Tested on ESXi 5.5
    - PyVmomi installed
options:
    hostname:
        description:
            - The hostname or IP address of the vSphere vCenter
        required: True
    username:
        description:
            - The username of the vSphere vCenter
        required: True
        aliases: ['user', 'admin']
    password:
        description:
            - The password of the vSphere vCenter
        required: True
        aliases: ['pass', 'pwd']
    datacenter_name:
        description:
            - The name of the datacenter the cluster will be created in.
        required: True
    cluster_name:
        description:
            - The name of the cluster that will be created
        required: True
    enable_ha:
        description:
            - If set to True will enable HA when the cluster is created.
        required: False
        default: False
    enable_drs:
        description:
            - If set to True will enable DRS when the cluster is created.
        required: False
        default: False
    enable_vsan:
        description:
            - If set to True will enable vSAN when the cluster is created.
        required: False
        default: False
'''

EXAMPLES = '''
# Example vmware_cluster command from Ansible Playbooks
- name: Create Cluster
      local_action: >
        vmware_cluster
        hostname="{{ ansible_ssh_host }}" username=root password=vmware
        datacenter_name="datacenter"
        cluster_name="cluster"
        enable_ha=True
        enable_drs=True
        enable_vsan=True
'''

try:
    from pyVmomi import vim, vmodl
    HAS_PYVMOMI = True
except ImportError:
    HAS_PYVMOMI = False


def configure_ha(enable_ha):
    das_config = vim.cluster.DasConfigInfo()
    das_config.enabled = enable_ha
    das_config.admissionControlPolicy = vim.cluster.FailoverLevelAdmissionControlPolicy()
    das_config.admissionControlPolicy.failoverLevel = 2
    return das_config


def configure_drs(enable_drs):
    drs_config = vim.cluster.DrsConfigInfo()
    drs_config.enabled = enable_drs
    # Set to partially automated
    drs_config.vmotionRate = 3
    return drs_config


def configure_vsan(enable_vsan):
    vsan_config = vim.vsan.cluster.ConfigInfo()
    vsan_config.enabled = enable_vsan
    vsan_config.defaultConfig = vim.vsan.cluster.ConfigInfo.HostDefaultInfo()
    vsan_config.defaultConfig.autoClaimStorage = False
    return vsan_config


def state_create_cluster(module):

    enable_ha = module.params['enable_ha']
    enable_drs = module.params['enable_drs']
    enable_vsan = module.params['enable_vsan']
    cluster_name = module.params['cluster_name']
    datacenter = module.params['datacenter']

    try:
        cluster_config_spec = vim.cluster.ConfigSpecEx()
        cluster_config_spec.dasConfig = configure_ha(enable_ha)
        cluster_config_spec.drsConfig = configure_drs(enable_drs)
        if enable_vsan:
            cluster_config_spec.vsanConfig = configure_vsan(enable_vsan)
        if not module.check_mode:
            datacenter.hostFolder.CreateClusterEx(cluster_name, cluster_config_spec)
        module.exit_json(changed=True)
    except vim.fault.DuplicateName:
        module.fail_json(msg="A cluster with the name %s already exists" % cluster_name)
    except vmodl.fault.InvalidArgument:
        module.fail_json(msg="Cluster configuration specification parameter is invalid")
    except vim.fault.InvalidName:
        module.fail_json(msg="%s is an invalid name for a cluster" % cluster_name)
    except vmodl.fault.NotSupported:
        # This should never happen
        module.fail_json(msg="Trying to create a cluster on an incorrect folder object")
    except vmodl.RuntimeFault as runtime_fault:
        module.fail_json(msg=runtime_fault.msg)
    except vmodl.MethodFault as method_fault:
        # This should never happen either
        module.fail_json(msg=method_fault.msg)


def state_destroy_cluster(module):
    cluster = module.params['cluster']
    changed = True
    result = None

    try:
        if not module.check_mode:
            task = cluster.Destroy_Task()
            changed, result = wait_for_task(task)
        module.exit_json(changed=changed, result=result)
    except vim.fault.VimFault as vim_fault:
        module.fail_json(msg=vim_fault.msg)
    except vmodl.RuntimeFault as runtime_fault:
        module.fail_json(msg=runtime_fault.msg)
    except vmodl.MethodFault as method_fault:
        module.fail_json(msg=method_fault.msg)


def state_exit_unchanged(module):
    module.exit_json(changed=False)


def state_update_cluster(module):

    cluster_config_spec = vim.cluster.ConfigSpecEx()
    cluster = module.params['cluster']
    enable_ha = module.params['enable_ha']
    enable_drs = module.params['enable_drs']
    enable_vsan = module.params['enable_vsan']
    changed = True
    result = None

    if cluster.configurationEx.dasConfig.enabled != enable_ha:
        cluster_config_spec.dasConfig = configure_ha(enable_ha)
    if cluster.configurationEx.drsConfig.enabled != enable_drs:
        cluster_config_spec.drsConfig = configure_drs(enable_drs)
    if cluster.configurationEx.vsanConfigInfo.enabled != enable_vsan:
        cluster_config_spec.vsanConfig = configure_vsan(enable_vsan)

    try:
        if not module.check_mode:
            task = cluster.ReconfigureComputeResource_Task(cluster_config_spec, True)
            changed, result = wait_for_task(task)
        module.exit_json(changed=changed, result=result)
    except vmodl.RuntimeFault as runtime_fault:
        module.fail_json(msg=runtime_fault.msg)
    except vmodl.MethodFault as method_fault:
        module.fail_json(msg=method_fault.msg)
    except TaskError as task_e:
        module.fail_json(msg=str(task_e))


def check_cluster_configuration(module):
    datacenter_name = module.params['datacenter_name']
    cluster_name = module.params['cluster_name']

    try:
        content = connect_to_api(module)
        datacenter = find_datacenter_by_name(content, datacenter_name)
        if datacenter is None:
            module.fail_json(msg="Datacenter %s does not exist, "
                                 "please create first with Ansible Module vmware_datacenter or manually."
                                 % datacenter_name)
        cluster = find_cluster_by_name_datacenter(datacenter, cluster_name)

        module.params['content'] = content
        module.params['datacenter'] = datacenter

        if cluster is None:
            return 'absent'
        else:
            module.params['cluster'] = cluster

            desired_state = (module.params['enable_ha'],
                             module.params['enable_drs'],
                             module.params['enable_vsan'])

            current_state = (cluster.configurationEx.dasConfig.enabled,
                             cluster.configurationEx.drsConfig.enabled,
                             cluster.configurationEx.vsanConfigInfo.enabled)

            if cmp(desired_state, current_state) != 0:
                return 'update'
            else:
                return 'present'
    except vmodl.RuntimeFault as runtime_fault:
        module.fail_json(msg=runtime_fault.msg)
    except vmodl.MethodFault as method_fault:
        module.fail_json(msg=method_fault.msg)


def main():

    argument_spec = vmware_argument_spec()
    argument_spec.update(dict(datacenter_name=dict(required=True, type='str'),
                         cluster_name=dict(required=True, type='str'),
                         enable_ha=dict(default=False, required=False, type='bool'),
                         enable_drs=dict(default=False, required=False, type='bool'),
                         enable_vsan=dict(default=False, required=False, type='bool'),
                         state=dict(default='present', choices=['present', 'absent'], type='str')))

    module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)

    if not HAS_PYVMOMI:
        module.fail_json(msg='pyvmomi is required for this module')

    cluster_states = {
        'absent': {
            'present': state_destroy_cluster,
            'absent': state_exit_unchanged,
        },
        'present': {
            'update': state_update_cluster,
            'present': state_exit_unchanged,
            'absent': state_create_cluster,
        }
    }
    desired_state = module.params['state']
    current_state = check_cluster_configuration(module)

    # Based on the desired_state and the current_state call
    # the appropriate method from the dictionary
    cluster_states[desired_state][current_state](module)

from ansible.module_utils.vmware import *
from ansible.module_utils.basic import *

if __name__ == '__main__':
    main()