-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathvmware_manage_tag.yml
67 lines (66 loc) · 2.16 KB
/
vmware_manage_tag.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
- name: Manage vSphere VM Tags
hosts: localhost
gather_facts: no
vars:
tag_category: Protection
tag_name: App01
#vm_datacenter: lab
#vm_cluster: cluster01
#vm_folder: /{{ vm_datacenter }}/vm/Ansible
vm_list:
- { vm: 'vm01', type: 'VirtualMachine', category: '{{ tag_category }}', tag: '{{ tag_name }}' }
- { vm: 'vm02', type: 'VirtualMachine', category: '{{ tag_category }}', tag: '{{ tag_name }}'}
tasks:
- name: Get category id from the given tag
vmware_tag_facts:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
validate_certs: no
register: tag_details
tags: debug
- name: Create Tag for '{{ tag_name }}'
vmware_tag:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
validate_certs: no
category_id: "{{ tag_details.tag_facts['Default']['tag_category_id'] }}"
tag_name: '{{ tag_name }}'
state: present
- name: Add Tag '{{ tag_name }}' to VMs
vmware_tag_manager:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
validate_certs: no
tag_names:
- "{{ item.category }}:{{ item.tag }}"
object_name: "{{ item.vm }}"
object_type: "{{ item.type }}"
state: add
with_items: "{{ vm_list }}"
register: tag_return
tags: add
- debug:
msg: "{{ item }}"
with_items: "{{ tag_return.results }}"
tags: debug
- name: Remove Tag
vmware_tag_manager:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
validate_certs: no
tag_names:
- "{{ item.category }}:{{ item.tag }}"
object_name: "{{ item.vm }}"
object_type: "{{ item.type }}"
state: remove
with_items: "{{ vm_list }}"
register: tag_return
tags: remove
- debug:
msg: "{{ item }}"
with_items: "{{ tag_return.results }}"
tags: debug