@ -24,6 +24,7 @@ description:
version_added : 2.8
version_added : 2.8
author :
author :
- Abhijeet Kasurde ( @Akasurde )
- Abhijeet Kasurde ( @Akasurde )
- Frederic Van Reet ( @GBrawl )
notes :
notes :
- Tested on vSphere 6.5
- Tested on vSphere 6.5
requirements :
requirements :
@ -49,10 +50,11 @@ options:
description :
description :
- Type of object to work with .
- Type of object to work with .
required : True
required : True
choices : [ VirtualMachine ]
choices : [ VirtualMachine , Datacenter , ClusterComputeResource , HostSystem , DistributedVirtualSwitch , DistributedVirtualPortgroup ]
object_name :
object_name :
description :
description :
- Name of the object to work with .
- Name of the object to work with .
- For DistributedVirtualPortgroups the format should be " switch_name:portgroup_name "
required : True
required : True
extends_documentation_fragment : vmware_rest_client . documentation
extends_documentation_fragment : vmware_rest_client . documentation
'''
'''
@ -72,7 +74,7 @@ EXAMPLES = r'''
state : add
state : add
delegate_to : localhost
delegate_to : localhost
- name : Remove a tag to a virtual machine
- name : Remove a tag from a virtual machine
vmware_tag_manager :
vmware_tag_manager :
hostname : ' {{ vcenter_hostname }} '
hostname : ' {{ vcenter_hostname }} '
username : ' {{ vcenter_username }} '
username : ' {{ vcenter_username }} '
@ -84,6 +86,32 @@ EXAMPLES = r'''
object_type : VirtualMachine
object_type : VirtualMachine
state : remove
state : remove
delegate_to : localhost
delegate_to : localhost
- name : Add tags to a distributed virtual switch
vmware_tag_manager :
hostname : ' {{ vcenter_hostname }} '
username : ' {{ vcenter_username }} '
password : ' {{ vcenter_password }} '
validate_certs : no
tag_names :
- Sample_Tag_0003
object_name : Switch_0001
object_type : DistributedVirtualSwitch
state : add
delegate_to : localhost
- name : Add tags to a distributed virtual portgroup
vmware_tag_manager :
hostname : ' {{ vcenter_hostname }} '
username : ' {{ vcenter_username }} '
password : ' {{ vcenter_password }} '
validate_certs : no
tag_names :
- Sample_Tag_0004
object_name : Switch_0001 : Portgroup_0001
object_type : DistributedVirtualPortgroup
state : add
delegate_to : localhost
'''
'''
RETURN = r '''
RETURN = r '''
@ -105,10 +133,9 @@ tag_status:
]
]
}
}
'''
'''
from ansible . module_utils . basic import AnsibleModule
from ansible . module_utils . basic import AnsibleModule
from ansible . module_utils . vmware_rest_client import VmwareRestClient
from ansible . module_utils . vmware_rest_client import VmwareRestClient
from ansible . module_utils . vmware import PyVmomi
from ansible . module_utils . vmware import ( PyVmomi , find_dvs_by_name , find_dvspg_by_name )
try :
try :
from com . vmware . vapi . std_client import DynamicID
from com . vmware . vapi . std_client import DynamicID
from com . vmware . cis . tagging_client import Tag , TagAssociation , Category
from com . vmware . cis . tagging_client import Tag , TagAssociation , Category
@ -131,6 +158,26 @@ class VmwareTagManager(VmwareRestClient):
if self . object_type == ' VirtualMachine ' :
if self . object_type == ' VirtualMachine ' :
self . managed_object = self . pyv . get_vm_or_template ( self . object_name )
self . managed_object = self . pyv . get_vm_or_template ( self . object_name )
if self . object_type == ' Datacenter ' :
self . managed_object = self . pyv . find_datacenter_by_name ( self . object_name )
if self . object_type == ' ClusterComputeResource ' :
self . managed_object = self . pyv . find_cluster_by_name ( self . object_name )
if self . object_type == ' HostSystem ' :
self . managed_object = self . pyv . find_hostsystem_by_name ( self . object_name )
if self . object_type == ' DistributedVirtualSwitch ' :
self . managed_object = find_dvs_by_name ( self . pyv . content , self . object_name )
self . object_type = ' VmwareDistributedVirtualSwitch '
if self . object_type == ' DistributedVirtualPortgroup ' :
dvs_name , pg_name = self . object_name . split ( " : " , 1 )
dv_switch = find_dvs_by_name ( self . pyv . content , dvs_name )
if dv_switch is None :
self . module . fail_json ( msg = " A distributed virtual switch with name %s does not exist " % dvs_name )
self . managed_object = find_dvspg_by_name ( dv_switch , pg_name )
if self . managed_object is None :
if self . managed_object is None :
self . module . fail_json ( msg = " Failed to find the managed object for %s with type %s " % ( self . object_name , self . object_type ) )
self . module . fail_json ( msg = " Failed to find the managed object for %s with type %s " % ( self . object_name , self . object_type ) )
@ -222,7 +269,9 @@ def main():
tag_names = dict ( type = ' list ' , required = True ) ,
tag_names = dict ( type = ' list ' , required = True ) ,
state = dict ( type = ' str ' , choices = [ ' absent ' , ' add ' , ' present ' , ' remove ' , ' set ' ] , default = ' add ' ) ,
state = dict ( type = ' str ' , choices = [ ' absent ' , ' add ' , ' present ' , ' remove ' , ' set ' ] , default = ' add ' ) ,
object_name = dict ( type = ' str ' , required = True ) ,
object_name = dict ( type = ' str ' , required = True ) ,
object_type = dict ( type = ' str ' , required = True , choices = [ ' VirtualMachine ' ] ) ,
object_type = dict ( type = ' str ' , required = True , choices = [ ' VirtualMachine ' , ' Datacenter ' , ' ClusterComputeResource ' ,
' HostSystem ' , ' DistributedVirtualSwitch ' ,
' DistributedVirtualPortgroup ' ] ) ,
)
)
module = AnsibleModule ( argument_spec = argument_spec )
module = AnsibleModule ( argument_spec = argument_spec )