diff --git a/changelogs/fragments/65765-vmware_tag_manager.yml b/changelogs/fragments/65765-vmware_tag_manager.yml new file mode 100644 index 00000000000000..81155dfae97165 --- /dev/null +++ b/changelogs/fragments/65765-vmware_tag_manager.yml @@ -0,0 +1,2 @@ +bugfixes: +- Added support to vmware_tag_manager module for specifying tag and category as dict if any of the name contains colon (https://github.com/ansible/ansible/issues/65765). diff --git a/lib/ansible/modules/cloud/vmware/vmware_tag_manager.py b/lib/ansible/modules/cloud/vmware/vmware_tag_manager.py index fa4a523a189ed4..770ff61c6ef620 100644 --- a/lib/ansible/modules/cloud/vmware/vmware_tag_manager.py +++ b/lib/ansible/modules/cloud/vmware/vmware_tag_manager.py @@ -35,8 +35,10 @@ tag_names: description: - List of tag(s) to be managed. - - You can also specify category name by specifying colon separated value. For example, "category_name:tag_name". - - You can skip category name if you have unique tag names. + - User can also specify category name by specifying colon separated value. For example, "category_name:tag_name". + - User can also specify tag and category as dict, when tag or category contains colon. + See example for more information. Added in version 2.10. + - User can skip category name if you have unique tag names. required: True type: list state: @@ -77,6 +79,22 @@ state: add delegate_to: localhost +- name: Specify tag and category as dict + vmware_tag_manager: + hostname: '{{ vcenter_hostname }}' + username: '{{ vcenter_username }}' + password: '{{ vcenter_password }}' + validate_certs: no + tag_names: + - tag: tag_0001 + category: cat_0001 + - tag: tag_0002 + category: cat_0002 + object_name: Fedora_VM + object_type: VirtualMachine + state: add + delegate_to: localhost + - name: Remove a tag from a virtual machine vmware_tag_manager: hostname: '{{ vcenter_hostname }}' @@ -218,15 +236,24 @@ def ensure_state(self): removed_tags_for_set = False for tag in self.tag_names: category_obj, category_name, tag_name = None, None, None - if ":" in tag: - # User specified category - category_name, tag_name = tag.split(":", 1) - category_obj = self.search_svc_object_by_name(self.category_service, category_name) - if not category_obj: - self.module.fail_json(msg="Unable to find the category %s" % category_name) - else: - # User specified only tag - tag_name = tag + if isinstance(tag, dict): + tag_name = tag.get('tag') + category_name = tag.get('category') + if category_name: + # User specified category + category_obj = self.search_svc_object_by_name(self.category_service, category_name) + if not category_obj: + self.module.fail_json(msg="Unable to find the category %s" % category_name) + elif isinstance(tag, str): + if ":" in tag: + # User specified category + category_name, tag_name = tag.split(":", 1) + category_obj = self.search_svc_object_by_name(self.category_service, category_name) + if not category_obj: + self.module.fail_json(msg="Unable to find the category %s" % category_name) + else: + # User specified only tag + tag_name = tag if category_name: tag_obj = self.get_tag_by_category(tag_name=tag_name, category_name=category_name) diff --git a/test/integration/targets/vmware_tag/tasks/main.yml b/test/integration/targets/vmware_tag/tasks/main.yml index 8961c6916a646e..a657f5463f4cec 100644 --- a/test/integration/targets/vmware_tag/tasks/main.yml +++ b/test/integration/targets/vmware_tag/tasks/main.yml @@ -5,3 +5,4 @@ - include: tag_crud_ops.yml - include: tag_manager_ops.yml - include: tag_manager_duplicate_tag_cat.yml +- include: tag_manager_dict.yml diff --git a/test/integration/targets/vmware_tag/tasks/tag_manager_dict.yml b/test/integration/targets/vmware_tag/tasks/tag_manager_dict.yml new file mode 100644 index 00000000000000..0b414552aef367 --- /dev/null +++ b/test/integration/targets/vmware_tag/tasks/tag_manager_dict.yml @@ -0,0 +1,113 @@ +# Test code for the vmware_tag_manager Operations. +# Copyright: (c) 2020, Abhijeet Kasurde +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +# Testcase for https://github.com/ansible/ansible/issues/65765 +- when: vcsim is not defined + block: + - name: Create first category + vmware_category: + hostname: '{{ vcenter_hostname }}' + username: '{{ vcenter_username }}' + password: '{{ vcenter_password }}' + validate_certs: no + category_name: "{{ cat_one }}" + category_cardinality: 'multiple' + state: present + register: category_one_create + + - name: Set category one id + set_fact: cat_one_id={{ category_one_create['category_results']['category_id'] }} + + - name: Create tag with colon in name + vmware_tag: + hostname: '{{ vcenter_hostname }}' + username: '{{ vcenter_username }}' + password: '{{ vcenter_password }}' + validate_certs: no + tag_name: "{{ tag_one }}" + category_id: "{{ cat_one_id }}" + state: present + register: tag_one_create + + - name: Check tag is created + assert: + that: + - tag_one_create.changed + + - name: Get VM Facts + vmware_vm_info: + hostname: '{{ vcenter_hostname }}' + username: '{{ vcenter_username }}' + password: '{{ vcenter_password }}' + validate_certs: False + register: vm_info + + - set_fact: vm_name="{{ vm_info['virtual_machines'][0]['guest_name'] }}" + + - name: Assign tag to given virtual machine + vmware_tag_manager: + hostname: '{{ vcenter_hostname }}' + username: '{{ vcenter_username }}' + password: '{{ vcenter_password }}' + validate_certs: no + tag_names: + - category: "{{ cat_one }}" + tag: "{{ tag_one }}" + object_name: "{{ vm_name }}" + object_type: VirtualMachine + state: add + delegate_to: localhost + register: vm_tag_info + + - name: Check if we assigned correct tags + assert: + that: + - vm_tag_info.changed + + - name: Remove tag to given virtual machine + vmware_tag_manager: + hostname: '{{ vcenter_hostname }}' + username: '{{ vcenter_username }}' + password: '{{ vcenter_password }}' + validate_certs: no + tag_names: + - category: "{{ cat_one }}" + tag: "{{ tag_one }}" + object_name: "{{ vm_name }}" + object_type: VirtualMachine + state: remove + delegate_to: localhost + register: vm_tag_info + + - name: Check if we removed correct tag + assert: + that: + - vm_tag_info.changed + + - name: Delete Tags + vmware_tag: + hostname: '{{ vcenter_hostname }}' + username: '{{ vcenter_username }}' + password: '{{ vcenter_password }}' + validate_certs: no + tag_name: "{{ item }}" + state: absent + register: delete_tag + with_items: + - "{{ tag_one }}" + + - name: Delete Categories + vmware_category: + hostname: '{{ vcenter_hostname }}' + username: '{{ vcenter_username }}' + password: '{{ vcenter_password }}' + validate_certs: no + category_name: "{{ item }}" + state: absent + register: delete_categories + with_items: + - "{{ cat_one }}" + vars: + cat_one: category_1003 + tag_one: tag:1003