Install VM mit inject pubkey

This commit is contained in:
Benedikt Wismans 2022-11-21 18:41:21 +01:00
parent 73dbe30936
commit 3b87e13c3a
4 changed files with 52 additions and 17 deletions

View File

@ -3,7 +3,7 @@
remote_user: core
become: true
roles:
- kvm_setup
# - kvm_setup
- kvm_installvm

View File

@ -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

View File

@ -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
...

View File

@ -5,8 +5,8 @@
<libosinfo:os id="http://ubuntu.com/ubuntu/20.04"/>
</libosinfo:libosinfo>
</metadata>
<memory unit='KiB'>{{ vm_ram_mb }} </memory>
<currentMemory unit='KiB'>{{ vm_ram_mb }}</currentMemory>
<memory unit='MiB'>{{ vm_ram_mb }}</memory>
<currentMemory unit='MiB'>{{ vm_ram_mb }}</currentMemory>
<vcpu placement='static'> {{ vm_vcpus }}</vcpu>
<resource>
<partition>/machine</partition>