VMware: Add support for specify tag and category as dict
User can now specify tag and category using dict in vmware_tag_manager module. This is useful when tag or category name contains colon. Fixes: #65765 Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
parent
33d5c68887
commit
7000c51c06
5 changed files with 161 additions and 11 deletions
2
changelogs/fragments/65765-vmware_tag_manager.yml
Normal file
2
changelogs/fragments/65765-vmware_tag_manager.yml
Normal file
|
@ -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).
|
|
@ -35,8 +35,10 @@ options:
|
||||||
tag_names:
|
tag_names:
|
||||||
description:
|
description:
|
||||||
- List of tag(s) to be managed.
|
- List of tag(s) to be managed.
|
||||||
- You can also specify category name by specifying colon separated value. For example, "category_name:tag_name".
|
- User 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 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
|
required: True
|
||||||
type: list
|
type: list
|
||||||
state:
|
state:
|
||||||
|
@ -77,6 +79,22 @@ EXAMPLES = r'''
|
||||||
state: add
|
state: add
|
||||||
delegate_to: localhost
|
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
|
- name: Remove a tag from a virtual machine
|
||||||
vmware_tag_manager:
|
vmware_tag_manager:
|
||||||
hostname: '{{ vcenter_hostname }}'
|
hostname: '{{ vcenter_hostname }}'
|
||||||
|
@ -218,6 +236,15 @@ class VmwareTagManager(VmwareRestClient):
|
||||||
removed_tags_for_set = False
|
removed_tags_for_set = False
|
||||||
for tag in self.tag_names:
|
for tag in self.tag_names:
|
||||||
category_obj, category_name, tag_name = None, None, None
|
category_obj, category_name, tag_name = None, None, None
|
||||||
|
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:
|
if ":" in tag:
|
||||||
# User specified category
|
# User specified category
|
||||||
category_name, tag_name = tag.split(":", 1)
|
category_name, tag_name = tag.split(":", 1)
|
||||||
|
|
3
test/integration/targets/vmware_tag_manager/aliases
Normal file
3
test/integration/targets/vmware_tag_manager/aliases
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
cloud/vcenter
|
||||||
|
unsupported
|
||||||
|
zuul/vmware/vcenter_only
|
|
@ -0,0 +1,5 @@
|
||||||
|
# Test code for the vmware_tag_manger Operations.
|
||||||
|
# Copyright: (c) 2020, Abhijeet Kasurde <akasurde@redhat.com>
|
||||||
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||||
|
|
||||||
|
- include: tag_manager_dict.yml
|
|
@ -0,0 +1,113 @@
|
||||||
|
# Test code for the vmware_tag_manager Operations.
|
||||||
|
# Copyright: (c) 2020, Abhijeet Kasurde <akasurde@redhat.com>
|
||||||
|
# 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
|
Loading…
Reference in a new issue