@ -25,6 +25,7 @@ short_description: create, terminate, start or stop a vm in vca
description :
description :
- Creates or terminates vca vms .
- Creates or terminates vca vms .
version_added : " 2.0 "
version_added : " 2.0 "
author : Peter Sprygada ( @privateip )
options :
options :
username :
username :
description :
description :
@ -87,11 +88,6 @@ options:
- The network name to which the vm should be attached .
- The network name to which the vm should be attached .
required : false
required : false
default : ' None '
default : ' None '
network_ip :
description :
- The ip address that should be assigned to vm when the ip assignment type is static
required : false
default : None
network_mode :
network_mode :
description :
description :
- The network mode in which the ip should be allocated .
- The network mode in which the ip should be allocated .
@ -103,16 +99,6 @@ options:
- The instance id of the region in vca flavour where the vm should be created
- The instance id of the region in vca flavour where the vm should be created
required : false
required : false
default : None
default : None
wait :
description :
- If the module should wait if the operation is poweroff or poweron , is better to wait to report the right state .
required : false
default : True
wait_timeout :
description :
- The wait timeout when wait is set to true
required : false
default : 250
vdc_name :
vdc_name :
description :
description :
- The name of the vdc where the vm should be created .
- The name of the vdc where the vm should be created .
@ -133,23 +119,6 @@ options:
- The amount of memory to be added to vm in megabytes
- The amount of memory to be added to vm in megabytes
required : false
required : false
default : None
default : None
verify_certs :
description :
- If the certificates of the authentication is to be verified
required : false
default : True
admin_password :
description :
- The password to be set for admin
required : false
default : None
operation :
description :
- The operation to be done on the vm
required : false
default : poweroff
choices : [ ' shutdown ' , ' poweroff ' , ' poweron ' , ' reboot ' , ' reset ' , ' suspend ' ]
'''
'''
EXAMPLES = '''
EXAMPLES = '''
@ -207,519 +176,151 @@ EXAMPLES = '''
template_name : " CentOS 6.5 64Bit CLI "
template_name : " CentOS 6.5 64Bit CLI "
network_mode : pool
network_mode : pool
'''
'''
import time , json , xmltodict
HAS_PYVCLOUD = False
try :
try :
from pyvcloud . vcloudair import VCA
from pyvcloud . vcloudair import VCA
HAS_PYVCLOUD = True
HAS_PYVCLOUD = True
except ImportError :
except ImportError :
pass
HAS_PYVCLOUD = False
SERVICE_MAP = { ' vca ' : ' ondemand ' , ' vchs ' : ' subscription ' , ' vcd ' : ' vcd ' }
VAPP_STATE_MAP = {
LOGIN_HOST = { }
' poweron ' : ' Powered on ' ,
LOGIN_HOST [ ' vca ' ] = ' vca.vmware.com '
' poweroff ' : ' Powered off ' ,
LOGIN_HOST [ ' vchs ' ] = ' vchs.vmware.com '
' reboot ' : None ,
VM_COMPARE_KEYS = [ ' admin_password ' , ' status ' , ' cpus ' , ' memory_mb ' ]
' reset ' : None ,
' shutdown ' : ' Powered off ' ,
def vm_state ( val = None ) :
' suspend ' : ' Suspended ' ,
if val == 8 :
' absent ' : None
return " Power_Off "
}
elif val == 4 :
return " Power_On "
def modify_vapp ( vapp , module ) :
else :
vm_name = module . params [ ' vm_name ' ]
return " Unknown Status "
vm_cpus = module . params [ ' vm_cpus ' ]
vm_memory = module . params [ ' vm_memory ' ]
def serialize_instances ( instance_list ) :
instances = [ ]
changed = False
for i in instance_list :
instances . append ( dict ( apiUrl = i [ ' apiUrl ' ] , instance_id = i [ ' id ' ] ) )
try :
return instances
vm = vapp . get_vms_details ( ) [ 0 ]
except IndexError :
def get_catalogs ( vca ) :
raise VcaError ( ' No VM provisioned for vapp ' )
catalogs = vca . get_catalogs ( )
results = [ ]
if vm [ ' status ' ] != ' Powered off ' :
for catalog in catalogs :
raise VcaError ( ' vApp must be powered off to modify ' )
if catalog . CatalogItems and catalog . CatalogItems . CatalogItem :
for item in catalog . CatalogItems . CatalogItem :
if vm_cpus != vm [ ' cpus ' ] and vm_cpus is not None :
results . append ( [ catalog . name , item . name ] )
if not module . check_mode :
else :
task = vapp . modify_vm_cpu ( vm_name , vm_cpus )
results . append ( [ catalog . name , ' ' ] )
changed = True
return results
if vm_memory != vm [ ' memory_mb ' ] and vm_memory is not None :
def vca_login ( module = None ) :
if not module . check_mode :
service_type = module . params . get ( ' service_type ' )
task = vca . modify_vm_memory ( vm_name , vm_memory )
username = module . params . get ( ' username ' )
changed = True
password = module . params . get ( ' password ' )
instance = module . params . get ( ' instance_id ' )
return changed
org = module . params . get ( ' org ' )
service = module . params . get ( ' service_id ' )
vdc_name = module . params . get ( ' vdc_name ' )
def set_vapp_state ( vapp , state ) :
version = module . params . get ( ' api_version ' )
vm = vapp . get_vms_details ( ) [ 0 ]
verify = module . params . get ( ' verify_certs ' )
try :
if not vdc_name :
if vm [ ' status ' ] != VAPP_STATE_MAP [ state ] :
if service_type == ' vchs ' :
func = getattr ( vm , state )
vdc_name = module . params . get ( ' service_id ' )
func ( )
if not org :
except KeyError :
if service_type == ' vchs ' :
raise VcaError ( ' unknown vapp state ' , state = str ( state ) , vm = str ( vm ) )
if vdc_name :
org = vdc_name
else :
def create_vapp ( vca , module ) :
org = service
vdc_name = module . params [ ' vdc_name ' ]
if service_type == ' vcd ' :
vapp_name = module . params [ ' vapp_name ' ]
host = module . params . get ( ' host ' )
template_name = module . params [ ' template_name ' ]
else :
catalog_name = module . params [ ' catalog_name ' ]
host = LOGIN_HOST [ service_type ]
network_name = module . params [ ' network_name ' ]
network_mode = module . params [ ' network_mode ' ]
if not username :
vm_name = module . params [ ' vm_name ' ]
if ' VCA_USER ' in os . environ :
vm_cpus = module . params [ ' vm_cpus ' ]
username = os . environ [ ' VCA_USER ' ]
vm_memory = module . params [ ' vm_memory ' ]
if not password :
deploy = module . params [ ' deploy ' ]
if ' VCA_PASS ' in os . environ :
password = os . environ [ ' VCA_PASS ' ]
task = vca . create_vapp ( vdc_name , vapp_name , template_name , catalog_name ,
if not username or not password :
network_name , network_mode , vm_name , vm_cpus ,
module . fail_json ( msg = " Either the username or password is not set, please check " )
vm_memory , deploy , False )
if service_type == ' vchs ' :
vca . block_until_completed ( task )
version = ' 5.6 '
if service_type == ' vcd ' :
return vca . get_vapp ( vca . get_vdc ( vdc_name ) , vapp_name )
if not version :
version == ' 5.6 '
def remove_vapp ( vca , module ) :
vdc_name = module . params [ ' vdc_name ' ]
vapp_name = module . params [ ' vapp_name ' ]
vca = VCA ( host = host , username = username , service_type = SERVICE_MAP [ service_type ] , version = version , verify = verify )
if not vca . delete_vapp ( vdc_name , vapp_name ) :
raise VcaError ( ' unable to delete %s from %s ' % ( vapp_name , vdc_name ) )
if service_type == ' vca ' :
if not vca . login ( password = password ) :
module . fail_json ( msg = " Login Failed: Please check username or password " , error = vca . response . content )
if not vca . login_to_instance ( password = password , instance = instance , token = None , org_url = None ) :
s_json = serialize_instances ( vca . instances )
module . fail_json ( msg = " Login to Instance failed: Seems like instance_id provided is wrong .. Please check " , \
valid_instances = s_json )
if not vca . login_to_instance ( instance = instance , password = None , token = vca . vcloud_session . token ,
org_url = vca . vcloud_session . org_url ) :
module . fail_json ( msg = " Error logging into org for the instance " , error = vca . response . content )
return vca
if service_type == ' vchs ' :
if not vca . login ( password = password ) :
module . fail_json ( msg = " Login Failed: Please check username or password " , error = vca . response . content )
if not vca . login ( token = vca . token ) :
module . fail_json ( msg = " Failed to get the token " , error = vca . response . content )
if not vca . login_to_org ( service , org ) :
module . fail_json ( msg = " Failed to login to org, Please check the orgname " , error = vca . response . content )
return vca
if service_type == ' vcd ' :
if not vca . login ( password = password , org = org ) :
module . fail_json ( msg = " Login Failed: Please check username or password or host parameters " )
if not vca . login ( password = password , org = org ) :
module . fail_json ( msg = " Failed to get the token " , error = vca . response . content )
if not vca . login ( token = vca . token , org = org , org_url = vca . vcloud_session . org_url ) :
module . fail_json ( msg = " Failed to login to org " , error = vca . response . content )
return vca
def set_vm_state ( module = None , vca = None , state = None ) :
wait = module . params . get ( ' wait ' )
wait_tmout = module . params . get ( ' wait_timeout ' )
vm_name = module . params . get ( ' vm_name ' )
vdc_name = module . params . get ( ' vdc_name ' )
vapp_name = module . params . get ( ' vm_name ' )
service_type = module . params . get ( ' service_type ' )
service_id = module . params . get ( ' service_id ' )
if service_type == ' vchs ' and not vdc_name :
vdc_name = service_id
vdc = vca . get_vdc ( vdc_name )
if wait :
tmout = time . time ( ) + wait_tmout
while tmout > time . time ( ) :
vapp = vca . get_vapp ( vdc , vapp_name )
vms = filter ( lambda vm : vm [ ' name ' ] == vm_name , vapp . get_vms_details ( ) )
vm = vms [ 0 ]
if vm [ ' status ' ] == state :
return True
time . sleep ( 5 )
module . fail_json ( msg = " Timeut waiting for the vms state to change " )
return True
def vm_details ( vdc = None , vapp = None , vca = None ) :
table = [ ]
networks = [ ]
vm_name = vapp
vdc1 = vca . get_vdc ( vdc )
if not vdc1 :
module . fail_json ( msg = " Error getting the vdc, Please check the vdc name " )
vap = vca . get_vapp ( vdc1 , vapp )
if vap :
vms = filter ( lambda vm : vm [ ' name ' ] == vm_name , vap . get_vms_details ( ) )
networks = vap . get_vms_network_info ( )
if len ( networks [ 0 ] ) > = 1 :
table . append ( dict ( vm_info = vms [ 0 ] , network_info = networks [ 0 ] [ 0 ] ) )
else :
table . append ( dict ( vm_info = vms [ 0 ] , network_info = networks [ 0 ] ) )
return table
def vapp_attach_net ( module = None , vca = None , vapp = None ) :
network_name = module . params . get ( ' network_name ' )
service_type = module . params . get ( ' service_type ' )
vdc_name = module . params . get ( ' vdc_name ' )
mode = module . params . get ( ' network_mode ' )
if mode . upper ( ) == ' STATIC ' :
network_ip = module . params . get ( ' network_ip ' )
else :
network_ip = None
if not vdc_name :
if service_type == ' vchs ' :
vdc_name = module . params . get ( ' service_id ' )
nets = filter ( lambda n : n . name == network_name , vca . get_networks ( vdc_name ) )
if len ( nets ) < = 1 :
net_task = vapp . disconnect_vms ( )
if not net_task :
module . fail_json ( msg = " Failure in detattaching vms from vnetworks " , error = vapp . response . content )
if not vca . block_until_completed ( net_task ) :
module . fail_json ( msg = " Failure in waiting for detaching vms from vnetworks " , error = vapp . response . content )
net_task = vapp . disconnect_from_networks ( )
if not net_task :
module . fail_json ( msg = " Failure in detattaching network from vapp " , error = vapp . response . content )
if not vca . block_until_completed ( net_task ) :
module . fail_json ( msg = " Failure in waiting for detaching network from vapp " , error = vapp . response . content )
if not network_name :
return True
net_task = vapp . connect_to_network ( nets [ 0 ] . name , nets [ 0 ] . href )
if not net_task :
module . fail_json ( msg = " Failure in attaching network to vapp " , error = vapp . response . content )
if not vca . block_until_completed ( net_task ) :
module . fail_json ( msg = " Failure in waiting for attching network to vapp " , error = vapp . response . content )
net_task = vapp . connect_vms ( nets [ 0 ] . name , connection_index = 0 , ip_allocation_mode = mode . upper ( ) , ip_address = network_ip )
if not net_task :
module . fail_json ( msg = " Failure in attaching network to vm " , error = vapp . response . content )
if not vca . block_until_completed ( net_task ) :
module . fail_json ( msg = " Failure in waiting for attaching network to vm " , error = vapp . response . content )
return True
nets = [ ]
for i in vca . get_networks ( vdc_name ) :
nets . append ( i . name )
module . fail_json ( msg = " Seems like network_name is not found in the vdc, please check Available networks as above " , Available_networks = nets )
def create_vm ( vca = None , module = None ) :
vm_name = module . params . get ( ' vm_name ' )
operation = module . params . get ( ' operation ' )
vm_cpus = module . params . get ( ' vm_cpus ' )
vm_memory = module . params . get ( ' vm_memory ' )
catalog_name = module . params . get ( ' catalog_name ' )
template_name = module . params . get ( ' template_name ' )
vdc_name = module . params . get ( ' vdc_name ' )
network_name = module . params . get ( ' network_name ' )
service_type = module . params . get ( ' service_type ' )
admin_pass = module . params . get ( ' admin_password ' )
script = module . params . get ( ' script ' )
vapp_name = vm_name
if not vdc_name :
if service_type == ' vchs ' :
vdc_name = module . params . get ( ' service_id ' )
task = vca . create_vapp ( vdc_name , vapp_name , template_name , catalog_name , vm_name = None )
if not task :
catalogs = get_catalogs ( vca )
module . fail_json ( msg = " Error in Creating VM, Please check catalog or template, Available catalogs and templates are as above or check the error field " , catalogs = catalogs , errors = vca . response . content )
if not vca . block_until_completed ( task ) :
module . fail_json ( msg = " Error in waiting for VM Creation, Please check logs " , errors = vca . response . content )
vdc = vca . get_vdc ( vdc_name )
if not vdc :
module . fail_json ( msg = " Error getting the vdc, Please check the vdc name " , errors = vca . response . content )
vapp = vca . get_vapp ( vdc , vapp_name )
task = vapp . modify_vm_name ( 1 , vm_name )
if not task :
module . fail_json ( msg = " Error in setting the vm_name to vapp_name " , errors = vca . response . content )
if not vca . block_until_completed ( task ) :
module . fail_json ( msg = " Error in waiting for VM Renaming, Please check logs " , errors = vca . response . content )
vapp = vca . get_vapp ( vdc , vapp_name )
task = vapp . customize_guest_os ( vm_name , computer_name = vm_name )
if not task :
module . fail_json ( msg = " Error in setting the computer_name to vm_name " , errors = vca . response . content )
if not vca . block_until_completed ( task ) :
module . fail_json ( msg = " Error in waiting for Computer Renaming, Please check logs " , errors = vca . response . content )
if network_name :
vapp = vca . get_vapp ( vdc , vapp_name )
if not vapp_attach_net ( module , vca , vapp ) :
module . fail_json ( msg = " Attaching network to VM fails " , errors = vca . response . content )
if vm_cpus :
vapp = vca . get_vapp ( vdc , vapp_name )
task = vapp . modify_vm_cpu ( vm_name , vm_cpus )
if not task :
module . fail_json ( msg = " Error adding cpu " , error = vapp . resonse . contents )
if not vca . block_until_completed ( task ) :
module . fail_json ( msg = " Failure in waiting for modifying cpu " , error = vapp . response . content )
if vm_memory :
vapp = vca . get_vapp ( vdc , vapp_name )
task = vapp . modify_vm_memory ( vm_name , vm_memory )
if not task :
module . fail_json ( msg = " Error adding memory " , error = vapp . resonse . contents )
if not vca . block_until_completed ( task ) :
module . fail_json ( msg = " Failure in waiting for modifying memory " , error = vapp . response . content )
if admin_pass :
vapp = vca . get_vapp ( vdc , vapp_name )
task = vapp . customize_guest_os ( vm_name , customization_script = None ,
computer_name = None , admin_password = admin_pass ,
reset_password_required = False )
if not task :
module . fail_json ( msg = " Error adding admin password " , error = vapp . resonse . contents )
if not vca . block_until_completed ( task ) :
module . fail_json ( msg = " Error in waiting for resettng admin pass, Please check logs " , errors = vapp . response . content )
if script :
vapp = vca . get_vapp ( vdc , vapp_name )
if os . path . exists ( os . path . expanduser ( script ) ) :
file_contents = open ( script , ' r ' )
task = vapp . customize_guest_os ( vm_name , customization_script = file_contents . read ( ) )
if not task :
module . fail_json ( msg = " Error adding customization script " , error = vapp . resonse . contents )
if not vca . block_until_completed ( task ) :
module . fail_json ( msg = " Error in waiting for customization script, please check logs " , errors = vapp . response . content )
task = vapp . force_customization ( vm_name , power_on = False )
if not task :
module . fail_json ( msg = " Error adding customization script " , error = vapp . resonse . contents )
if not vca . block_until_completed ( task ) :
module . fail_json ( msg = " Error in waiting for customization script, please check logs " , errors = vapp . response . content )
else :
module . fail_json ( msg = " The file specified in script paramter is not avaialable or accessible " )
vapp = vca . get_vapp ( vdc , vapp_name )
if operation == ' poweron ' :
vapp . poweron ( )
set_vm_state ( module , vca , state = ' Powered on ' )
elif operation == ' poweroff ' :
vapp . poweroff ( )
elif operation == ' reboot ' :
vapp . reboot ( )
elif operation == ' reset ' :
vapp . reset ( )
elif operation == ' suspend ' :
vapp . suspend ( )
elif operation == ' shutdown ' :
vapp . shutdown ( )
details = vm_details ( vdc_name , vapp_name , vca )
module . exit_json ( changed = True , msg = " VM created " , vm_details = details [ 0 ] )
def vapp_reconfigure ( module = None , diff = None , vm = None , vca = None , vapp = None , vdc_name = None ) :
flag = False
vapp_name = module . params . get ( ' vm_name ' )
vm_name = module . params . get ( ' vm_name ' )
cpus = module . params . get ( ' vm_cpus ' )
memory = module . params . get ( ' vm_memory ' )
admin_pass = module . params . get ( ' admin_password ' )
if ' status ' in diff :
operation = module . params . get ( ' operation ' )
if operation == ' poweroff ' :
vapp . poweroff ( )
set_vm_state ( module , vca , state = ' Powered off ' )
flag = True
if ' network ' in diff :
vapp_attach_net ( module , vca , vapp )
flag = True
if ' cpus ' in diff :
task = vapp . modify_vm_cpu ( vm_name , cpus )
if not vca . block_until_completed ( task ) :
module . fail_json ( msg = " Failure in waiting for modifying cpu, might be vm is powered on and doesnt support hotplugging " , error = vapp . response . content )
flag = True
if ' memory_mb ' in diff :
task = vapp . modify_vm_memory ( vm_name , memory )
if not vca . block_until_completed ( task ) :
module . fail_json ( msg = " Failure in waiting for modifying memory, might be vm is powered on and doesnt support hotplugging " , error = vapp . response . content )
flag = True
if ' admin_password ' in diff :
task = vapp . customize_guest_os ( vm_name , customization_script = None ,
computer_name = None , admin_password = admin_pass ,
reset_password_required = False )
if not task :
module . fail_json ( msg = " Error adding admin password " , error = vapp . resonse . contents )
if not vca . block_until_completed ( task ) :
module . fail_json ( msg = " Error in waiting for resettng admin pass, Please check logs " , errors = vapp . response . content )
flag = True
if ' status ' in diff :
operation = module . params . get ( ' operation ' )
if operation == ' poweron ' :
vapp . poweron ( )
set_vm_state ( module , vca , state = ' Powered on ' )
elif operation == ' reboot ' :
vapp . reboot ( )
elif operation == ' reset ' :
vapp . reset ( )
elif operation == ' suspend ' :
vapp . suspend ( )
elif operation == ' shutdown ' :
vapp . shutdown ( )
flag = True
details = vm_details ( vdc_name , vapp_name , vca )
if flag :
module . exit_json ( changed = True , msg = " VM reconfigured " , vm_details = details [ 0 ] )
module . exit_json ( changed = False , msg = " VM exists as per configuration " , \
vm_details = details [ 0 ] )
def vm_exists ( module = None , vapp = None , vca = None , vdc_name = None ) :
vm_name = module . params . get ( ' vm_name ' )
operation = module . params . get ( ' operation ' )
vm_cpus = module . params . get ( ' vm_cpus ' )
vm_memory = module . params . get ( ' vm_memory ' )
network_name = module . params . get ( ' network_name ' )
admin_pass = module . params . get ( ' admin_password ' )
d_vm = { }
d_vm [ ' name ' ] = vm_name
d_vm [ ' cpus ' ] = vm_cpus
d_vm [ ' memory_mb ' ] = vm_memory
d_vm [ ' admin_password ' ] = admin_pass
if operation == ' poweron ' :
d_vm [ ' status ' ] = ' Powered on '
elif operation == ' poweroff ' :
d_vm [ ' status ' ] = ' Powered off '
else :
d_vm [ ' status ' ] = ' operate '
vms = filter ( lambda vm : vm [ ' name ' ] == vm_name , vapp . get_vms_details ( ) )
if len ( vms ) > 1 :
module . fail_json ( msg = " The vapp seems to have more than one vm with same name, \
currently we only support a single vm deployment " )
elif len ( vms ) == 0 :
return False
else :
vm = vms [ 0 ]
diff = [ ]
for i in VM_COMPARE_KEYS :
if not d_vm [ i ] :
continue
if vm [ i ] != d_vm [ i ] :
diff . append ( i )
if len ( diff ) == 1 and ' status ' in diff :
vapp_reconfigure ( module , diff , vm , vca , vapp , vdc_name )
networks = vapp . get_vms_network_info ( )
if not network_name and len ( networks ) > = 1 :
if len ( networks [ 0 ] ) > = 1 :
if networks [ 0 ] [ 0 ] [ ' network_name ' ] != ' none ' :
diff . append ( ' network ' )
if not network_name :
if len ( diff ) == 0 :
return True
if not networks [ 0 ] and network_name :
diff . append ( ' network ' )
if networks [ 0 ] :
if len ( networks [ 0 ] ) > = 1 :
if networks [ 0 ] [ 0 ] [ ' network_name ' ] != network_name :
diff . append ( ' network ' )
if vm [ ' status ' ] != ' Powered off ' :
if operation != ' poweroff ' and len ( diff ) > 0 :
module . fail_json ( msg = " To change any properties of a vm, The vm should be in Powered Off state " )
if len ( diff ) == 0 :
return True
else :
vapp_reconfigure ( module , diff , vm , vca , vapp , vdc_name )
def main ( ) :
def main ( ) :
module = AnsibleModule (
argument_spec = vca_argument_spec ( )
argument_spec = dict (
argument_spec . update (
username = dict ( default = None ) ,
dict (
password = dict ( default = None ) ,
vdc_name = dict ( requred = True ) ,
org = dict ( default = None ) ,
vapp_name = dict ( required = True ) ,
service_id = dict ( default = None ) ,
template_name = dict ( required = True ) ,
script = dict ( default = None ) ,
catalog_name = dict ( default = ' Public Catalog ' ) ,
host = dict ( default = None ) ,
network_name = dict ( ) ,
api_version = dict ( default = ' 5.7 ' ) ,
network_mode = dict ( default = ' pool ' , choices = [ ' dhcp ' , ' static ' , ' pool ' ] ) ,
service_type = dict ( default = ' vca ' , choices = [ ' vchs ' , ' vca ' , ' vcd ' ] ) ,
vm_name = dict ( ) ,
state = dict ( default = ' present ' , choices = [ ' present ' , ' absent ' ] ) ,
vm_memory = dict ( ) ,
catalog_name = dict ( default = " Public Catalog " ) ,
vm_cpus = dict ( ) ,
template_name = dict ( default = None , required = True ) ,
deploy = dict ( default = False ) ,
network_name = dict ( default = None ) ,
state = dict ( default = ' poweron ' , choices = VAPP_STATE_MAP . keys ( ) )
network_ip = dict ( default = None ) ,
network_mode = dict ( default = ' pool ' , choices = [ ' dhcp ' , ' static ' , ' pool ' ] ) ,
instance_id = dict ( default = None ) ,
wait = dict ( default = True , type = ' bool ' ) ,
wait_timeout = dict ( default = 250 , type = ' int ' ) ,
vdc_name = dict ( default = None ) ,
vm_name = dict ( default = ' default_ansible_vm1 ' ) ,
vm_cpus = dict ( default = None , type = ' int ' ) ,
verify_certs = dict ( default = True , type = ' bool ' ) ,
vm_memory = dict ( default = None , type = ' int ' ) ,
admin_password = dict ( default = None ) ,
operation = dict ( default = ' poweroff ' , choices = [ ' shutdown ' , ' poweroff ' , ' poweron ' , ' reboot ' , ' reset ' , ' suspend ' ] )
)
)
)
)
module = AnsibleModule ( argument_spec = argument_spec , supports_check_mode = True )
vdc_name = module . params . get ( ' vdc_name ' )
vdc_name = module . params [ ' vdc_name ' ]
vm_name = module . params . get ( ' vm_name ' )
vapp_name = module . params [ ' vapp_name ' ]
org = module . params . get ( ' org ' )
state = module . params [ ' state ' ]
service = module . params . get ( ' service_id ' )
state = module . params . get ( ' state ' )
service_type = module . params . get ( ' service_type ' )
host = module . params . get ( ' host ' )
instance_id = module . params . get ( ' instance_id ' )
network_mode = module . params . get ( ' network_mode ' )
network_ip = module . params . get ( ' network_ip ' )
vapp_name = vm_name
if not HAS_PYVCLOUD :
if not HAS_PYVCLOUD :
module . fail_json ( msg = " python module pyvcloud is needed for this module " )
module . fail_json ( msg = " python module pyvcloud is needed for this module " )
if network_mode . upper ( ) == ' STATIC ' :
if not network_ip :
module . fail_json ( msg = " if network_mode is STATIC, network_ip is mandatory " )
if service_type == ' vca ' :
if not instance_id :
module . fail_json ( msg = " When service type is vca the instance_id parameter is mandatory " )
if not vdc_name :
module . fail_json ( msg = " When service type is vca the vdc_name parameter is mandatory " )
if service_type == ' vchs ' :
if not service :
module . fail_json ( msg = " When service type vchs the service_id parameter is mandatory " )
if not org :
org = service
if not vdc_name :
vdc_name = service
if service_type == ' vcd ' :
if not host :
module . fail_json ( msg = " When service type is vcd host parameter is mandatory " )
vca = vca_login ( module )
vca = vca_login ( module )
vdc = vca . get_vdc ( vdc_name )
vdc = vca . get_vdc ( vdc_name )
if not vdc :
if not vdc :
module . fail_json ( msg = " Error getting the vdc, Please check the vdc name " )
module . fail_json ( msg = " Error getting the vdc, Please check the vdc name " )
vapp = vca . get_vapp ( vdc , vapp_name )
if vapp :
if state == ' absent ' :
task = vca . delete_vapp ( vdc_name , vapp_name )
if not vca . block_until_completed ( task ) :
module . fail_json ( msg = " failure in deleting vapp " )
module . exit_json ( changed = True , msg = " Vapp deleted " )
if vm_exists ( module , vapp , vca , vdc_name ) :
details = vm_details ( vdc_name , vapp_name , vca )
module . exit_json ( changed = False , msg = " vapp exists " , vm_details = details [ 0 ] )
else :
create_vm ( vca , module )
if state == ' absent ' :
module . exit_json ( changed = False , msg = " Vapp does not exist " )
create_vm ( vca , module )
result = dict ( changed = False )
vapp = vca . get_vapp ( vdc , vapp_name )
try :
if not vapp and state != ' absent ' :
if not module . check_mode :
vapp = create_vapp ( vca , module )
set_vapp_state ( vapp , state )
result [ ' changed ' ] = True
elif vapp and state == ' absent ' :
if not module . check_mode :
remove_vapp ( vca , module )
result [ ' changed ' ] = True
elif vapp :
if not module . check_mode :
changed = modify_vapp ( vapp , module )
set_vapp_state ( vapp , state )
result [ ' changed ' ] = True
except VcaError , e :
module . fail_json ( msg = e . message , * * e . kwargs )
except Exception , e :
module . fail_json ( msg = e . message )
module . exit_json ( * * result )
# import module snippets
# import module snippets
from ansible . module_utils . basic import *
from ansible . module_utils . basic import *
from ansible . module_utils . vca import *
if __name__ == ' __main__ ' :
if __name__ == ' __main__ ' :
main ( )
main ( )