-
Notifications
You must be signed in to change notification settings - Fork 1
/
pve_manager_template_upgrade.yml
91 lines (77 loc) · 2.28 KB
/
pve_manager_template_upgrade.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
---
- name: Provision VM
hosts: spsrv
gather_facts: false
vars:
resource_type: vm
vmid: 5777
hostname: templatevm
net_ip: 192.168.13.77
storage_path: /mnt/tank0/ds0/proxmox
templates_path: "{{ storage_path }}/template/qcow2"
vm_image: "{{ templates_path }}/CentOS-7-x86_64-GenericCloud-2111.qcow2"
updated_image_name: "{{ templates_path }}/CentOS-7-x86_64-GenericCloud-updated.qcow2"
roles:
- pve_manager
tasks:
- name: Set facts
ansible.builtin.set_fact:
vmid: "{{ vmid }}"
storage_path: "{{ storage_path }}"
updated_image_name: "{{ updated_image_name }}"
- name: Configure VM
hosts: templatevm
gather_facts: false
handlers:
- name: run script
ansible.builtin.command: >
/tmp/sys_prep_cloud.sh
async: 1
poll: 0
become: true
changed_when: true
tasks:
- name: Wait for yum process to exit (cloud-init updates)
ansible.builtin.wait_for:
path: /var/run/yum.pid
state: absent
timeout: 600
- name: Copy script
ansible.builtin.copy:
src: files/sys_prep_cloud.sh
dest: /tmp
mode: "0755"
notify: run script
- name: Create template
hosts: spsrv
gather_facts: false
tasks:
- name: Check that VM is stopped
ansible.builtin.command: >
qm status {{ vmid }}
register: vm_status
retries: 5
delay: 5
until: "'stopped' in vm_status.stdout"
changed_when: false
- name: Convert qcow2
when: "'stopped' in vm_status.stdout"
block:
- name: Convert disk to qcow2 (full-size)
ansible.builtin.command: >
qm move_disk {{ vmid }} scsi0 local-spsrv-proxmox --format qcow2 --delete
changed_when: true
- name: Delete old template
ansible.builtin.file:
path: "{{ updated_image_name }}"
state: absent
- name: Convert qcow2 to template (shrinked)
ansible.builtin.command: >
qemu-img convert -O qcow2 -c
{{ storage_path }}/images/{{ vmid }}/vm-{{ vmid }}-disk-0.qcow2
{{ updated_image_name }}
changed_when: true
- name: Destroy VM
ansible.builtin.command: >
qm destroy {{ vmid }}
changed_when: true