@ -40,7 +40,12 @@ options:
vm :
vm :
description :
description :
- " Name of the Virtual Machine to manage. "
- " Name of the Virtual Machine to manage. "
required : true
- " You must provide either C(vm) parameter or C(template) parameter. "
template :
description :
- " Name of the template to manage. "
- " You must provide either C(vm) parameter or C(template) parameter. "
version_added : " 2.4 "
state :
state :
description :
description :
- " Should the Virtual Machine NIC be present/absent/plugged/unplugged. "
- " Should the Virtual Machine NIC be present/absent/plugged/unplugged. "
@ -90,11 +95,24 @@ EXAMPLES = '''
vm : myvm
vm : myvm
name : mynic
name : mynic
# add NIC to template
- ovirt_nics :
auth : " {{ ovirt_auth }} "
state : present
template : my_template
name : nic1
interface : virtio
profile : ovirtmgmt
network : ovirtmgmt
# Remove NIC from VM
# Remove NIC from VM
- ovirt_nics :
- ovirt_nics :
state : absent
state : absent
vm : myvm
vm : myvm
name : mynic
name : mynic
'''
'''
RETURN = '''
RETURN = '''
@ -129,10 +147,10 @@ from ansible.module_utils.ovirt import (
)
)
class Vm NicsModule( BaseModule ) :
class Entity NicsModule( BaseModule ) :
def __init__ ( self , * args , * * kwargs ) :
def __init__ ( self , * args , * * kwargs ) :
super ( Vm NicsModule, self ) . __init__ ( * args , * * kwargs )
super ( Entity NicsModule, self ) . __init__ ( * args , * * kwargs )
self . vnic_id = None
self . vnic_id = None
@property
@property
@ -158,11 +176,17 @@ class VmNicsModule(BaseModule):
)
)
def update_check ( self , entity ) :
def update_check ( self , entity ) :
return (
if self . _module . params . get ( ' vm ' ) :
equal ( self . _module . params . get ( ' interface ' ) , str ( entity . interface ) ) and
return (
equal ( self . _module . params . get ( ' profile ' ) , get_link_name ( self . _connection , entity . vnic_profile ) ) and
equal ( self . _module . params . get ( ' interface ' ) , str ( entity . interface ) ) and
equal ( self . _module . params . get ( ' mac_address ' ) , entity . mac . address )
equal ( self . _module . params . get ( ' profile ' ) , get_link_name ( self . _connection , entity . vnic_profile ) ) and
)
equal ( self . _module . params . get ( ' mac_address ' ) , entity . mac . address )
)
elif self . _module . params . get ( ' template ' ) :
return (
equal ( self . _module . params . get ( ' interface ' ) , str ( entity . interface ) ) and
equal ( self . _module . params . get ( ' profile ' ) , get_link_name ( self . _connection , entity . vnic_profile ) )
)
def main ( ) :
def main ( ) :
@ -171,7 +195,8 @@ def main():
choices = [ ' present ' , ' absent ' , ' plugged ' , ' unplugged ' ] ,
choices = [ ' present ' , ' absent ' , ' plugged ' , ' unplugged ' ] ,
default = ' present '
default = ' present '
) ,
) ,
vm = dict ( required = True ) ,
vm = dict ( required = False ) ,
template = dict ( required = False ) ,
name = dict ( required = True ) ,
name = dict ( required = True ) ,
interface = dict ( default = None ) ,
interface = dict ( default = None ) ,
profile = dict ( default = None ) ,
profile = dict ( default = None ) ,
@ -181,6 +206,7 @@ def main():
module = AnsibleModule (
module = AnsibleModule (
argument_spec = argument_spec ,
argument_spec = argument_spec ,
supports_check_mode = True ,
supports_check_mode = True ,
required_one_of = [ [ ' vm ' , ' template ' ] ]
)
)
check_sdk ( module )
check_sdk ( module )
@ -189,18 +215,26 @@ def main():
# search for the NIC:
# search for the NIC:
auth = module . params . pop ( ' auth ' )
auth = module . params . pop ( ' auth ' )
connection = create_connection ( auth )
connection = create_connection ( auth )
vms_service = connection . system_service ( ) . vms_service ( )
entity_name = None
# Locate the VM, where we will manage NICs:
if module . params . get ( ' vm ' ) :
vm_name = module . params . get ( ' vm ' )
# Locate the VM, where we will manage NICs:
vm = search_by_name ( vms_service , vm_name )
entity_name = module . params . get ( ' vm ' )
if vm is None :
collection_service = connection . system_service ( ) . vms_service ( )
raise Exception ( " VM ' %s ' was not found. " % vm_name )
elif module . params . get ( ' template ' ) :
entity_name = module . params . get ( ' template ' )
# Locate the service that manages the virtual machines NICs:
collection_service = connection . system_service ( ) . templates_service ( )
vm_service = vms_service . vm_service ( vm . id )
nics_service = vm_service . nics_service ( )
# TODO: We have to modify the search_by_name function to accept raise_error=True/False,
vmnics_module = VmNicsModule (
entity = search_by_name ( collection_service , entity_name )
if entity is None :
raise Exception ( " Vm/Template ' %s ' was not found. " % entity_name )
service = collection_service . service ( entity . id )
cluster_id = entity . cluster
nics_service = service . nics_service ( )
entitynics_module = EntityNicsModule (
connection = connection ,
connection = connection ,
module = module ,
module = module ,
service = nics_service ,
service = nics_service ,
@ -209,7 +243,7 @@ def main():
# Find vNIC id of the network interface (if any):
# Find vNIC id of the network interface (if any):
profile = module . params . get ( ' profile ' )
profile = module . params . get ( ' profile ' )
if profile and module . params [ ' network ' ] :
if profile and module . params [ ' network ' ] :
cluster_name = get_link_name ( connection , vm. cluster)
cluster_name = get_link_name ( connection , cluster_id )
dcs_service = connection . system_service ( ) . data_centers_service ( )
dcs_service = connection . system_service ( ) . data_centers_service ( )
dc = dcs_service . list ( search = ' Clusters.name= %s ' % cluster_name ) [ 0 ]
dc = dcs_service . list ( search = ' Clusters.name= %s ' % cluster_name ) [ 0 ]
networks_service = dcs_service . service ( dc . id ) . networks_service ( )
networks_service = dcs_service . service ( dc . id ) . networks_service ( )
@ -227,24 +261,24 @@ def main():
)
)
for vnic in connection . system_service ( ) . vnic_profiles_service ( ) . list ( ) :
for vnic in connection . system_service ( ) . vnic_profiles_service ( ) . list ( ) :
if vnic . name == profile and vnic . network . id == network . id :
if vnic . name == profile and vnic . network . id == network . id :
vm nics_module. vnic_id = vnic . id
entity nics_module. vnic_id = vnic . id
# Handle appropriate action:
# Handle appropriate action:
state = module . params [ ' state ' ]
state = module . params [ ' state ' ]
if state == ' present ' :
if state == ' present ' :
ret = vm nics_module. create ( )
ret = entity nics_module. create ( )
elif state == ' absent ' :
elif state == ' absent ' :
ret = vm nics_module. remove ( )
ret = entity nics_module. remove ( )
elif state == ' plugged ' :
elif state == ' plugged ' :
vm nics_module. create ( )
entity nics_module. create ( )
ret = vm nics_module. action (
ret = entity nics_module. action (
action = ' activate ' ,
action = ' activate ' ,
action_condition = lambda nic : not nic . plugged ,
action_condition = lambda nic : not nic . plugged ,
wait_condition = lambda nic : nic . plugged ,
wait_condition = lambda nic : nic . plugged ,
)
)
elif state == ' unplugged ' :
elif state == ' unplugged ' :
vm nics_module. create ( )
entity nics_module. create ( )
ret = vm nics_module. action (
ret = entity nics_module. action (
action = ' deactivate ' ,
action = ' deactivate ' ,
action_condition = lambda nic : nic . plugged ,
action_condition = lambda nic : nic . plugged ,
wait_condition = lambda nic : not nic . plugged ,
wait_condition = lambda nic : not nic . plugged ,