From 3b87e13c3a1f6b763becef12e4e1e75395ed59a8 Mon Sep 17 00:00:00 2001 From: "benedikt.wismans@sdw.systems" Date: Mon, 21 Nov 2022 18:41:21 +0100 Subject: [PATCH] Install VM mit inject pubkey --- bootstrapTest.yml | 2 +- host_vars/bbb.xitq.de | 7 ++- roles/kvm_installvm/tasks/main.yml | 56 ++++++++++++++----- .../kvm_installvm/templates/template2204.xml | 4 +- 4 files changed, 52 insertions(+), 17 deletions(-) diff --git a/bootstrapTest.yml b/bootstrapTest.yml index 671d526..8c38ef4 100644 --- a/bootstrapTest.yml +++ b/bootstrapTest.yml @@ -3,7 +3,7 @@ remote_user: core become: true roles: - - kvm_setup + # - kvm_setup - kvm_installvm diff --git a/host_vars/bbb.xitq.de b/host_vars/bbb.xitq.de index 4924ddc..5fdbd2a 100644 --- a/host_vars/bbb.xitq.de +++ b/host_vars/bbb.xitq.de @@ -21,7 +21,12 @@ libvirt_pool_dir: "/var/lib/libvirt/images" vm_base_image: template2204.qcow2 vm_base_image_archiv: "{{ vm_base_image + '.gz' }}" -vm_name: myVMname +# bobby +password: $6$A7Sw5gku7$0WqY/Z4Ei0axG.mZwDQlFJfK8Rtx6H0eKuP8CW23V5f/5j9sXendknLtXexQfgDzA4Ri55ZqhJnpf05MJV71J0 + +ssh_key_root: /root/.ssh/id_rsa.pub +ssh_key_core: /home/core/.ssh/id_rsa.pub +vm_name: vhosttst vm_vcpus: 2 vm_ram_mb: 4096 vm_net: default diff --git a/roles/kvm_installvm/tasks/main.yml b/roles/kvm_installvm/tasks/main.yml index a9943d9..1aad7fd 100644 --- a/roles/kvm_installvm/tasks/main.yml +++ b/roles/kvm_installvm/tasks/main.yml @@ -9,27 +9,57 @@ - name: Create VM if not exists block: - - name: Upload base image archiv to core's tmp directory - copy: - src: "{{ vm_base_image_archiv }}" - dest: "/home/core/tmp/" - owner: libvirt-qemu - group: kvm - mode: "0600" +# - name: Upload base image archiv to core's tmp directory +# copy: +# src: "{{ vm_base_image_archiv }}" +# dest: "/home/core/tmp/" +# owner: libvirt-qemu +# group: kvm +# mode: "0600" - - name: Gunzip base image in core's tmp directory - shell: - cmd: "/usr/bin/gunzip /home/core/tmp/{{ vm_base_image_archiv }}" +# - name: Gunzip base image in core's tmp directory +# shell: +# cmd: "/usr/bin/gunzip /home/core/tmp/{{ vm_base_image_archiv }}" - - name: Copy unarchived base image to /var/lib/libvirt/images and rename to $vm_name + # Existierende VM-Disk NICHT überschreiben: force=no. Ergbnis in copy_results speichern: copy_results is changed + - name: Copy unarchived base image to /var/lib/libvirt/images and rename to $vm_name.qcow2 if not exists copy: src: "/home/core/tmp/{{ vm_base_image }}" - dest: "{{ libvirt_pool_dir }}/{{ vm_name }}" + dest: "{{ libvirt_pool_dir }}/{{ vm_name }}.qcow2" remote_src: "yes" owner: libvirt-qemu group: kvm mode: "0600" - + force: "no" + register: copy_results + + - name: Configure the image + command: | + virt-customize -a {{ libvirt_pool_dir }}/{{ vm_name }}.qcow2 \ + --hostname {{ vm_name }} \ + --root-password password:{{ password }} \ + --ssh-inject 'core:file:{{ ssh_key_core }}' + + when: copy_results is changed + + # Falls das V-File kopiert wurde - weil es das noch nicht gab - wird die VM defined + # Ist eigentlich unnötig, denn der ganze Block wird nicht ausgeführt wenn die VM bereits defined ist + - name: Define vm + community.libvirt.virt: + command: define + xml: "{{ lookup('template', 'template2204.xml') }}" + when: copy_results is changed + when: "vm_name not in existing_vms.list_vms" +- name: Ensure VM is started + community.libvirt.virt: + name: "{{ vm_name }}" + state: running + register: vm_start_results + until: "vm_start_results is success" + retries: 15 + delay: 2 + + ... \ No newline at end of file diff --git a/roles/kvm_installvm/templates/template2204.xml b/roles/kvm_installvm/templates/template2204.xml index cdbb3a7..7d026da 100644 --- a/roles/kvm_installvm/templates/template2204.xml +++ b/roles/kvm_installvm/templates/template2204.xml @@ -5,8 +5,8 @@ - {{ vm_ram_mb }} - {{ vm_ram_mb }} + {{ vm_ram_mb }} + {{ vm_ram_mb }} {{ vm_vcpus }} /machine