-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path006.poweron_vm_prompt.yml
More file actions
66 lines (60 loc) · 1.96 KB
/
006.poweron_vm_prompt.yml
File metadata and controls
66 lines (60 loc) · 1.96 KB
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
- name: Power On All the Virtual Machines
hosts: localhost
gather_facts: no
collections:
- community.vmware
vars_prompt:
- name: "esxi_user"
prompt: "Enter ESXi username"
private: no
default: "root"
- name: "esxi_password"
prompt: "Enter ESXi password"
private: yes
- name: "esxi_host"
prompt: "Enter ESXi hostname or IP"
private: no
default: "192.168.1.3"
- name: "datacenter"
prompt: "Enter vSphere datacenter name"
private: no
default: "ha-datacenter"
- name: "vms_to_power_on"
prompt: |
Available VMs:
{% set vm_list = lookup('file', 'vmware_vm_report.txt').split('\n')
| select('match', '^\\|\\s+[^\\+\\-\\s].*')
| map('regex_search', '^\\|\\s+([a-zA-Z0-9\\-_]+)\\s+', '\\1')
| map('first')
| list %}
{{ vm_list | join(', ') }}
Enter comma-separated VM names to power on from the above list (or 'all' to power on all):
private: no
tasks:
- name: Get VM list from report file
set_fact:
all_vms: "{{ lookup('file', 'vmware_vm_report.txt').split('\n')
| select('match', '^\\|\\s+[^\\+\\-\\s].*')
| map('regex_search', '^\\|\\s+([a-zA-Z0-9\\-_]+)\\s+', '\\1')
| map('first')
| list }}"
- name: Determine VMs to power on based on input
set_fact:
vm_list: >-
{{
('all' in (vms_to_power_on | lower))
| ternary(all_vms, vms_to_power_on.split(',') | map('trim') | list)
}}
- name: Debug vm_list
debug:
var: vm_list
- name: Power on specified VMs
vmware.vmware.vm_powerstate:
hostname: "{{ esxi_host }}"
username: "{{ esxi_user }}"
password: "{{ esxi_password }}"
validate_certs: no
datacenter: "{{ datacenter }}"
name: "{{ item }}"
state: powered-on
loop: "{{ vm_list }}"