blob: da6712dfa3b4928beb3889e64d82fb1a1e970f69 (
plain)
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
|
- name: clone proxmox template
proxmox_kvm:
node: '{{ proxmox_node }}'
api_host: localhost
api_user: '{{ proxmox_api_user }}@pam'
api_password: '{{ proxmox_api_password }}'
name: '{{ proxmox_hostname }}'
storage: '{{ proxmox_storage }}'
clone: '{{ proxmox_template }}'
full: yes
format: unspecified
state: present
delegate_to: '{{ proxmox_api_host }}'
register: vm_clone
- name: wait for PVE to settle
pause:
seconds: 5
when: vm_clone.changed
- name: get new vmid
shell: >-
qm list | awk -v name={{ proxmox_hostname }} '$2 == name { print $1; rc=1 } END { exit !rc }'
changed_when: False
register: proxmox_vmid
until: proxmox_vmid.stdout | int > 0
retries: 5
delay: 2
delegate_to: '{{ proxmox_api_host }}'
- name: update VM properties
proxmox_kvm:
node: '{{ proxmox_node }}'
api_host: localhost
api_user: '{{ proxmox_api_user }}@pam'
api_password: '{{ proxmox_api_password }}'
bios: '{{ proxmox_bios }}'
efidisk0:
storage: '{{ proxmox_storage }}'
efitype: 4m
pre_enrolled_keys: 1
format: unspecified
name: '{{ proxmox_hostname }}'
agent: '{{ proxmox_guest_agent }}'
storage: '{{ proxmox_storage }}'
onboot: '{{ proxmox_onboot }}'
cpu: '{{ proxmox_cpu }}'
sockets: '{{ proxmox_sockets }}'
cores: '{{ proxmox_cores }}'
vcpus: '{{ proxmox_sockets * proxmox_cores }}'
memory: '{{ proxmox_memory }}'
vga: '{{ proxmox_vga }}'
scsihw: '{{ proxmox_scsihw }}'
nameservers: "{{ proxmox_nameservers | join(',') }}"
searchdomains: '{{ proxmox_searchdomain }}'
sshkeys: "{{ proxmox_pubkeys | join('\n') }}"
ciuser: '{{ proxmox_username }}'
cipassword: '{{ proxmox_password }}'
cicustom: 'user={{ proxmox_userdata }}'
ipconfig:
ipconfig0: 'ip={{ (proxmox_ip ~ "/" ~ proxmox_netmask) | ansible.utils.ipaddr("cidr") }},gw={{ proxmox_gateway }}'
update: yes
delegate_to: '{{ proxmox_api_host }}'
# The proxox_kvm module is not smart enough to report when the VM remains unchanged - sad!
changed_when: false
- name: query the virtual NIC configuration
shell: >
qm config {{ proxmox_vmid.stdout }}
| awk '$1 == "net0:" {print $2}'
| sed -e 's/=/: /g' -e 's/,/\n/g'
register: qm_config_net0
changed_when: False
delegate_to: '{{ proxmox_api_host }}'
- name: convert NIC to YAML dictionary
set_fact:
vm_nic: '{{ qm_config_net0.stdout | from_yaml }}'
- name: set the virtual NIC vlan tag
command: >
qm set {{ proxmox_vmid.stdout }}
-net0 virtio={{ vm_nic.virtio }},bridge={{ vm_nic.bridge }},firewall={{ proxmox_firewall | int }},tag={{ proxmox_vlan }}
when: (proxmox_vlan | int) != (vm_nic.tag | default(1) | int)
delegate_to: '{{ proxmox_api_host }}'
- name: query the virtual disk configuration
shell: >
qm config {{ proxmox_vmid.stdout }}
| awk '$1 == "scsi0:" {print $2}'
| sed -e 's/[=:]/: /g' -e 's/,/\n/g'
register: qm_config_scsi0
changed_when: False
delegate_to: '{{ proxmox_api_host }}'
- name: convert disk to YAML dictionary
set_fact:
vm_disk: '{{ qm_config_scsi0.stdout | trim | from_yaml }}'
- name: grow the virtual disk
command: qm resize {{ proxmox_vmid.stdout }} scsi0 {{ proxmox_disk ~ 'G' }}
when: (proxmox_disk ~ 'G') != vm_disk.size
delegate_to: '{{ proxmox_api_host }}'
- name: set virtual disk properties
command: >
qm set {{ proxmox_vmid.stdout }}
-scsi0 {{ proxmox_storage }}:{{ vm_disk[proxmox_storage] }},discard={{ 'on' if proxmox_discard else 'off' }},ssd={{ proxmox_ssd | int }}
when: >-
vm_disk.discard is not defined
or vm_disk.discard != proxmox_discard
or vm_disk.ssd is not defined
or (vm_disk.ssd | int | bool) != proxmox_ssd
delegate_to: '{{ proxmox_api_host }}'
- name: configure efidisk
block:
- name: query the efi disk configuration
shell: "qm config {{ proxmox_vmid.stdout }} | grep -q '^efidisk0:'"
register: qm_config_efidisk0
changed_when: no
failed_when: no
delegate_to: '{{ proxmox_api_host }}'
- name: create efidisk
command: qm set {{ proxmox_vmid.stdout }} -efidisk0 {{ proxmox_storage }}:1,efitype=4m,pre-enrolled-keys=1
delegate_to: '{{ proxmox_api_host }}'
when: qm_config_efidisk0.rc != 0
when: "proxmox_bios == 'ovmf'"
- name: start the VM
proxmox_kvm:
node: '{{ proxmox_node }}'
api_host: localhost
api_user: '{{ proxmox_api_user }}@pam'
api_password: '{{ proxmox_api_password }}'
vmid: '{{ proxmox_vmid.stdout }}'
state: started
delegate_to: '{{ proxmox_api_host }}'
- name: wait for VM to become reachable
wait_for_connection:
timeout: 120
|