-
Notifications
You must be signed in to change notification settings - Fork 31
/
get_all_tags_from_virtual_machine.yml
57 lines (50 loc) · 1.71 KB
/
get_all_tags_from_virtual_machine.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
---
- name: Gather all tags attached to the given virtual machine
gather_facts: no
vars_files:
- vcenter_vars.yml
hosts: localhost
tasks:
- name: Login task
include_tasks: login.yml
- name: Get virtual machine id for {{ vm_name }}
uri:
url: https://{{ vcenter_server }}/rest/vcenter/vm?filter.names={{ vm_name }}
force_basic_auth: yes
validate_certs: "{{ validate_certs }}"
headers:
Cookie: "{{ login.set_cookie }}"
register: vVMs
- set_fact:
vm_id: "{{ vVMs.json.value[0].vm }}"
- debug:
msg: "{{ vm_id }}"
when: debug_flag
- name: Get tags attached to virtual machine {{ vm_name }}
uri:
url: https://{{ vcenter_server }}/rest/com/vmware/cis/tagging/tag-association?~action=list-attachable-tags
force_basic_auth: yes
validate_certs: "{{ validate_certs }}"
method: POST
body_format: json
body:
object_id:
id: '{{ vm_id }}'
type: 'VirtualMachine'
headers:
Cookie: "{{ login.set_cookie }}"
register: vTags
- name: Get tag names for collected tags
uri:
url: https://{{ vcenter_server }}/rest//com/vmware/cis/tagging/tag/id:{{ item }}
force_basic_auth: yes
validate_certs: "{{ validate_certs }}"
headers:
Cookie: "{{ login.set_cookie }}"
register: attached_tags
with_items: "{{ vTags.json.value }}"
- name: Create a list of tags which is associated with {{ vm_name }}
set_fact: vm_tag_list="{{ vm_tag_list|default([]) + [ item.json.value.name ] }}"
with_items: "{{ attached_tags.results }}"
- debug:
msg: "{{ vm_tag_list }}"