From d0dad3fbc44bd8df0989c31cc0c1fc581ba30079 Mon Sep 17 00:00:00 2001 From: Leander Hutton Date: Fri, 8 Dec 2023 19:01:12 -0500 Subject: [PATCH] Initial commit --- README.md | 50 +++++++++++++++++++++++++++++++ defaults/main.yml | 12 ++++++++ meta/main.yml | 52 ++++++++++++++++++++++++++++++++ tasks/main.yml | 57 ++++++++++++++++++++++++++++++++++++ templates/vm-template.xml.j2 | 52 ++++++++++++++++++++++++++++++++ tests/inventory | 2 ++ tests/test.yml | 5 ++++ 7 files changed, 230 insertions(+) create mode 100644 README.md create mode 100644 defaults/main.yml create mode 100644 meta/main.yml create mode 100644 tasks/main.yml create mode 100644 templates/vm-template.xml.j2 create mode 100644 tests/inventory create mode 100644 tests/test.yml diff --git a/README.md b/README.md new file mode 100644 index 0000000..397d143 --- /dev/null +++ b/README.md @@ -0,0 +1,50 @@ +kvm_provision Role +========= + +Deployes libvirt virtual machines from various template sources. + + +Requirements +------------ + +Community libvirt: + +``` +ansible-galaxy collection install community.libvirt +``` + +Example Playbook +---------------- + +``` +- name: Deploys VM based on template image + hosts: localhost + gather_facts: yes + become: yes + vars: + pool_dir: "/var/lib/libvirt/images" + vm: debian_12 + vcpus: 2 + ram_mb: 2048 + cleanup: no + vm_user: beegyoshi + vm_pass: stinky + net: bridged-network + + tasks: + - name: KVM Provision role + include_role: + name: kvm_provision + vars: + libvirt_pool_dir: "{{ pool_dir }}" + vm_name: "{{ vm }}" + vm_vcpus: "{{ vcpus }}" + vm_ram_mb: "{{ ram_mb }}" + vm_net: "{{ net }}" + cleanup_tmp: "{{ cleanup }}" + +``` +Author Information +------------------ + +leander@one-button.org diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..d92b1e1 --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,12 @@ +--- +# defaults file for kvm_provision +base_image_name: debian12-template.qcow2 +base_image_dir: "/var/lib/libvirt/images" +libvirt_pool_dir: "/var/lib/libvirt/images" +vm_name: debain_12_dev +vm_vcpus: 2 +vm_ram_mb: 2048 +vm_net: bridged-network +vm_user: bigppman +vm_pass: stoopid +cleanup_tmp: no diff --git a/meta/main.yml b/meta/main.yml new file mode 100644 index 0000000..c572acc --- /dev/null +++ b/meta/main.yml @@ -0,0 +1,52 @@ +galaxy_info: + author: your name + description: your role description + company: your company (optional) + + # If the issue tracker for your role is not on github, uncomment the + # next line and provide a value + # issue_tracker_url: http://example.com/issue/tracker + + # Choose a valid license ID from https://spdx.org - some suggested licenses: + # - BSD-3-Clause (default) + # - MIT + # - GPL-2.0-or-later + # - GPL-3.0-only + # - Apache-2.0 + # - CC-BY-4.0 + license: license (GPL-2.0-or-later, MIT, etc) + + min_ansible_version: 2.1 + + # If this a Container Enabled role, provide the minimum Ansible Container version. + # min_ansible_container_version: + + # + # Provide a list of supported platforms, and for each platform a list of versions. + # If you don't wish to enumerate all versions for a particular platform, use 'all'. + # To view available platforms and versions (or releases), visit: + # https://galaxy.ansible.com/api/v1/platforms/ + # + # platforms: + # - name: Fedora + # versions: + # - all + # - 25 + # - name: SomePlatform + # versions: + # - all + # - 1.0 + # - 7 + # - 99.99 + + galaxy_tags: [] + # List tags for your role here, one per line. A tag is a keyword that describes + # and categorizes the role. Users find roles by searching for tags. Be sure to + # remove the '[]' above, if you add tags to this list. + # + # NOTE: A tag is limited to a single word comprised of alphanumeric characters. + # Maximum 20 tags per role. + +dependencies: [] + # List your role dependencies here, one per line. Be sure to remove the '[]' above, + # if you add dependencies to this list. diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..7dbb717 --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,57 @@ +--- +# tasks file for kvm_provision + +- name: Ensure requirements in place + package: + name: + - guestfs-tools + - python3-libvirt + state: present + become: yes + +- name: Get VMs list + community.libvirt.virt: + command: list_vms + register: existing_vms + changed_when: no + +- name: Create VM if not exists + block: + + - name: Copy base image to libvirt directory + copy: + dest: "{{ libvirt_pool_dir }}/{{ vm_name }}.qcow2" + src: "{{ libvirt_pool_dir }}/{{ base_image_name }}" + force: no + remote_src: yes + mode: 0660 + register: copy_results + - name: Define vm + community.libvirt.virt: + command: define + xml: "{{ lookup('template', 'vm-template.xml.j2') }}" + - name: Configure the image + command: | + virt-customize -a {{ libvirt_pool_dir }}/{{ vm_name }}.qcow2 \ + --hostname {{ vm_name }} \ + --password {{ vm_user }}:password:{{ vm_pass }} \ + --firstboot-command '/usr/bin/ssh-keygen -A' + 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 + +- name: Ensure temporary file is deleted + file: + path: "/tmp/{{ base_image_name }}" + state: absent + when: cleanup_tmp | bool diff --git a/templates/vm-template.xml.j2 b/templates/vm-template.xml.j2 new file mode 100644 index 0000000..3d1309e --- /dev/null +++ b/templates/vm-template.xml.j2 @@ -0,0 +1,52 @@ + + {{ vm_name }} + {{ vm_ram_mb }} + {{ vm_vcpus }} + + hvm + + + + + /usr/bin/qemu-system-x86_64 + + + + +
+ + + + +
+ + + +
+ + + +
+ + +
+ + + + + + + +