-
Notifications
You must be signed in to change notification settings - Fork 1
/
pve_manager_template_ct_build_local.yml
132 lines (106 loc) · 3.47 KB
/
pve_manager_template_ct_build_local.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
---
- name: Provision ct
hosts: spbleq12
gather_facts: false
vars:
template_storage_name: local
template_storage_path: /var/lib/vz/template/cache/
template_name: ubuntu-24.04-standard_24.04-2_amd64.tar.zst
template_name_custom: ubuntu-24.04-docker.tar.zst
resource_type: ct
ct_template: "{{ template_storage_name }}:vztmpl/{{ template_name }}"
vmid: 5551
hostname: ct-template
net_ip: 192.168.20.242
net_vlan: ""
net_gw: 192.168.20.1
net_dns: 192.168.20.1
pre_tasks:
- name: Download template
ansible.builtin.command: >
pveam download {{ template_storage_name }} {{ template_name }}
register: _template_download_result
changed_when: "'finished' and not 'no need to download' in _template_download_result.stdout"
roles:
- { role: pve_manager }
tasks:
- name: set_fact
ansible.builtin.set_fact:
vmid: "{{ vmid }}"
hostname: "{{ hostname }}"
template_storage_path: "{{ template_storage_path }}"
template_name_custom: "{{ template_name_custom }}"
- name: Pre-configure ct
hosts: ct-template
gather_facts: true
roles:
- { role: common/apt_sources }
tasks:
- name: Upgrade packages
ansible.builtin.package:
update_cache: true
state: latest
name: "*"
- name: Set timezone
community.general.timezone:
name: Europe/Moscow
- name: Set locale
ansible.builtin.shell: >
localectl set-locale LANG=C.UTF-8 && \
locale-gen en_US.UTF-8
changed_when: true
- name: Configure ct
hosts: ct-template
gather_facts: true
roles:
- { role: common/packages }
- { role: spirkaa.zsh_prezto }
- { role: spirkaa.nano }
- { role: common/motd }
- { role: common/sshd }
- { role: common/docker }
tasks:
- name: Flush handlers
ansible.builtin.meta: flush_handlers
- name: Cleanup packages # noqa: command-instead-of-module
ansible.builtin.shell: >
apt-get clean -y \
&& rm -rf /var/lib/apt/lists/*
changed_when: true
- name: reboot
ansible.builtin.reboot:
- name: Create template from ct
hosts: spbleq12
gather_facts: false
tasks:
- name: "{{ hostname }} - shutdown"
ansible.builtin.command: >
pct shutdown {{ vmid }}
changed_when: true
- name: "{{ hostname }} - delete net0"
ansible.builtin.command: >
pct set {{ vmid }} --delete net0
changed_when: true
- name: "{{ hostname }} - create backup"
ansible.builtin.command: >
vzdump {{ vmid }} --mode stop --compress zstd --zstd 0 --dumpdir {{ template_storage_path }}
changed_when: true
- name: "{{ hostname }} - destroy ct"
ansible.builtin.command: >
pct destroy {{ vmid }}
changed_when: true
- name: "{{ hostname }} - get backup files" # noqa: risky-shell-pipe
ansible.builtin.shell: >
ls {{ template_storage_path }} | grep {{ vmid }}
register: _backup_files
failed_when: _backup_files.rc == 2
changed_when: false
- name: "{{ hostname }} - rename backup archive"
ansible.builtin.command: >
mv {{ template_storage_path }}{{ _backup_files.stdout_lines[-1] }} {{ template_storage_path }}{{ template_name_custom }}
register: _backup
changed_when: true
- name: "{{ hostname }} - remove backup log"
ansible.builtin.file:
path: "{{ template_storage_path }}{{ _backup_files.stdout_lines[-2] }}"
state: absent