From ca16e3d55fe86ba054d8f114c8d4858ebf3a793a Mon Sep 17 00:00:00 2001
From: June <june@jsts.xyz>
Date: Sun, 23 Feb 2025 22:41:06 +0100
Subject: [PATCH] dep._hypervisor(playb.): introduce play for setting up vm
 template gen.

---
 .../chaosknoten/host_vars/chaosknoten.yaml    |  6 +++
 inventories/chaosknoten/hosts.yaml            |  3 ++
 playbooks/deploy_hypervisor.yaml              | 54 +++++++++++++++++++
 3 files changed, 63 insertions(+)
 create mode 100644 inventories/chaosknoten/host_vars/chaosknoten.yaml
 create mode 100644 playbooks/deploy_hypervisor.yaml

diff --git a/inventories/chaosknoten/host_vars/chaosknoten.yaml b/inventories/chaosknoten/host_vars/chaosknoten.yaml
new file mode 100644
index 0000000..1c8fa93
--- /dev/null
+++ b/inventories/chaosknoten/host_vars/chaosknoten.yaml
@@ -0,0 +1,6 @@
+# Used in deploy_hypervisor playbook.
+hypervisor__template_vm_config:
+  - name: STORAGE
+    value: nvme0
+  - name: BRIDGE
+    value: vmbr4
diff --git a/inventories/chaosknoten/hosts.yaml b/inventories/chaosknoten/hosts.yaml
index cae283d..1f5b31b 100644
--- a/inventories/chaosknoten/hosts.yaml
+++ b/inventories/chaosknoten/hosts.yaml
@@ -171,3 +171,6 @@ netbox_hosts:
   hosts:
     eh22-netbox:
     netbox:
+proxmox_vm_template_hosts:
+  hosts:
+    chaosknoten:
diff --git a/playbooks/deploy_hypervisor.yaml b/playbooks/deploy_hypervisor.yaml
new file mode 100644
index 0000000..0739d35
--- /dev/null
+++ b/playbooks/deploy_hypervisor.yaml
@@ -0,0 +1,54 @@
+- name: Ensure the VM template generation is set up
+  hosts: proxmox_vm_template_hosts
+  tasks:
+    - name: Ensure /usr/local/{lib,sbin} exist
+      ansible.builtin.file:
+        path: "{{ item }}"
+        state: directory
+        owner: root
+        group: root
+        mode: "0755"
+      become: true
+      loop:
+        - "/usr/local/lib/"
+        - "/usr/local/sbin/"
+
+    - name: Ensure the pve-template-vm repo is present
+      ansible.builtin.git:
+        repo: https://git.hamburg.ccc.de/CCCHH/pve-template-vm.git
+        dest: /usr/local/lib/pve-template-vm
+        version: main
+        force: true
+        depth: 1
+        single_branch: true
+        track_submodules: true
+      become: true
+
+    # /usr/local/sbin as the script uses qm, which is also found in /usr/sbin.
+    - name: Ensure symlink to build-proxmox-template exists in /usr/local/sbin
+      ansible.builtin.file:
+        src: /usr/local/lib/pve-template-vm/build-proxmox-template
+        dest: /usr/local/sbin/build-proxmox-template
+        state: link
+        owner: root
+        group: root
+        mode: '0755'
+      become: true
+
+    # This sets up a cron job running /usr/local/sbin/build-proxmox-template using the env vars defined in hypervisor__template_vm_config.
+    - name: Ensure cron job is present for building a fresh VM template every week on Friday 04:00
+      ansible.builtin.cron:
+        name: "ansible build proxmox template"
+        cron_file: ansible_build_proxmox_template
+        minute: 0
+        hour: 4
+        weekday: 5
+        user: root
+        job: "{% if hypervisor__template_vm_config is defined and hypervisor__template_vm_config | length > 0 %}\
+              /usr/bin/env \
+              {% for item in hypervisor__template_vm_config | default([]) %}\
+              {{ item.name }}=\"{{ item.value }}\" \
+              {% endfor %}\
+              {% endif %}\
+              /usr/local/sbin/build-proxmox-template"
+      become: true