forked from brahmanim/playbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tag_vm.yml
70 lines (61 loc) · 1.91 KB
/
tag_vm.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
68
69
70
---
- hosts: localhost
vars:
vm: "vms/1000000000029"
tags:
- { category: "department", name: "finance"}
- { category: "lifecycle", name: "retire_full"}
tasks:
- debug: var=manageiq.api_url
- debug: var=manageiq.api_token
- name: Set the single vm tag URL
set_fact:
vm_tag_url: "{{ manageiq.api_url }}/api/{{ vm }}/tags"
- debug: var=vm_tag_url
- name: Set the tags for a single vm
uri:
url: "{{ vm_tag_url }}"
method: POST
body_format: json
body:
action: assign
resources : "{{ tags }}"
validate_certs: False
headers:
X-Auth-Token: "{{ manageiq.api_token }}"
Content-Type: "application/json"
status_code: 200
register: output
- debug: var=output.json.results[0].success
- name: Check if the VM was successfully tagged
fail: msg="{{output.json.results[0].message}}"
when: output.json.results[0].success == false
# Bulk tagging
#
- name: Set the bulk tag URL
set_fact:
bulk_tag_url: "{{ manageiq.api_url }}/api/vms"
- name: Set the array for bulk tagging
set_fact:
bulk_tags: "{{ bulk_tags|default([]) + [ { 'href': item, 'tags': tags } ] }}"
with_items:
- "api/{{ vm }}"
- "api/vms/1000000000045"
- name: Set the bulk tag
uri:
url: "{{ bulk_tag_url }}"
method: POST
body_format: json
body:
action: assign_tags
resources : "{{ bulk_tags }}"
validate_certs: False
headers:
X-Auth-Token: "{{ manageiq.api_token }}"
Content-Type: "application/json"
status_code: 200
register: output
- debug: var=output.json.results[0].success
- name: Check if the VM was successfully bulk tagged
fail: msg="{{output.json.results[0].message}}"
when: output.json.results[0].success == false