-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathprovisioning.yml
175 lines (158 loc) · 5.32 KB
/
provisioning.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
---
# This should work on Ansible >2.2
# First, Do all of the provisioning and add the hosts to in-memory inventory
- hosts: all
gather_facts: false
connection: local
vars:
ansible_python_interpreter: "{{ansible_playbook_python}}"
tasks:
- name: "INCLUDES | Include vaulted credentials"
include_vars: group_vars/vaulted.yml
no_log: true
- name: "PROVISION | Create VM from template"
vmware_guest:
validate_certs: "{{ validate_certs }}"
hostname: "{{ vcenter_host }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
datacenter: "{{ datacenter }}"
name: "{{ name }}"
folder: "{{ folder }}"
template: "{{ vmtemplate }}"
state: poweredon
annotation: '{{ vmnotes|default("Nothing significant to report") }}'
cluster: "{{ cluster|default('homefarm_cluster') }}"
hardware:
num_cpus: "{{ cpu }}"
memory_mb: "{{ mem_mb }}"
disk:
- size_gb: "{{ disk }}"
type: thin
datastore: "{{ datastore }}"
networks:
- name: "{{ port_grp }}"
wait_for_ip_address: yes
register: dyn_vm
when: net_type == 'dhcp'
- name: "PROVISION | Create VM from template"
vmware_guest:
validate_certs: "{{ validate_certs }}"
hostname: "{{ vcenter_host }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
datacenter: "{{ datacenter }}"
name: "{{ name }}"
folder: "{{ folder }}"
template: "{{ vmtemplate }}"
state: poweredon
annotation: '{{ vmnotes|default("Nothing significant to report") }}'
cluster: "{{ cluster|default('homefarm_cluster') }}"
hardware:
num_cpus: "{{ cpu }}"
memory_mb: "{{ mem_mb }}"
disk:
- size_gb: "{{ disk }}"
type: thin
datastore: "{{ datastore }}"
networks:
- name: "{{ port_grp }}"
type: "{{ net_type }}"
ip: "{{ server_ip }}"
netmask: "{{ netmask }}"
gateway: "{{ gateway }}"
wait_for_ip_address: yes
register: stat_vm
when: net_type == 'static'
- name: "COMBINE | Gather dynamic and static VMs into one var"
set_fact:
new_vm: "{{ dyn_vm|combine(stat_vm) }}"
- debug:
msg: "{{ new_vm }}"
# Notes from reading the vmware_guest network code block:
# Either name or a VLAN must be specified
# type must be in static or dynamic AND
# if you set type to dynamic but then specify any addressing info it will fail
# Type is optional parameter, if user provided IP or Subnet assume
# network type as 'static'
# If type static you must specify an IP and netmask
# Although MAC is included in all of the examples, it appears to be optional
# if a MAC is supplied, PyVmomiDeviceHelper function will be called to validate that it is valid
- name: "INVENTORY | Update temp inventory with new host"
add_host:
hostname: "{{ item.hw_name }}"
ansible_host: "{{ item.ipv4 }}"
groups: stage1
with_items:
- "{{ new_vm.instance }}"
# - "{{ new_vm2.instance }}"
# Second, reconfigure networking as necessary
- hosts: stage1
gather_facts: true
connection: smart
become: true
tasks:
- name: "NETWORK | Configure the ifcfg file for the machine we just created"
template:
src: ifcfg.j2
dest: /etc/sysconfig/network-scripts/ifcfg-ens192
owner: root
group: root
mode: 0644
when: net_type == 'dhcp'
- name: "INVENTORY | Update the inventory file (case == DHCP)"
lineinfile:
line: "{{ name }} ansible_host={{ new_vm.instance.ipv4 }}"
path: "~/Documents/gitworking/ansible-vmware-provisioning/hosts"
regexp: "{{ name }}.*"
state: present
validate: 'ansible-inventory --list -i %s'
delegate_to: localhost
# when: net_type == 'dhcp'
become: false
# - name: "INVENTORY | Update the inventory file (case == static addressing)"
# lineinfile:
# line: "{{ name }} ansible_host={{ server_ip }}"
# path: "~/Documents/gitworking/ansible-vmware-provisioning/hosts"
# regexp: "{{ name }}.*"
# state: present
# validate: 'ansible-inventory --list -i %s'
# delegate_to: localhost
# when: server_ip != 'dhcp'
# become: false
# Need to use this instead of the new reboot module because the IP changes in the middle of task exec
- name: "REBOOT | Reboot to ensure network settings take effect"
vmware_vm_shell:
validate_certs: "{{ validate_certs }}"
hostname: "{{ vcenter_host }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
datacenter: "{{ datacenter }}"
vm_id: "{{ new_vm.instance.hw_product_uuid }}"
vm_id_type: uuid
vm_username: root
vm_password: "{{ rooter }}"
vm_shell: /sbin/shutdown
vm_shell_args: " -r now"
delegate_to: localhost
when: net_type != 'dhcp'
become: false
- name: "REBOOT | Wait for server to come back"
local_action:
module: wait_for
host: "{{ server_ip }}"
port: 22
state: started
delay: 30
timeout: 300
when: net_type != 'dhcp'
become: false
# Finally, apply some common role stuff (including install pexpect), and then do disk resizing
- hosts: all
gather_facts: true
connection: smart
become: true
roles:
- role: common
- include: disk_resize.yml
when: disk > 10