cs_instance: remove CloudStackException dep (#31724)

- fixes dep
- fix docs
This commit is contained in:
René Moser 2017-10-14 00:09:57 +02:00 committed by GitHub
commit 8b8d5aaca7
2 changed files with 169 additions and 284 deletions

View file

@ -27,192 +27,134 @@ options:
- Host name of the instance. C(name) can only contain ASCII letters. - Host name of the instance. C(name) can only contain ASCII letters.
- Name will be generated (UUID) by CloudStack if not specified and can not be changed afterwards. - Name will be generated (UUID) by CloudStack if not specified and can not be changed afterwards.
- Either C(name) or C(display_name) is required. - Either C(name) or C(display_name) is required.
required: false
default: null
display_name: display_name:
description: description:
- Custom display name of the instances. - Custom display name of the instances.
- Display name will be set to C(name) if not specified. - Display name will be set to C(name) if not specified.
- Either C(name) or C(display_name) is required. - Either C(name) or C(display_name) is required.
required: false
default: null
group: group:
description: description:
- Group in where the new instance should be in. - Group in where the new instance should be in.
required: false
default: null
state: state:
description: description:
- State of the instance. - State of the instance.
required: false default: present
default: 'present' choices: [ deployed, started, stopped, restarted, restored, destroyed, expunged, present, absent ]
choices: [ 'deployed', 'started', 'stopped', 'restarted', 'restored', 'destroyed', 'expunged', 'present', 'absent' ]
service_offering: service_offering:
description: description:
- Name or id of the service offering of the new instance. - Name or id of the service offering of the new instance.
- If not set, first found service offering is used. - If not set, first found service offering is used.
required: false
default: null
cpu: cpu:
description: description:
- The number of CPUs to allocate to the instance, used with custom service offerings - The number of CPUs to allocate to the instance, used with custom service offerings
required: false
default: null
cpu_speed: cpu_speed:
description: description:
- The clock speed/shares allocated to the instance, used with custom service offerings - The clock speed/shares allocated to the instance, used with custom service offerings
required: false
default: null
memory: memory:
description: description:
- The memory allocated to the instance, used with custom service offerings - The memory allocated to the instance, used with custom service offerings
required: false
default: null
template: template:
description: description:
- Name or id of the template to be used for creating the new instance. - Name or id of the template to be used for creating the new instance.
- Required when using C(state=present). - Required when using C(state=present).
- Mutually exclusive with C(ISO) option. - Mutually exclusive with C(ISO) option.
required: false
default: null
iso: iso:
description: description:
- Name or id of the ISO to be used for creating the new instance. - Name or id of the ISO to be used for creating the new instance.
- Required when using C(state=present). - Required when using C(state=present).
- Mutually exclusive with C(template) option. - Mutually exclusive with C(template) option.
required: false
default: null
template_filter: template_filter:
description: description:
- Name of the filter used to search for the template or iso. - Name of the filter used to search for the template or iso.
- Used for params C(iso) or C(template) on C(state=present). - Used for params C(iso) or C(template) on C(state=present).
required: false default: executable
default: 'executable' choices: [ featured, self, selfexecutable, sharedexecutable, executable, community ]
choices: [ 'featured', 'self', 'selfexecutable', 'sharedexecutable', 'executable', 'community' ] aliases: [ iso_filter ]
aliases: [ 'iso_filter' ]
version_added: '2.1' version_added: '2.1'
hypervisor: hypervisor:
description: description:
- Name the hypervisor to be used for creating the new instance. - Name the hypervisor to be used for creating the new instance.
- Relevant when using C(state=present), but only considered if not set on ISO/template. - Relevant when using C(state=present), but only considered if not set on ISO/template.
- If not set or found on ISO/template, first found hypervisor will be used. - If not set or found on ISO/template, first found hypervisor will be used.
required: false choices: [ KVM, VMware, BareMetal, XenServer, LXC, HyperV, UCS, OVM, Simulator ]
default: null
choices: [ 'KVM', 'VMware', 'BareMetal', 'XenServer', 'LXC', 'HyperV', 'UCS', 'OVM' ]
keyboard: keyboard:
description: description:
- Keyboard device type for the instance. - Keyboard device type for the instance.
required: false choices: [ de, de-ch, es, fi, fr, fr-be, fr-ch, is, it, jp, nl-be, no, pt, uk, us ]
default: null
choices: [ 'de', 'de-ch', 'es', 'fi', 'fr', 'fr-be', 'fr-ch', 'is', 'it', 'jp', 'nl-be', 'no', 'pt', 'uk', 'us' ]
networks: networks:
description: description:
- List of networks to use for the new instance. - List of networks to use for the new instance.
required: false aliases: [ network ]
default: []
aliases: [ 'network' ]
ip_address: ip_address:
description: description:
- IPv4 address for default instance's network during creation. - IPv4 address for default instance's network during creation.
required: false
default: null
ip6_address: ip6_address:
description: description:
- IPv6 address for default instance's network. - IPv6 address for default instance's network.
required: false
default: null
ip_to_networks: ip_to_networks:
description: description:
- "List of mappings in the form {'network': NetworkName, 'ip': 1.2.3.4}" - "List of mappings in the form {'network': NetworkName, 'ip': 1.2.3.4}"
- Mutually exclusive with C(networks) option. - Mutually exclusive with C(networks) option.
required: false aliases: [ ip_to_network ]
default: null
aliases: [ 'ip_to_network' ]
disk_offering: disk_offering:
description: description:
- Name of the disk offering to be used. - Name of the disk offering to be used.
required: false
default: null
disk_size: disk_size:
description: description:
- Disk size in GByte required if deploying instance from ISO. - Disk size in GByte required if deploying instance from ISO.
required: false
default: null
root_disk_size: root_disk_size:
description: description:
- Root disk size in GByte required if deploying instance with KVM hypervisor and want resize the root disk size at startup - Root disk size in GByte required if deploying instance with KVM hypervisor and want resize the root disk size at startup
(need CloudStack >= 4.4, cloud-initramfs-growroot installed and enabled in the template) (need CloudStack >= 4.4, cloud-initramfs-growroot installed and enabled in the template)
required: false
default: null
security_groups: security_groups:
description: description:
- List of security groups the instance to be applied to. - List of security groups the instance to be applied to.
required: false aliases: [ security_group ]
default: null
aliases: [ 'security_group' ]
domain: domain:
description: description:
- Domain the instance is related to. - Domain the instance is related to.
required: false
default: null
account: account:
description: description:
- Account the instance is related to. - Account the instance is related to.
required: false
default: null
project: project:
description: description:
- Name of the project the instance to be deployed in. - Name of the project the instance to be deployed in.
required: false
default: null
zone: zone:
description: description:
- Name of the zone in which the instance should be deployed. - Name of the zone in which the instance should be deployed.
- If not set, default zone is used. - If not set, default zone is used.
required: false
default: null
ssh_key: ssh_key:
description: description:
- Name of the SSH key to be deployed on the new instance. - Name of the SSH key to be deployed on the new instance.
required: false
default: null
affinity_groups: affinity_groups:
description: description:
- Affinity groups names to be applied to the new instance. - Affinity groups names to be applied to the new instance.
required: false aliases: [ affinity_group ]
default: []
aliases: [ 'affinity_group' ]
user_data: user_data:
description: description:
- Optional data (ASCII) that can be sent to the instance upon a successful deployment. - Optional data (ASCII) that can be sent to the instance upon a successful deployment.
- The data will be automatically base64 encoded. - The data will be automatically base64 encoded.
- Consider switching to HTTP_POST by using C(CLOUDSTACK_METHOD=post) to increase the HTTP_GET size limit of 2KB to 32 KB. - Consider switching to HTTP_POST by using C(CLOUDSTACK_METHOD=post) to increase the HTTP_GET size limit of 2KB to 32 KB.
required: false
default: null
force: force:
description: description:
- Force stop/start the instance if required to apply changes, otherwise a running instance will not be changed. - Force stop/start the instance if required to apply changes, otherwise a running instance will not be changed.
required: false
default: false default: false
tags: tags:
description: description:
- List of tags. Tags are a list of dictionaries having keys C(key) and C(value). - List of tags. Tags are a list of dictionaries having keys C(key) and C(value).
- "If you want to delete all tags, set a empty list e.g. C(tags: [])." - "If you want to delete all tags, set a empty list e.g. C(tags: [])."
required: false aliases: [ tag ]
default: null
aliases: [ 'tag' ]
poll_async: poll_async:
description: description:
- Poll async jobs until job has finished. - Poll async jobs until job has finished.
required: false
default: true default: true
extends_documentation_fragment: cloudstack extends_documentation_fragment: cloudstack
''' '''
EXAMPLES = ''' EXAMPLES = '''
# Create a instance from an ISO
# NOTE: Names of offerings and ISOs depending on the CloudStack configuration. # NOTE: Names of offerings and ISOs depending on the CloudStack configuration.
- cs_instance: - name: create a instance from an ISO
cs_instance:
name: web-vm-1 name: web-vm-1
iso: Linux Debian 7 64-bit iso: Linux Debian 7 64-bit
hypervisor: VMware hypervisor: VMware
@ -227,7 +169,7 @@ EXAMPLES = '''
- Storage Integration - Storage Integration
delegate_to: localhost delegate_to: localhost
# For changing a running instance, use the 'force' parameter - name: for changing a running instance, use the 'force' parameter
- cs_instance: - cs_instance:
name: web-vm-1 name: web-vm-1
display_name: web-vm-01.example.com display_name: web-vm-01.example.com
@ -236,9 +178,9 @@ EXAMPLES = '''
force: yes force: yes
delegate_to: localhost delegate_to: localhost
# Create or update a instance on Exoscale's public cloud using display_name. # NOTE: user_data can be used to kickstart the instance using cloud-init yaml config.
# Note: user_data can be used to kickstart the instance using cloud-init yaml config. - name: create or update a instance on Exoscale's public cloud using display_name.
- cs_instance: cs_instance:
display_name: web-vm-1 display_name: web-vm-1
template: Linux Debian 7 64-bit template: Linux Debian 7 64-bit
service_offering: Tiny service_offering: Tiny
@ -254,8 +196,8 @@ EXAMPLES = '''
- nginx - nginx
delegate_to: localhost delegate_to: localhost
# Create an instance with multiple interfaces specifying the IP addresses - name: create an instance with multiple interfaces specifying the IP addresses
- cs_instance: cs_instance:
name: web-vm-1 name: web-vm-1
template: Linux Debian 7 64-bit template: Linux Debian 7 64-bit
service_offering: Tiny service_offering: Tiny
@ -266,19 +208,19 @@ EXAMPLES = '''
ip: 192.0.2.1 ip: 192.0.2.1
delegate_to: localhost delegate_to: localhost
# Ensure an instance is stopped - name: ensure an instance is stopped
- cs_instance: cs_instance:
name: web-vm-1 name: web-vm-1
state: stopped state: stopped
delegate_to: localhost delegate_to: localhost
# Ensure an instance is running - name: ensure an instance is running
- cs_instance: cs_instance:
name: web-vm-1 name: web-vm-1
state: started state: started
delegate_to: localhost delegate_to: localhost
# Remove an instance - name: remove an instance
- cs_instance: - cs_instance:
name: web-vm-1 name: web-vm-1
state: absent state: absent
@ -405,12 +347,6 @@ instance_name:
''' '''
import base64 import base64
try:
from cs import CloudStackException
except ImportError:
pass # Handled in AnsibleCloudStack.__init__
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.cloudstack import (AnsibleCloudStack, CS_HYPERVISORS, cs_argument_spec, from ansible.module_utils.cloudstack import (AnsibleCloudStack, CS_HYPERVISORS, cs_argument_spec,
cs_required_together) cs_required_together)
@ -436,11 +372,10 @@ class AnsibleCloudStackInstance(AnsibleCloudStack):
self.template = None self.template = None
self.iso = None self.iso = None
def get_service_offering_id(self): def get_service_offering_id(self):
service_offering = self.module.params.get('service_offering') service_offering = self.module.params.get('service_offering')
service_offerings = self.cs.listServiceOfferings() service_offerings = self.query_api('listServiceOfferings', )
if service_offerings: if service_offerings:
if not service_offering: if not service_offering:
return service_offerings['serviceoffering'][0]['id'] return service_offerings['serviceoffering'][0]['id']
@ -448,8 +383,7 @@ class AnsibleCloudStackInstance(AnsibleCloudStack):
for s in service_offerings['serviceoffering']: for s in service_offerings['serviceoffering']:
if service_offering in [s['name'], s['id']]: if service_offering in [s['name'], s['id']]:
return s['id'] return s['id']
self.module.fail_json(msg="Service offering '%s' not found" % service_offering) self.fail_json(msg="Service offering '%s' not found" % service_offering)
def get_template_or_iso(self, key=None): def get_template_or_iso(self, key=None):
template = self.module.params.get('template') template = self.module.params.get('template')
@ -458,12 +392,13 @@ class AnsibleCloudStackInstance(AnsibleCloudStack):
if not template and not iso: if not template and not iso:
return None return None
args = {} args = {
args['account'] = self.get_account(key='name') 'account': self.get_account(key='name'),
args['domainid'] = self.get_domain(key='id') 'domainid': self.get_domain(key='id'),
args['projectid'] = self.get_project(key='id') 'projectid': self.get_project(key='id'),
args['zoneid'] = self.get_zone(key='id') 'zoneid': self.get_zone(key='id'),
args['isrecursive'] = True 'isrecursive': True,
}
if template: if template:
if self.template: if self.template:
@ -471,7 +406,7 @@ class AnsibleCloudStackInstance(AnsibleCloudStack):
rootdisksize = self.module.params.get('root_disk_size') rootdisksize = self.module.params.get('root_disk_size')
args['templatefilter'] = self.module.params.get('template_filter') args['templatefilter'] = self.module.params.get('template_filter')
templates = self.cs.listTemplates(**args) templates = self.query_api('listTemplates', **args)
if templates: if templates:
for t in templates['template']: for t in templates['template']:
if template in [t['displaytext'], t['name'], t['id']]: if template in [t['displaytext'], t['name'], t['id']]:
@ -479,21 +414,26 @@ class AnsibleCloudStackInstance(AnsibleCloudStack):
continue continue
self.template = t self.template = t
return self._get_by_key(key, self.template) return self._get_by_key(key, self.template)
more_info = ""
if rootdisksize: if rootdisksize:
more_info = " (with size <= %s)" % rootdisksize more_info = " (with size <= %s)" % rootdisksize
else:
more_info = ""
self.module.fail_json(msg="Template '%s' not found%s" % (template, more_info)) self.module.fail_json(msg="Template '%s' not found%s" % (template, more_info))
elif iso: elif iso:
if self.iso: if self.iso:
return self._get_by_key(key, self.iso) return self._get_by_key(key, self.iso)
args['isofilter'] = self.module.params.get('template_filter') args['isofilter'] = self.module.params.get('template_filter')
isos = self.cs.listIsos(**args) isos = self.query_api('listIsos', **args)
if isos: if isos:
for i in isos['iso']: for i in isos['iso']:
if iso in [i['displaytext'], i['name'], i['id']]: if iso in [i['displaytext'], i['name'], i['id']]:
self.iso = i self.iso = i
return self._get_by_key(key, self.iso) return self._get_by_key(key, self.iso)
self.module.fail_json(msg="ISO '%s' not found" % iso) self.module.fail_json(msg="ISO '%s' not found" % iso)
def get_instance(self): def get_instance(self):
@ -506,7 +446,7 @@ class AnsibleCloudStackInstance(AnsibleCloudStack):
'projectid': self.get_project(key='id'), 'projectid': self.get_project(key='id'),
} }
# Do not pass zoneid, as the instance name must be unique across zones. # Do not pass zoneid, as the instance name must be unique across zones.
instances = self.cs.listVirtualMachines(**args) instances = self.query_api('listVirtualMachines', **args)
if instances: if instances:
for v in instances['virtualmachine']: for v in instances['virtualmachine']:
if instance_name.lower() in [v['name'].lower(), v['displayname'].lower(), v['id']]: if instance_name.lower() in [v['name'].lower(), v['displayname'].lower(), v['id']]:
@ -514,7 +454,6 @@ class AnsibleCloudStackInstance(AnsibleCloudStack):
break break
return self.instance return self.instance
def _get_instance_user_data(self, instance): def _get_instance_user_data(self, instance):
# Query the user data if we need to # Query the user data if we need to
if 'userdata' in instance: if 'userdata' in instance:
@ -522,11 +461,10 @@ class AnsibleCloudStackInstance(AnsibleCloudStack):
user_data = "" user_data = ""
if self.get_user_data() is not None: if self.get_user_data() is not None:
res = self.cs.getVirtualMachineUserData(virtualmachineid=instance['id']) res = self.query_api('getVirtualMachineUserData', virtualmachineid=instance['id'])
user_data = res['virtualmachineuserdata'].get('userdata', "") user_data = res['virtualmachineuserdata'].get('userdata', "")
return user_data return user_data
def get_iptonetwork_mappings(self): def get_iptonetwork_mappings(self):
network_mappings = self.module.params.get('ip_to_networks') network_mappings = self.module.params.get('ip_to_networks')
if network_mappings is None: if network_mappings is None:
@ -553,11 +491,8 @@ class AnsibleCloudStackInstance(AnsibleCloudStack):
'projectid': self.get_project('id'), 'projectid': self.get_project('id'),
'name': ssh_key_name, 'name': ssh_key_name,
} }
ssh_key_pairs = self.cs.listSSHKeyPairs(**args) ssh_key_pairs = self.query_api('listSSHKeyPairs', **args)
if 'errortext' in ssh_key_pairs: if 'sshkeypair' in ssh_key_pairs:
self.module.fail_json(msg="Failed: '%s'" % ssh_key_pairs['errortext'])
elif 'sshkeypair' in ssh_key_pairs:
return self._get_by_key(key=key, my_dict=ssh_key_pairs['sshkeypair'][0]) return self._get_by_key(key=key, my_dict=ssh_key_pairs['sshkeypair'][0])
elif fail_on_missing: elif fail_on_missing:
@ -592,7 +527,7 @@ class AnsibleCloudStackInstance(AnsibleCloudStack):
return False return False
security_groups = [s.lower() for s in security_groups] security_groups = [s.lower() for s in security_groups]
instance_security_groups = self.instance.get('securitygroup',[]) instance_security_groups = self.instance.get('securitygroup') or []
instance_security_group_names = [] instance_security_group_names = []
for instance_security_group in instance_security_groups: for instance_security_group in instance_security_groups:
@ -606,7 +541,6 @@ class AnsibleCloudStackInstance(AnsibleCloudStack):
return True return True
return False return False
def get_network_ids(self, network_names=None): def get_network_ids(self, network_names=None):
if network_names is None: if network_names is None:
network_names = self.module.params.get('networks') network_names = self.module.params.get('networks')
@ -620,7 +554,7 @@ class AnsibleCloudStackInstance(AnsibleCloudStack):
'projectid': self.get_project(key='id'), 'projectid': self.get_project(key='id'),
'zoneid': self.get_zone(key='id'), 'zoneid': self.get_zone(key='id'),
} }
networks = self.cs.listNetworks(**args) networks = self.query_api('listNetworks', **args)
if not networks: if not networks:
self.module.fail_json(msg="No networks available") self.module.fail_json(msg="No networks available")
@ -638,7 +572,6 @@ class AnsibleCloudStackInstance(AnsibleCloudStack):
return network_ids return network_ids
def present_instance(self, start_vm=True): def present_instance(self, start_vm=True):
instance = self.get_instance() instance = self.get_instance()
@ -656,14 +589,12 @@ class AnsibleCloudStackInstance(AnsibleCloudStack):
return instance return instance
def get_user_data(self): def get_user_data(self):
user_data = self.module.params.get('user_data') user_data = self.module.params.get('user_data')
if user_data is not None: if user_data is not None:
user_data = base64.b64encode(str(user_data)) user_data = base64.b64encode(str(user_data))
return user_data return user_data
def get_details(self): def get_details(self):
res = None res = None
cpu = self.module.params.get('cpu') cpu = self.module.params.get('cpu')
@ -677,7 +608,6 @@ class AnsibleCloudStackInstance(AnsibleCloudStack):
}] }]
return res return res
def deploy_instance(self, start_vm=True): def deploy_instance(self, start_vm=True):
self.result['changed'] = True self.result['changed'] = True
networkids = self.get_network_ids() networkids = self.get_network_ids()
@ -708,12 +638,9 @@ class AnsibleCloudStackInstance(AnsibleCloudStack):
args['size'] = self.module.params.get('disk_size') args['size'] = self.module.params.get('disk_size')
args['startvm'] = start_vm args['startvm'] = start_vm
args['rootdisksize'] = self.module.params.get('root_disk_size') args['rootdisksize'] = self.module.params.get('root_disk_size')
args['affinitygroupnames'] = ','.join(self.module.params.get('affinity_groups')) args['affinitygroupnames'] = self.module.params.get('affinity_groups')
args['details'] = self.get_details() args['details'] = self.get_details()
args['securitygroupnames'] = self.module.params.get('security_groups')
security_groups = self.module.params.get('security_groups')
if security_groups is not None:
args['securitygroupnames'] = ','.join(security_groups)
template_iso = self.get_template_or_iso() template_iso = self.get_template_or_iso()
if 'hypervisor' not in template_iso: if 'hypervisor' not in template_iso:
@ -721,29 +648,27 @@ class AnsibleCloudStackInstance(AnsibleCloudStack):
instance = None instance = None
if not self.module.check_mode: if not self.module.check_mode:
instance = self.cs.deployVirtualMachine(**args) instance = self.query_api('deployVirtualMachine', **args)
if 'errortext' in instance:
self.module.fail_json(msg="Failed: '%s'" % instance['errortext'])
poll_async = self.module.params.get('poll_async') poll_async = self.module.params.get('poll_async')
if poll_async: if poll_async:
instance = self.poll_job(instance, 'virtualmachine') instance = self.poll_job(instance, 'virtualmachine')
return instance return instance
def update_instance(self, instance, start_vm=True): def update_instance(self, instance, start_vm=True):
# Service offering data # Service offering data
args_service_offering = {} args_service_offering = {
args_service_offering['id'] = instance['id'] 'id': instance['id'],
}
if self.module.params.get('service_offering'): if self.module.params.get('service_offering'):
args_service_offering['serviceofferingid'] = self.get_service_offering_id() args_service_offering['serviceofferingid'] = self.get_service_offering_id()
service_offering_changed = self.has_changed(args_service_offering, instance) service_offering_changed = self.has_changed(args_service_offering, instance)
# Instance data # Instance data
args_instance_update = {} args_instance_update = {
args_instance_update['id'] = instance['id'] 'id': instance['id'],
args_instance_update['userdata'] = self.get_user_data() 'userdata': self.get_user_data(),
}
instance['userdata'] = self._get_instance_user_data(instance) instance['userdata'] = self._get_instance_user_data(instance)
args_instance_update['ostypeid'] = self.get_os_type(key='id') args_instance_update['ostypeid'] = self.get_os_type(key='id')
if self.module.params.get('group'): if self.module.params.get('group'):
@ -777,9 +702,7 @@ class AnsibleCloudStackInstance(AnsibleCloudStack):
# Change service offering # Change service offering
if service_offering_changed: if service_offering_changed:
res = self.cs.changeServiceForVirtualMachine(**args_service_offering) res = self.query_api('changeServiceForVirtualMachine', **args_service_offering)
if 'errortext' in res:
self.module.fail_json(msg="Failed: '%s'" % res['errortext'])
instance = res['virtualmachine'] instance = res['virtualmachine']
self.instance = instance self.instance = instance
@ -787,9 +710,7 @@ class AnsibleCloudStackInstance(AnsibleCloudStack):
if instance_changed or security_groups_changed: if instance_changed or security_groups_changed:
if security_groups_changed: if security_groups_changed:
args_instance_update['securitygroupnames'] = ','.join(self.module.params.get('security_groups')) args_instance_update['securitygroupnames'] = ','.join(self.module.params.get('security_groups'))
res = self.cs.updateVirtualMachine(**args_instance_update) res = self.query_api('updateVirtualMachine', **args_instance_update)
if 'errortext' in res:
self.module.fail_json(msg="Failed: '%s'" % res['errortext'])
instance = res['virtualmachine'] instance = res['virtualmachine']
self.instance = instance self.instance = instance
@ -800,10 +721,7 @@ class AnsibleCloudStackInstance(AnsibleCloudStack):
args_ssh_key['id'] = instance['id'] args_ssh_key['id'] = instance['id']
args_ssh_key['projectid'] = self.get_project(key='id') args_ssh_key['projectid'] = self.get_project(key='id')
args_ssh_key['keypair'] = self.module.params.get('ssh_key') args_ssh_key['keypair'] = self.module.params.get('ssh_key')
instance = self.cs.resetSSHKeyForVirtualMachine(**args_ssh_key) instance = self.query_api('resetSSHKeyForVirtualMachine', **args_ssh_key)
if 'errortext' in instance:
self.module.fail_json(msg="Failed: '%s'" % instance['errortext'])
instance = self.poll_job(instance, 'virtualmachine') instance = self.poll_job(instance, 'virtualmachine')
self.instance = instance self.instance = instance
@ -815,35 +733,27 @@ class AnsibleCloudStackInstance(AnsibleCloudStack):
"Use force=true to allow the instance %s to be stopped/started." % instance['name']) "Use force=true to allow the instance %s to be stopped/started." % instance['name'])
return instance return instance
def recover_instance(self, instance): def recover_instance(self, instance):
if instance['state'].lower() in ['destroying', 'destroyed']: if instance['state'].lower() in ['destroying', 'destroyed']:
self.result['changed'] = True self.result['changed'] = True
if not self.module.check_mode: if not self.module.check_mode:
res = self.cs.recoverVirtualMachine(id=instance['id']) res = self.query_api('recoverVirtualMachine', id=instance['id'])
if 'errortext' in res:
self.module.fail_json(msg="Failed: '%s'" % res['errortext'])
instance = res['virtualmachine'] instance = res['virtualmachine']
return instance return instance
def absent_instance(self): def absent_instance(self):
instance = self.get_instance() instance = self.get_instance()
if instance: if instance:
if instance['state'].lower() not in ['expunging', 'destroying', 'destroyed']: if instance['state'].lower() not in ['expunging', 'destroying', 'destroyed']:
self.result['changed'] = True self.result['changed'] = True
if not self.module.check_mode: if not self.module.check_mode:
res = self.cs.destroyVirtualMachine(id=instance['id']) res = self.query_api('destroyVirtualMachine', id=instance['id'])
if 'errortext' in res:
self.module.fail_json(msg="Failed: '%s'" % res['errortext'])
poll_async = self.module.params.get('poll_async') poll_async = self.module.params.get('poll_async')
if poll_async: if poll_async:
instance = self.poll_job(res, 'virtualmachine') instance = self.poll_job(res, 'virtualmachine')
return instance return instance
def expunge_instance(self): def expunge_instance(self):
instance = self.get_instance() instance = self.get_instance()
if instance: if instance:
@ -851,22 +761,18 @@ class AnsibleCloudStackInstance(AnsibleCloudStack):
if instance['state'].lower() in ['destroying', 'destroyed']: if instance['state'].lower() in ['destroying', 'destroyed']:
self.result['changed'] = True self.result['changed'] = True
if not self.module.check_mode: if not self.module.check_mode:
res = self.cs.destroyVirtualMachine(id=instance['id'], expunge=True) res = self.query_api('destroyVirtualMachine', id=instance['id'], expunge=True)
elif instance['state'].lower() not in ['expunging']: elif instance['state'].lower() not in ['expunging']:
self.result['changed'] = True self.result['changed'] = True
if not self.module.check_mode: if not self.module.check_mode:
res = self.cs.destroyVirtualMachine(id=instance['id'], expunge=True) res = self.query_api('destroyVirtualMachine', id=instance['id'], expunge=True)
if res and 'errortext' in res:
self.module.fail_json(msg="Failed: '%s'" % res['errortext'])
poll_async = self.module.params.get('poll_async') poll_async = self.module.params.get('poll_async')
if poll_async: if poll_async:
res = self.poll_job(res, 'virtualmachine') res = self.poll_job(res, 'virtualmachine')
return instance return instance
def stop_instance(self): def stop_instance(self):
instance = self.get_instance() instance = self.get_instance()
# in check mode instance may not be instanciated # in check mode instance may not be instanciated
@ -877,17 +783,13 @@ class AnsibleCloudStackInstance(AnsibleCloudStack):
if instance['state'].lower() in ['starting', 'running']: if instance['state'].lower() in ['starting', 'running']:
self.result['changed'] = True self.result['changed'] = True
if not self.module.check_mode: if not self.module.check_mode:
instance = self.cs.stopVirtualMachine(id=instance['id']) instance = self.query_api('stopVirtualMachine', id=instance['id'])
if 'errortext' in instance:
self.module.fail_json(msg="Failed: '%s'" % instance['errortext'])
poll_async = self.module.params.get('poll_async') poll_async = self.module.params.get('poll_async')
if poll_async: if poll_async:
instance = self.poll_job(instance, 'virtualmachine') instance = self.poll_job(instance, 'virtualmachine')
return instance return instance
def start_instance(self): def start_instance(self):
instance = self.get_instance() instance = self.get_instance()
# in check mode instance may not be instanciated # in check mode instance may not be instanciated
@ -898,17 +800,13 @@ class AnsibleCloudStackInstance(AnsibleCloudStack):
if instance['state'].lower() in ['stopped', 'stopping']: if instance['state'].lower() in ['stopped', 'stopping']:
self.result['changed'] = True self.result['changed'] = True
if not self.module.check_mode: if not self.module.check_mode:
instance = self.cs.startVirtualMachine(id=instance['id']) instance = self.query_api('startVirtualMachine', id=instance['id'])
if 'errortext' in instance:
self.module.fail_json(msg="Failed: '%s'" % instance['errortext'])
poll_async = self.module.params.get('poll_async') poll_async = self.module.params.get('poll_async')
if poll_async: if poll_async:
instance = self.poll_job(instance, 'virtualmachine') instance = self.poll_job(instance, 'virtualmachine')
return instance return instance
def restart_instance(self): def restart_instance(self):
instance = self.get_instance() instance = self.get_instance()
# in check mode instance may not be instanciated # in check mode instance may not be instanciated
@ -916,10 +814,7 @@ class AnsibleCloudStackInstance(AnsibleCloudStack):
if instance['state'].lower() in ['running', 'starting']: if instance['state'].lower() in ['running', 'starting']:
self.result['changed'] = True self.result['changed'] = True
if not self.module.check_mode: if not self.module.check_mode:
instance = self.cs.rebootVirtualMachine(id=instance['id']) instance = self.query_api('rebootVirtualMachine', id=instance['id'])
if 'errortext' in instance:
self.module.fail_json(msg="Failed: '%s'" % instance['errortext'])
poll_async = self.module.params.get('poll_async') poll_async = self.module.params.get('poll_async')
if poll_async: if poll_async:
@ -929,7 +824,6 @@ class AnsibleCloudStackInstance(AnsibleCloudStack):
instance = self.start_instance() instance = self.start_instance()
return instance return instance
def restore_instance(self): def restore_instance(self):
instance = self.get_instance() instance = self.get_instance()
self.result['changed'] = True self.result['changed'] = True
@ -938,16 +832,13 @@ class AnsibleCloudStackInstance(AnsibleCloudStack):
args = {} args = {}
args['templateid'] = self.get_template_or_iso(key='id') args['templateid'] = self.get_template_or_iso(key='id')
args['virtualmachineid'] = instance['id'] args['virtualmachineid'] = instance['id']
res = self.cs.restoreVirtualMachine(**args) res = self.query_api('restoreVirtualMachine', **args)
if 'errortext' in res:
self.module.fail_json(msg="Failed: '%s'" % res['errortext'])
poll_async = self.module.params.get('poll_async') poll_async = self.module.params.get('poll_async')
if poll_async: if poll_async:
instance = self.poll_job(res, 'virtualmachine') instance = self.poll_job(res, 'virtualmachine')
return instance return instance
def get_result(self, instance): def get_result(self, instance):
super(AnsibleCloudStackInstance, self).get_result(instance) super(AnsibleCloudStackInstance, self).get_result(instance)
if instance: if instance:
@ -972,37 +863,37 @@ class AnsibleCloudStackInstance(AnsibleCloudStack):
def main(): def main():
argument_spec = cs_argument_spec() argument_spec = cs_argument_spec()
argument_spec.update(dict( argument_spec.update(dict(
name = dict(default=None), name=dict(),
display_name = dict(default=None), display_name=dict(),
group = dict(default=None), group=dict(),
state=dict(choices=['present', 'deployed', 'started', 'stopped', 'restarted', 'restored', 'absent', 'destroyed', 'expunged'], default='present'), state=dict(choices=['present', 'deployed', 'started', 'stopped', 'restarted', 'restored', 'absent', 'destroyed', 'expunged'], default='present'),
service_offering = dict(default=None), service_offering=dict(),
cpu = dict(default=None, type='int'), cpu=dict(type='int'),
cpu_speed = dict(default=None, type='int'), cpu_speed=dict(type='int'),
memory = dict(default=None, type='int'), memory=dict(type='int'),
template = dict(default=None), template=dict(),
iso = dict(default=None), iso=dict(),
template_filter=dict(default="executable", aliases=['iso_filter'], choices=['featured', 'self', 'selfexecutable', 'sharedexecutable', 'executable', template_filter=dict(default="executable", aliases=['iso_filter'], choices=['featured', 'self', 'selfexecutable', 'sharedexecutable', 'executable',
'community']), 'community']),
networks = dict(type='list', aliases=[ 'network' ], default=None), networks=dict(type='list', aliases=['network']),
ip_to_networks = dict(type='list', aliases=['ip_to_network'], default=None), ip_to_networks=dict(type='list', aliases=['ip_to_network']),
ip_address=dict(defaul=None), ip_address=dict(defaul=None),
ip6_address=dict(defaul=None), ip6_address=dict(defaul=None),
disk_offering = dict(default=None), disk_offering=dict(),
disk_size = dict(type='int', default=None), disk_size=dict(type='int'),
root_disk_size = dict(type='int', default=None), root_disk_size=dict(type='int'),
keyboard = dict(choices=['de', 'de-ch', 'es', 'fi', 'fr', 'fr-be', 'fr-ch', 'is', 'it', 'jp', 'nl-be', 'no', 'pt', 'uk', 'us'], default=None), keyboard=dict(choices=['de', 'de-ch', 'es', 'fi', 'fr', 'fr-be', 'fr-ch', 'is', 'it', 'jp', 'nl-be', 'no', 'pt', 'uk', 'us']),
hypervisor = dict(choices=CS_HYPERVISORS, default=None), hypervisor=dict(choices=CS_HYPERVISORS),
security_groups = dict(type='list', aliases=[ 'security_group' ], default=None), security_groups=dict(type='list', aliases=['security_group']),
affinity_groups = dict(type='list', aliases=[ 'affinity_group' ], default=[]), affinity_groups=dict(type='list', aliases=['affinity_group']),
domain = dict(default=None), domain=dict(),
account = dict(default=None), account=dict(),
project = dict(default=None), project=dict(),
user_data = dict(default=None), user_data=dict(),
zone = dict(default=None), zone=dict(),
ssh_key = dict(default=None), ssh_key=dict(),
force=dict(type='bool', default=False), force=dict(type='bool', default=False),
tags = dict(type='list', aliases=[ 'tag' ], default=None), tags=dict(type='list', aliases=['tag']),
poll_async=dict(type='bool', default=True), poll_async=dict(type='bool', default=True),
)) ))
@ -1023,7 +914,6 @@ def main():
supports_check_mode=True supports_check_mode=True
) )
try:
acs_instance = AnsibleCloudStackInstance(module) acs_instance = AnsibleCloudStackInstance(module)
state = module.params.get('state') state = module.params.get('state')
@ -1057,10 +947,6 @@ def main():
module.fail_json(msg="Instance named '%s' in error state." % module.params.get('name')) module.fail_json(msg="Instance named '%s' in error state." % module.params.get('name'))
result = acs_instance.get_result(instance) result = acs_instance.get_result(instance)
except CloudStackException as e:
module.fail_json(msg='CloudStackException: %s' % str(e))
module.exit_json(**result) module.exit_json(**result)

View file

@ -64,7 +64,6 @@ lib/ansible/modules/cloud/azure/azure_rm_virtualmachine.py
lib/ansible/modules/cloud/azure/azure_rm_virtualnetwork.py lib/ansible/modules/cloud/azure/azure_rm_virtualnetwork.py
lib/ansible/modules/cloud/azure/azure_rm_virtualnetwork_facts.py lib/ansible/modules/cloud/azure/azure_rm_virtualnetwork_facts.py
lib/ansible/modules/cloud/centurylink/clc_loadbalancer.py lib/ansible/modules/cloud/centurylink/clc_loadbalancer.py
lib/ansible/modules/cloud/cloudstack/cs_instance.py
lib/ansible/modules/cloud/cloudstack/_cs_nic.py lib/ansible/modules/cloud/cloudstack/_cs_nic.py
lib/ansible/modules/cloud/docker/_docker.py lib/ansible/modules/cloud/docker/_docker.py
lib/ansible/modules/cloud/docker/docker_container.py lib/ansible/modules/cloud/docker/docker_container.py