VMware: Tag various VMware objects (#54033)
* tag portgroups and dvs * Add support for datacenter tagging * Support for tagging clusters * Hostsystem tag option * pep recommendations * Remove Vmware from VmwareDistributedSwitch object type * Fix typos in example tasks of vmware_tag_manager
This commit is contained in:
parent
8ac22098d9
commit
5feadbef4a
1 changed files with 54 additions and 5 deletions
|
@ -24,6 +24,7 @@ description:
|
|||
version_added: 2.8
|
||||
author:
|
||||
- Abhijeet Kasurde (@Akasurde)
|
||||
- Frederic Van Reet (@GBrawl)
|
||||
notes:
|
||||
- Tested on vSphere 6.5
|
||||
requirements:
|
||||
|
@ -49,10 +50,11 @@ options:
|
|||
description:
|
||||
- Type of object to work with.
|
||||
required: True
|
||||
choices: [ VirtualMachine ]
|
||||
choices: [ VirtualMachine, Datacenter, ClusterComputeResource, HostSystem, DistributedVirtualSwitch, DistributedVirtualPortgroup ]
|
||||
object_name:
|
||||
description:
|
||||
- Name of the object to work with.
|
||||
- For DistributedVirtualPortgroups the format should be "switch_name:portgroup_name"
|
||||
required: True
|
||||
extends_documentation_fragment: vmware_rest_client.documentation
|
||||
'''
|
||||
|
@ -72,7 +74,7 @@ EXAMPLES = r'''
|
|||
state: add
|
||||
delegate_to: localhost
|
||||
|
||||
- name: Remove a tag to a virtual machine
|
||||
- name: Remove a tag from a virtual machine
|
||||
vmware_tag_manager:
|
||||
hostname: '{{ vcenter_hostname }}'
|
||||
username: '{{ vcenter_username }}'
|
||||
|
@ -84,6 +86,32 @@ EXAMPLES = r'''
|
|||
object_type: VirtualMachine
|
||||
state: remove
|
||||
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'''
|
||||
|
@ -105,10 +133,9 @@ tag_status:
|
|||
]
|
||||
}
|
||||
'''
|
||||
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
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:
|
||||
from com.vmware.vapi.std_client import DynamicID
|
||||
from com.vmware.cis.tagging_client import Tag, TagAssociation, Category
|
||||
|
@ -131,6 +158,26 @@ class VmwareTagManager(VmwareRestClient):
|
|||
if self.object_type == 'VirtualMachine':
|
||||
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:
|
||||
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),
|
||||
state=dict(type='str', choices=['absent', 'add', 'present', 'remove', 'set'], default='add'),
|
||||
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)
|
||||
|
||||
|
|
Loading…
Reference in a new issue