2015-07-23 12:14:47 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
2015-07-25 05:16:35 +02:00
|
|
|
# Copyright (c) 2015 Ansible, Inc.
|
2015-07-23 12:14:47 +02:00
|
|
|
#
|
2015-07-25 05:16:35 +02:00
|
|
|
# This file is part of Ansible
|
2015-07-23 12:14:47 +02:00
|
|
|
#
|
2015-07-25 05:16:35 +02:00
|
|
|
# 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.
|
2015-07-23 12:14:47 +02:00
|
|
|
#
|
2015-07-25 05:16:35 +02:00
|
|
|
# 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/>.
|
2015-07-23 12:14:47 +02:00
|
|
|
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: vca_vapp
|
2015-09-21 13:17:16 +02:00
|
|
|
short_description: Manages vCloud Air vApp instances.
|
2015-07-23 12:14:47 +02:00
|
|
|
description:
|
2015-09-21 13:17:16 +02:00
|
|
|
- This module will actively managed vCloud Air vApp instances. Instances
|
|
|
|
can be created and deleted as well as both deployed and undeployed.
|
2015-07-23 12:14:47 +02:00
|
|
|
version_added: "2.0"
|
2015-09-17 20:59:30 +02:00
|
|
|
author: Peter Sprygada (@privateip)
|
2015-07-23 12:14:47 +02:00
|
|
|
options:
|
2015-09-21 13:17:16 +02:00
|
|
|
vapp_name:
|
|
|
|
description:
|
|
|
|
- The name of the vCloud Air vApp instance
|
|
|
|
required: yes
|
|
|
|
vdc_name:
|
|
|
|
description:
|
|
|
|
- The name of the virtual data center (VDC) that contains the vAPP
|
|
|
|
required: yes
|
|
|
|
template_name:
|
|
|
|
description:
|
|
|
|
- The name of the vApp template to use to create the vApp instance. If
|
|
|
|
the I(state) is not `absent` then the I(template_name) value must be
|
|
|
|
provided. The I(template_name) must be previously uploaded to the
|
|
|
|
catalog specified by I(catalog_name)
|
|
|
|
required: no
|
|
|
|
default: None
|
|
|
|
network_name:
|
|
|
|
description:
|
|
|
|
- The name of the network that should be attached to the virtual machine
|
|
|
|
in the vApp. The virtual network specified must already be created in
|
|
|
|
the vCloud Air VDC. If the I(state) is not 'absent' then the
|
|
|
|
I(network_name) argument must be provided.
|
|
|
|
required: no
|
|
|
|
default: None
|
|
|
|
network_mode:
|
|
|
|
description:
|
|
|
|
- Configures the mode of the network connection.
|
|
|
|
required: no
|
|
|
|
default: pool
|
|
|
|
choices: ['pool', 'dhcp', 'static']
|
|
|
|
vm_name:
|
|
|
|
description:
|
|
|
|
- The name of the virtual machine instance in the vApp to manage.
|
|
|
|
required: no
|
|
|
|
default: None
|
|
|
|
vm_cpus:
|
|
|
|
description:
|
|
|
|
- The number of vCPUs to configure for the VM in the vApp. If the
|
|
|
|
I(vm_name) argument is provided, then this becomes a per VM setting
|
|
|
|
otherwise it is applied to all VMs in the vApp.
|
|
|
|
required: no
|
|
|
|
default: None
|
|
|
|
vm_memory:
|
|
|
|
description:
|
|
|
|
- The amount of memory in MB to allocate to VMs in the vApp. If the
|
|
|
|
I(vm_name) argument is provided, then this becomes a per VM setting
|
|
|
|
otherise it is applied to all VMs in the vApp.
|
|
|
|
required: no
|
|
|
|
default: None
|
|
|
|
operation:
|
|
|
|
description:
|
|
|
|
- Specifies an operation to be performed on the vApp.
|
|
|
|
required: no
|
|
|
|
default: noop
|
|
|
|
choices: ['noop', 'poweron', 'poweroff', 'suspend', 'shutdown', 'reboot', 'reset']
|
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- Configures the state of the vApp.
|
|
|
|
required: no
|
|
|
|
default: present
|
|
|
|
choices: ['present', 'absent', 'deployed', 'undeployed']
|
2015-07-23 12:14:47 +02:00
|
|
|
username:
|
|
|
|
description:
|
2015-09-21 13:17:16 +02:00
|
|
|
- The vCloud Air username to use during authentication
|
2015-07-23 12:14:47 +02:00
|
|
|
required: false
|
|
|
|
default: None
|
|
|
|
password:
|
|
|
|
description:
|
2015-09-21 13:17:16 +02:00
|
|
|
- The vCloud Air password to use during authentication
|
2015-07-23 12:14:47 +02:00
|
|
|
required: false
|
|
|
|
default: None
|
|
|
|
org:
|
|
|
|
description:
|
|
|
|
- The org to login to for creating vapp, mostly set when the service_type is vdc.
|
|
|
|
required: false
|
|
|
|
default: None
|
|
|
|
service_id:
|
|
|
|
description:
|
|
|
|
- The service id in a vchs environment to be used for creating the vapp
|
|
|
|
required: false
|
|
|
|
default: None
|
2015-09-21 13:17:16 +02:00
|
|
|
instance_id:
|
|
|
|
description:
|
|
|
|
- The vCloud Air instance ID
|
|
|
|
required: no
|
|
|
|
default: None
|
2015-07-23 12:14:47 +02:00
|
|
|
host:
|
|
|
|
description:
|
|
|
|
- The authentication host to be used when service type is vcd.
|
|
|
|
required: false
|
|
|
|
default: None
|
|
|
|
api_version:
|
|
|
|
description:
|
|
|
|
- The api version to be used with the vca
|
|
|
|
required: false
|
|
|
|
default: "5.7"
|
|
|
|
service_type:
|
|
|
|
description:
|
|
|
|
- The type of service we are authenticating against
|
|
|
|
required: false
|
|
|
|
default: vca
|
|
|
|
choices: [ "vca", "vchs", "vcd" ]
|
|
|
|
vdc_name:
|
|
|
|
description:
|
|
|
|
- The name of the vdc where the vm should be created.
|
|
|
|
required: false
|
|
|
|
default: None
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
|
2015-09-21 13:17:16 +02:00
|
|
|
- name: Creates a new vApp in a VCA instance
|
|
|
|
vca_vapp:
|
|
|
|
vapp_name: tower
|
|
|
|
state=present
|
|
|
|
template_name='Ubuntu Server 12.04 LTS (amd64 20150127)'
|
|
|
|
vdc_name=VDC1
|
|
|
|
instance_id=<your instance id here>
|
|
|
|
username=<your username here>
|
|
|
|
password=<your password here>
|
2015-07-23 12:14:47 +02:00
|
|
|
|
|
|
|
'''
|
|
|
|
|
2015-09-21 13:17:16 +02:00
|
|
|
DEFAULT_VAPP_OPERATION = 'noop'
|
2015-09-17 20:59:30 +02:00
|
|
|
|
2015-09-21 13:17:16 +02:00
|
|
|
VAPP_STATUS = {
|
|
|
|
'Powered off': 'poweroff',
|
|
|
|
'Powered on': 'poweron',
|
|
|
|
'Suspended': 'suspend'
|
|
|
|
}
|
2015-09-17 20:59:30 +02:00
|
|
|
|
2015-09-21 13:17:16 +02:00
|
|
|
VAPP_STATES = ['present', 'absent', 'deployed', 'undeployed']
|
|
|
|
VAPP_OPERATIONS = ['poweron', 'poweroff', 'suspend', 'shutdown',
|
|
|
|
'reboot', 'reset', 'noop']
|
2015-09-17 20:59:30 +02:00
|
|
|
|
|
|
|
|
2015-09-21 13:17:16 +02:00
|
|
|
def get_instance(module):
|
|
|
|
vapp_name = module.params['vapp_name']
|
|
|
|
inst = dict(vapp_name=vapp_name, state='absent')
|
2015-09-17 20:59:30 +02:00
|
|
|
try:
|
2015-09-21 13:17:16 +02:00
|
|
|
vapp = module.get_vapp(vapp_name)
|
|
|
|
if vapp:
|
|
|
|
status = module.vca.get_status(vapp.me.get_status())
|
|
|
|
inst['status'] = VAPP_STATUS.get(status, 'unknown')
|
|
|
|
inst['state'] = 'deployed' if vapp.me.deployed else 'undeployed'
|
|
|
|
return inst
|
|
|
|
except VcaError:
|
|
|
|
return inst
|
|
|
|
|
|
|
|
def create(module):
|
2015-09-17 20:59:30 +02:00
|
|
|
vdc_name = module.params['vdc_name']
|
|
|
|
vapp_name = module.params['vapp_name']
|
|
|
|
template_name = module.params['template_name']
|
|
|
|
catalog_name = module.params['catalog_name']
|
|
|
|
network_name = module.params['network_name']
|
|
|
|
network_mode = module.params['network_mode']
|
|
|
|
vm_name = module.params['vm_name']
|
|
|
|
vm_cpus = module.params['vm_cpus']
|
|
|
|
vm_memory = module.params['vm_memory']
|
2015-09-21 13:17:16 +02:00
|
|
|
deploy = module.params['state'] == 'deploy'
|
|
|
|
poweron = module.params['operation'] == 'poweron'
|
2015-09-17 20:59:30 +02:00
|
|
|
|
2015-09-21 13:17:16 +02:00
|
|
|
task = module.vca.create_vapp(vdc_name, vapp_name, template_name,
|
|
|
|
catalog_name, network_name, network_mode,
|
|
|
|
vm_name, vm_cpus, vm_memory, deploy, poweron)
|
2015-09-17 20:59:30 +02:00
|
|
|
|
2015-09-21 13:17:16 +02:00
|
|
|
module.vca.block_until_completed(task)
|
2015-09-17 20:59:30 +02:00
|
|
|
|
2015-09-21 13:17:16 +02:00
|
|
|
def delete(module):
|
2015-09-17 20:59:30 +02:00
|
|
|
vdc_name = module.params['vdc_name']
|
|
|
|
vapp_name = module.params['vapp_name']
|
2015-09-21 13:17:16 +02:00
|
|
|
module.vca.delete_vapp(vdc_name, vapp_name)
|
2015-09-17 20:59:30 +02:00
|
|
|
|
2015-09-21 13:17:16 +02:00
|
|
|
def do_operation(module):
|
|
|
|
vapp_name = module.params['vapp_name']
|
|
|
|
operation = module.params['operation']
|
2015-07-23 12:14:47 +02:00
|
|
|
|
2015-09-21 13:17:16 +02:00
|
|
|
vm_name = module.params.get('vm_name')
|
|
|
|
vm = None
|
|
|
|
if vm_name:
|
|
|
|
vm = module.get_vm(vapp_name, vm_name)
|
2015-07-23 12:14:47 +02:00
|
|
|
|
2015-09-21 13:17:16 +02:00
|
|
|
if operation == 'poweron':
|
|
|
|
operation = 'powerOn'
|
|
|
|
elif operation == 'poweroff':
|
|
|
|
operation = 'powerOff'
|
2015-07-23 12:14:47 +02:00
|
|
|
|
2015-09-21 13:17:16 +02:00
|
|
|
cmd = 'power:%s' % operation
|
|
|
|
module.get_vapp(vapp_name).execute(cmd, 'post', targetVM=vm)
|
|
|
|
|
|
|
|
def set_state(module):
|
2015-09-17 20:59:30 +02:00
|
|
|
state = module.params['state']
|
2015-09-21 13:17:16 +02:00
|
|
|
vapp = module.get_vapp(module.params['vapp_name'])
|
|
|
|
if state == 'deployed':
|
|
|
|
action = module.params['operation'] == 'poweron'
|
|
|
|
if not vapp.deploy(action):
|
|
|
|
module.fail('unable to deploy vapp')
|
|
|
|
elif state == 'undeployed':
|
|
|
|
action = module.params['operation']
|
|
|
|
if action == 'poweroff':
|
|
|
|
action = 'powerOff'
|
|
|
|
elif action != 'suspend':
|
|
|
|
action = None
|
|
|
|
if not vapp.undeploy(action):
|
|
|
|
module.fail('unable to undeploy vapp')
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
|
|
|
argument_spec = dict(
|
|
|
|
vapp_name=dict(required=True),
|
|
|
|
vdc_name=dict(required=True),
|
|
|
|
template_name=dict(),
|
|
|
|
catalog_name=dict(default='Public Catalog'),
|
|
|
|
network_name=dict(),
|
|
|
|
network_mode=dict(default='pool', choices=['dhcp', 'static', 'pool']),
|
|
|
|
vm_name=dict(),
|
|
|
|
vm_cpus=dict(),
|
|
|
|
vm_memory=dict(),
|
|
|
|
operation=dict(default=DEFAULT_VAPP_OPERATION, choices=VAPP_OPERATIONS),
|
|
|
|
state=dict(default='present', choices=VAPP_STATES)
|
|
|
|
)
|
2015-07-23 12:14:47 +02:00
|
|
|
|
2015-09-21 13:17:16 +02:00
|
|
|
module = VcaAnsibleModule(argument_spec=argument_spec,
|
|
|
|
supports_check_mode=True)
|
2015-07-23 12:14:47 +02:00
|
|
|
|
2015-09-21 13:17:16 +02:00
|
|
|
state = module.params['state']
|
|
|
|
operation = module.params['operation']
|
2015-09-17 20:59:30 +02:00
|
|
|
|
2015-09-21 13:17:16 +02:00
|
|
|
instance = get_instance(module)
|
2015-09-17 20:59:30 +02:00
|
|
|
|
|
|
|
result = dict(changed=False)
|
|
|
|
|
2015-09-21 13:17:16 +02:00
|
|
|
if instance and state == 'absent':
|
|
|
|
if not module.check_mode:
|
|
|
|
delete(module)
|
|
|
|
result['changed'] = True
|
2015-09-17 20:59:30 +02:00
|
|
|
|
2015-09-21 13:17:16 +02:00
|
|
|
elif state != 'absent':
|
|
|
|
if instance['state'] == 'absent':
|
2015-09-17 20:59:30 +02:00
|
|
|
if not module.check_mode:
|
2015-09-21 13:17:16 +02:00
|
|
|
create(module)
|
2015-09-17 20:59:30 +02:00
|
|
|
result['changed'] = True
|
2015-09-21 13:17:16 +02:00
|
|
|
|
|
|
|
elif instance['state'] != state and state != 'present':
|
2015-09-17 20:59:30 +02:00
|
|
|
if not module.check_mode:
|
2015-09-21 13:17:16 +02:00
|
|
|
set_state(module)
|
2015-09-17 20:59:30 +02:00
|
|
|
result['changed'] = True
|
2015-09-21 13:17:16 +02:00
|
|
|
|
|
|
|
if operation != instance.get('status') and operation != 'noop':
|
2015-09-17 20:59:30 +02:00
|
|
|
if not module.check_mode:
|
2015-09-21 13:17:16 +02:00
|
|
|
do_operation(module)
|
2015-09-17 20:59:30 +02:00
|
|
|
result['changed'] = True
|
|
|
|
|
2015-09-21 13:17:16 +02:00
|
|
|
return module.exit(**result)
|
2015-09-17 20:59:30 +02:00
|
|
|
|
2015-07-23 12:14:47 +02:00
|
|
|
# import module snippets
|
|
|
|
from ansible.module_utils.basic import *
|
2015-09-17 20:59:30 +02:00
|
|
|
from ansible.module_utils.vca import *
|
2015-07-23 12:14:47 +02:00
|
|
|
if __name__ == '__main__':
|
2015-09-17 20:59:30 +02:00
|
|
|
main()
|