From 00623c7f0cf0528fb8040ee15a540f40b758ce50 Mon Sep 17 00:00:00 2001 From: Martin Wohlert Date: Wed, 9 Jun 2021 21:07:20 +0200 Subject: [PATCH] =?UTF-8?q?besseres=20templating=20f=C3=BCr=20VMs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group_vars/docker_nodes.yml | 7 +------ roles/virt/tasks/main.yml | 18 +++++++++++------- roles/virt_vm/tasks/create_vm.yml | 2 +- roles/virt_vm/tasks/main.yml | 6 ++++++ vm_templates.yml | 10 ++++++++++ 5 files changed, 29 insertions(+), 14 deletions(-) create mode 100644 vm_templates.yml diff --git a/group_vars/docker_nodes.yml b/group_vars/docker_nodes.yml index aa53058..3d7aed3 100644 --- a/group_vars/docker_nodes.yml +++ b/group_vars/docker_nodes.yml @@ -1,6 +1 @@ -virt: - host: localhost - image: fedora-34-x64.qcow2 - cpus: 2 - ram: 2048 - hdd: 10g +vm_template: fedora_34 diff --git a/roles/virt/tasks/main.yml b/roles/virt/tasks/main.yml index 02f33d2..d33af63 100644 --- a/roles/virt/tasks/main.yml +++ b/roles/virt/tasks/main.yml @@ -1,16 +1,20 @@ --- +- include_vars: vm_templates.yml + +- name: test + debug: + msg: "{{ vm_templates[item] }}" + loop: "{{ vm_templates|flatten(1) }}" + - name: fetch images get_url: - dest: "{{ libvirt.images }}/{{ item.dest }}" + dest: "{{ libvirt.images }}/{{ vm_templates[item].image.filename }}" group: root owner: root mode: u=rw,g=r,o=r - url: "{{ item.url }}" - checksum: "{{ item.checksum }}" - with_items: - - dest: fedora-34-x64.qcow2 - checksum: sha1:fc08ad81a893b8253ec3bf6e25273cbb4fc8eafb - url: https://download.fedoraproject.org/pub/fedora/linux/releases/34/Cloud/x86_64/images/Fedora-Cloud-Base-34-1.2.x86_64.qcow2 + url: "{{ vm_templates[item].image.url }}" + checksum: "{{ vm_templates[item].image.checksum }}" + loop: "{{ vm_templates|flatten(1) }}" - name: virt_net "docker" present virt_net: diff --git a/roles/virt_vm/tasks/create_vm.yml b/roles/virt_vm/tasks/create_vm.yml index f8f7723..7d114a0 100644 --- a/roles/virt_vm/tasks/create_vm.yml +++ b/roles/virt_vm/tasks/create_vm.yml @@ -4,7 +4,7 @@ - name: create vm storage delegate_to: "{{ virt.host }}" copy: - src: "{{ libvirt.images }}/{{ virt.image }}" + src: "{{ libvirt.images }}/{{ virt.image.filename }}" dest: "{{ libvirt.images }}/{{ inventory_hostname }}.qcow2" force: no diff --git a/roles/virt_vm/tasks/main.yml b/roles/virt_vm/tasks/main.yml index e4ba894..c27465d 100644 --- a/roles/virt_vm/tasks/main.yml +++ b/roles/virt_vm/tasks/main.yml @@ -1,4 +1,6 @@ --- +- include_vars: vm_templates.yml + - name: check vm existance delegate_to: "{{ virt.host }}" stat: @@ -6,8 +8,12 @@ get_checksum: false get_attributes: false get_mime: false + vars: + virt: "{{ vm_templates[vm_template] }}" register: vmimg - name: create vm include_tasks: create_vm.yml + vars: + virt: "{{ vm_templates[vm_template] }}" when: not vmimg.stat.exists diff --git a/vm_templates.yml b/vm_templates.yml new file mode 100644 index 0000000..edd42b5 --- /dev/null +++ b/vm_templates.yml @@ -0,0 +1,10 @@ +vm_templates: + fedora_34: + host: localhost + cpus: 1 + ram: 1024 + hdd: 5g + image: + checksum: sha1:fc08ad81a893b8253ec3bf6e25273cbb4fc8eafb + filename: fedora-34.qcow2 + url: https://download.fedoraproject.org/pub/fedora/linux/releases/34/Cloud/x86_64/images/Fedora-Cloud-Base-34-1.2.x86_64.qcow2