-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathvm_clone-old.yml
137 lines (118 loc) · 4.04 KB
/
vm_clone-old.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
---
# vim: set ft=ansible et ts=2 sw=2:
#
# Create a new VM from a template
- name: VM from template
hosts: vmcreate
gather_facts: false
connection: local
vars:
vcenter_hostname: vcsa.box
esxhost: esx1.box
datastore: datastore1
network: "VMnetwork"
vmtemplate: CentOS7-Base
vmcluster: LAN
notes: Created by Ansible
dumpfacts: False
tasks:
- name: Check for required variables
fail: msg="Must pass name and group to -e"
when: name is not defined or group is not defined
- name: Check for vSphere access parameters
fail: msg="Must set vcenter_user and vcenter_pass in a Vault"
when: (vcenter_user is not defined) or (vcenter_pass is not defined)
- name: Create VM from template
vsphere_guest:
vcenter_hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_user }}"
password: "{{ vcenter_pass }}"
guest: "{{ name }}"
from_template: yes
template_src: "{{ vmtemplate }}"
resource_pool: "/Resources"
vm_extra_config:
notes: "{{ notes }}"
otheridentifyinginfo:
env: "dev"
esxi:
datacenter: BOX
hostname: "{{ esxhost }}"
vm_hardware:
memory_mb: "{{ vm_memory | default(1024) }}"
- block:
- name: Wait a minute for tools to start
wait_for:
timeout: 60
changed_when: False
- name: Gather VM facts
vsphere_guest:
vcenter_hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_user }}"
password: "{{ vcenter_pass }}"
guest: "{{ name }}"
vmware_guest_facts: yes
register: newvm
- name: IP address info
debug:
msg: "{{ newvm.ansible_facts.hw_eth0.ipaddresses[0] }} {{ name }}"
# oops, we probably need to wait a little longer to grab IP address
rescue:
- name: Wait a bit longer for tools to start
wait_for:
timeout: 30
changed_when: False
- name: Gather VM facts
vsphere_guest:
vcenter_hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_user }}"
password: "{{ vcenter_pass }}"
guest: "{{ name }}"
vmware_guest_facts: yes
register: newvm
- name: IP address info
debug:
msg: "{{ newvm.ansible_facts.hw_eth0.ipaddresses[0] }} {{ name }}"
- name: Dump all facts when ask to
debug:
msg: "{{ newvm.ansible_facts }}"
when: dumpfacts
- name: Wait for ssh to be available
wait_for:
host: "{{ newvm.ansible_facts.hw_eth0.ipaddresses[0] }}"
port: 22
timeout: 600
state: started
- name: Create temporary variables file
copy:
content: "ansible_ssh_host: {{ newvm.ansible_facts.hw_eth0.ipaddresses[0] }}"
dest: "/var/tmp/{{ name }}-vars.yml"
- name: Ensure host is in Tower inventory
command: "tower-cli host modify --create-on-missing --name {{ name }} --inventory 6 --variables /var/tmp/{{ name }}-vars.yml"
- name: Associate host with group in Tower
command: "tower-cli host associate --host {{ name }} --group {{ group }}"
- name: Dump temporary variables file
file:
path: "/var/tmp/{{ name }}-vars.yml"
state: absent
- name: Add to new group
add_host:
name: "{{ newvm.ansible_facts.hw_eth0.ipaddresses[0] }}"
group: just_created
ip: "{{ newvm.ansible_facts.hw_eth0.ipaddresses[0] }}"
changed_when: false
- name: Bootstrap new VM
hosts: just_created
become: true
pre_tasks:
- name: Set hostname
hostname:
name: "{{ name }}"
- name: Set hostname in /etc/hosts
lineinfile:
dest: /etc/hosts
regexp: "^{{ ansible_default_ipv4.address }}.+$"
line: "{{ ansible_default_ipv4.address }} {{ name }}"
- name: Poke Tower for post-install config
shell: test -x /opt/scripts/req_conf.sh && /opt/scripts/req_conf.sh &
changed_when: false