commit 9e4f2a6de504829b7385c74bd095c9d1c9ded9c3 Author: David Thurstenson Date: Wed Nov 9 23:35:37 2022 -0600 Initial commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..e80a656 --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# Ansible Arch Linux Libvirt Lab + +This playbook is intended to help automate the deployment of Arch Linux guests to libvirt on localhost. + +This exists mainly as an excercise while I teach myself ansible, and is mostly a reimplementation of this article: gitea@git.thurstylark.com:thurstylark/ansible-arch-libvirt-lab.git + +As always, patches welcome! diff --git a/kvm_provision.yml b/kvm_provision.yml new file mode 100644 index 0000000..c63732a --- /dev/null +++ b/kvm_provision.yml @@ -0,0 +1,17 @@ +--- + +- name: Deploys VM based on cloud image + hosts: localhost + gather_facts: yes + become: yes + vars: + vm: arch-lab01 + cleanup: yes + + tasks: + - name: KVM Provision role + include_role: + name: kvm_provision + vars: + vm_name: "{{ vm }}" + cleanup_tmp: "{{ cleanup }}" diff --git a/roles/kvm_provision/defaults/main.yml b/roles/kvm_provision/defaults/main.yml new file mode 100644 index 0000000..5594d19 --- /dev/null +++ b/roles/kvm_provision/defaults/main.yml @@ -0,0 +1,11 @@ +--- + +base_image_name: Arch-Linux-x86_64-basic.qcow2 +base_image_url: https://geo.mirror.pkgbuild.com/images/latest/{{ base_image_name }} +libvirt_pool_dir: "/var/lib/libvirt/images" +vm_name: arch-lab +vm_vcpus: 2 +vm_ram_mb: 2048 +vm_net: default +vm_root_pass: letmein +cleanup_tmp: no diff --git a/roles/kvm_provision/tasks/main.yml b/roles/kvm_provision/tasks/main.yml new file mode 100644 index 0000000..86a67c5 --- /dev/null +++ b/roles/kvm_provision/tasks/main.yml @@ -0,0 +1,63 @@ +--- +# tasks file for kvm_provision + +- name: Ensure prereqs are installed + package: + name: + - guestfs-tools + - libvirt-python + state: present + become: yes + +- name: Get VMs list + virt: + command: list_vms + register: existing_vms + changed_when: no + +- name: Create VM if not exists + block: + + - name: Download base image + get_url: + url: "{{ base_image_url }}" + dest: "/tmp/{{ base_image_name }}" + + - name: Copy base image to libvirt storage pool + copy: + src: "/tmp/{{ base_image_name }}" + dest: "{{ libvirt_pool_dir }}/{{ vm_name }}.qcow2" + force: no + remote_src: yes + mode: 0660 + become: yes + register: copy_results + + - name: Customize image + command: | + virt-customize -a {{ libvirt_pool_dir }}/{{ vm_name }}.qcow2 \ + --hostname {{ vm_name }} \ + --root-password password:{{ vm_root_pass }} + when: copy_results is changed + + - name: Define VM + virt: + command: define + xml: "{{ lookup('template', 'vm-template.xml.j2') }}" + + when: "vm_name not in existing_vms.list_vms" + +- name: Start new VM + virt: + name: "{{ vm_name }}" + state: running + register: vm_start_results + until: "vm_start_results is success" + retries: 15 + delay: 2 + +- name: Clean up temporary files + file: + path: "/tmp/{{ base_image_name }}" + state: absent + when: cleanup_tmp | bool diff --git a/roles/kvm_provision/templates/vm-template.xml.j2 b/roles/kvm_provision/templates/vm-template.xml.j2 new file mode 100644 index 0000000..b80da86 --- /dev/null +++ b/roles/kvm_provision/templates/vm-template.xml.j2 @@ -0,0 +1,136 @@ + + {{ vm_name }} + {{ vm_ram_mb }} + {{ vm_vcpus }} + + hvm + + + + + + + + + + + + + + destroy + restart + destroy + + + + + + /usr/bin/qemu-system-x86_64 + + + + +
+ + +
+ + +
+ + + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + +
+ + +
+ + + + +
+ + + + + + + + + + + +
+ + + +
+ + +
+ + + + + + + + +
+ +