Initialer Import der Synology Scripts

This commit is contained in:
root
2026-08-05 08:43:57 +02:00
commit 5e32a7c411
404 changed files with 79932 additions and 0 deletions
+177
View File
@@ -0,0 +1,177 @@
# -----------------------------
# Reset facts for this VM
# -----------------------------
- name: Reset facts
set_fact:
scsi_disks: {}
net_config: {}
ide_config: {}
efidisk0_config: {}
tpmstate0_config: {}
# -----------------------------
# Check if VM exists
# -----------------------------
- name: Check if VM exists
uri:
url: "https://{{ hostvars[node_map[item.Node]].ansible_host }}:8006/api2/json/nodes/{{ item.Node | lower }}/qemu/{{ item['VM ID'] }}/config"
method: GET
validate_certs: false
headers:
Authorization: "PVEAPIToken=root@pam!ansible={{ api_token_secret }}"
register: vm_config
failed_when: false # 401/404 stoppen das Playbook nicht
ignore_errors: yes
- name: Set VM exists fact
set_fact:
vm_exists: "{{ vm_config.status == 200 }}"
# -----------------------------
# 1. Build SCSI disks map
# -----------------------------
- name: Build scsi disks map
set_fact:
scsi_disks: >-
{{
scsi_disks | default({}) |
combine({
('scsi' ~ disk.Device): {
"storage": item['Disk Storage'].split()[0],
"size": disk.Size | int
}
})
}}
loop: "{{ item.Disks }}"
loop_control:
loop_var: disk
# -----------------------------
# 2. Build network config
# -----------------------------
- name: Build network config
set_fact:
net_config:
net0: "model={{ item.Model | lower | regex_replace(' .*','') }},bridge={{ item.Bridge }}"
# -----------------------------
# 3. Build IDE config
# -----------------------------
- name: Build IDE config
set_fact:
ide_config: >-
{{
( {} |
combine(
{ 'ide2': ((item['ISO Storage'] | default('ISO') | string).split()[0] ~ ":iso/" ~ item['ISO File'] ~ ",media=cdrom") }
) |
combine(
item['Enable VirtIO'] | ternary(
{ 'ide0': ((item['VirtIO Storage'] | default('ISO') | string).split()[0] ~ ":iso/" ~ item['VirtIO ISO'] ~ ",media=cdrom") },
{}
)
)
)
}}
# -----------------------------
# 4. Build EFI disk config
# -----------------------------
- name: Build EFI disk config
set_fact:
efidisk0_config: >-
{%
set efidisk = item['EFI STORAGE'] | ternary({
"storage": ((item['EFI STORAGE'] | string).split()[0]),
"efitype": "4m",
"format": "qcow2",
"pre_enrolled_keys": false
}, {}) %}
{{ efidisk }}
# -----------------------------
# 5. Build TPM config
# -----------------------------
- name: Build TPM config for Proxmox
set_fact:
tpmstate0_config: >-
{{
item['Add TPM'] | ternary({
"storage": (item['TPM Storage'] | string).split()[0],
"version": (item['TPM Version'] | regex_replace('^v','')) | default('2.0'),
}, {})
}}
# -----------------------------
# 6. Show VM variables before creating VM
# -----------------------------
- name: Show VM variables before creating VM
debug:
msg:
- "VM ID: {{ item['VM ID'] }}"
- "Node: {{ item.Node }}"
- "TPM Add: {{ item['Add TPM'] }}"
- "TPM Config: {{ tpmstate0_config }}"
- "SCSI Disks: {{ scsi_disks }}"
- "Network Config: {{ net_config }}"
- "IDE Config: {{ ide_config }}"
- "EFI Disk Config: {{ efidisk0_config }}"
- "Maschine Config: {{ item.Maschine }}"
- "BIOS Config: {{ item.BIOS }}"
- "VM exists: {{ vm_exists }}"
# -----------------------------
# 7. Create VM if it does not exist
# -----------------------------
- name: Create VM
community.proxmox.proxmox_kvm:
api_host: "{{ hostvars[node_map[item.Node]].ansible_host }}"
api_user: "root@pam"
api_token_id: "ansible"
api_token_secret: "{{ api_token_secret }}"
validate_certs: false
node: "{{ item.Node | lower }}"
vmid: "{{ item['VM ID'] }}"
name: "{{ item.Name | lower | regex_replace('_','-') }}"
memory: "{{ item.Memory }}"
cores: "{{ item.Cores }}"
sockets: "{{ item.Sockets }}"
machine: "{{ 'q35' if 'q35' in (item.Maschine | lower) else 'pc' }}"
bios: "{{ 'ovmf' if 'ovmf' in item.BIOS | lower else 'seabios' }}"
ostype: "{{ 'win11' if item['OS Typ'] == 'Microsoft' else 'l26' }}"
agent: "{{ 1 if item['Qemu Agent'] else 0 }}"
balloon: "{{ item.Ballooning }}"
onboot: "{{ item['Start at boot'] }}"
net: "{{ net_config }}"
ide: "{{ ide_config }}"
efidisk0: "{{ efidisk0_config if efidisk0_config != {} else omit }}"
tpmstate0: "{{ tpmstate0_config if tpmstate0_config != {} else omit }}"
tags: "{{ item.Service | lower }}"
state: present
timeout: 600
when: not vm_exists
# -----------------------------
# 8. Create SCSI disks if not exist
# -----------------------------
- name: Create SCSI disks for VM
community.proxmox.proxmox_disk:
api_host: "{{ hostvars[node_map[item.Node]].ansible_host }}"
api_user: "root@pam"
api_token_id: "ansible"
api_token_secret: "{{ api_token_secret }}"
validate_certs: false
vmid: "{{ item['VM ID'] }}"
disk: "{{ disk.key }}"
storage: "{{ disk.value.storage }}"
size: "{{ disk.value.size }}"
state: present
iothread: 1
loop: "{{ scsi_disks | dict2items }}"
loop_control:
loop_var: disk
when: vm_exists and (disk.key not in vm_config.json.data.keys())