-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathensure-vm-state.yml
147 lines (135 loc) · 4.71 KB
/
ensure-vm-state.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
---
- name: Ensure VM state
hosts: localhost
gather_facts: false
vars:
name: delete_this_machine_anytime
vm_state: poweredon
memory_mb: 2048
num_cpus: 2
num_cpu_cores_per_socket: 1
template: rhel_8_5_image
net_name: RH-seg-2991
net_type: dhcp # dhcp vs. static. If static, fix below values
# ip: 10.128.1.256
# netmask: 255.255.255.0
# gateway: 10.128.1.1
# domain: cool.lab
# dns_servers:
# - 10.128.1.10
# - 10.128.1.11
use_aap_inventory: false
tasks:
- name: >
Use vmware credentials from vault if set there, otherwise
from AAP credentials
set_fact:
vcenter_credentials:
vcenter_server: '{{ lookup("env", "VMWARE_HOST") }}'
vcenter_username: '{{ lookup("env", "VMWARE_USER") }}'
vcenter_password: '{{ lookup("env", "VMWARE_PASSWORD") }}'
when:
- vcenter_credentials is not defined
- vcenter_credentials.vcenter_server is not defined
- vcenter_credentials.vcenter_username is not defined
- vcenter_credentials.vcenter_password is not defined
- name: when not deleting vm
when: vm_state != 'absent'
block:
- name: Clone the template
community.vmware.vmware_guest:
name: "{{ short_name }}"
state: "{{ vm_state }}"
hostname: "{{ vcenter_credentials.vcenter_server }}"
username: "{{ vcenter_credentials.vcenter_username }}"
password: "{{ vcenter_credentials.vcenter_password }}"
validate_certs: false
template: "{{ template }}"
datacenter: "{{ vcenter_datacenter }}"
folder: "{{ vcenter_folder }}"
cluster: "{{ vcenter_cluster }}"
wait_for_ip_address: true
hardware:
memory_mb: "{{ memory_mb }}"
num_cpus: "{{ num_cpus }}"
num_cpu_cores_per_socket: "{{ num_cpu_cores_per_socket }}"
networks:
- name: "{{ net_name }}"
type: "{{ net_type }}"
ip: "{{ ip | default(omit) }}"
netmask: "{{ netmask | default(omit) }}"
gateway: "{{ gateway | default(omit) }}"
wait_for_ip_address_timeout: 60
connected: true
start_connected: true
customization:
domain: "{{ domain | default(omit) }}"
dns_servers: "{{ dns_servers | default(omit) }}"
register: vm
- name: Save Server IP Address for future use
set_stats:
data:
server_ip: "{{ vm.instance.ipv4 }}"
server_name: "{{ short_name }}"
when: vm is succeeded
- name: Create tower inventory
awx.awx.inventory:
name: "Temporary Inventory"
description: "Temporary inventory for hosts to be created"
organization: "Default"
state: present
validate_certs: false
when:
- use_aap_inventory
- vm is succeeded
- name: Add tower host
awx.awx.host:
name: "{{ short_name }}"
inventory: "Temporary Inventory"
state: present
validate_certs: false
variables:
ansible_host: "{{ vm.instance.ipv4 }}"
when:
- use_aap_inventory
- vm is succeeded
- name: Add host to temporary group for new vms
awx.awx.group:
name: new_vms
inventory: "Temporary Inventory"
hosts:
- "{{ short_name }}"
validate_certs: false
preserve_existing_hosts: true
when:
- use_aap_inventory
- vm is succeeded
- name: Wait for Virtual Machine to boot up and become responsive to SSH
wait_for:
port: 22
host: "{{ vm.instance.ipv4 }}"
search_regex: OpenSSH
delay: 10
timeout: 600
when: vm is succeeded
- name: Delete the VM
community.vmware.vmware_guest:
name: "{{ short_name }}"
hostname: "{{ vcenter_credentials.vcenter_server }}"
username: "{{ vcenter_credentials.vcenter_username }}"
password: "{{ vcenter_credentials.vcenter_password }}"
validate_certs: false
datacenter: "{{ vcenter_datacenter }}"
folder: "{{ vcenter_folder }}"
state: "{{ vm_state }}"
cluster: "{{ vcenter_cluster }}"
force: true
when: vm_state == 'absent'
- name: vm info
debug:
var: vm
when: vm_state != 'absent'
- name: store vm ip
set_fact:
host_ip: "{{ vm.instance.ipv4 }}"
when: vm_state != 'absent'