commit bd558730ee9e92de44d095e7979a84f9e986cd4c parent d3235333b078b444ac015c6613f92079485a204d Author: pyratebeard <root@pyratebeard.net> Date: Thu, 2 May 2024 17:22:37 +0100 vm deploy and remove use terraform to deploy a vm then create cluster resource with shell commands Diffstat:
A | homelab/playbook.yml | | | 179 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | homelab/vars.yml | | | 8 | ++++++++ |
2 files changed, 187 insertions(+), 0 deletions(-)
diff --git a/homelab/playbook.yml b/homelab/playbook.yml @@ -0,0 +1,179 @@ +--- +# ██ ████ +# ░██ ░██░ +# ██████ █████ ██████ ██████ ██████ ██████ ██████ ██████ ██████████ +# ░░░██░ ██░░░██░░██░░█░░██░░█ ░░░░░░██ ░░░██░ ██░░░░██░░██░░█░░██░░██░░██ +# ░██ ░███████ ░██ ░ ░██ ░ ███████ ░██ ░██ ░██ ░██ ░ ░██ ░██ ░██ +# ░██ ░██░░░░ ░██ ░██ ██░░░░██ ░██ ░██ ░██ ░██ ░██ ░██ ░██ +# ░░██ ░░██████░███ ░███ ░░████████ ░██ ░░██████ ░███ ███ ░██ ░██ +# ░░ ░░░░░░ ░░░ ░░░ ░░░░░░░░ ░░ ░░░░░░ ░░░ ░░░ ░░ ░░ +# +# author ▓▒ pyratebeard <root@pyratebeard.net> +# code ▓▒ https://git.pyratebeard.net/playbooks/ + +- hosts: pigley + gather_facts: true + become: true + pre_tasks: + - name: "load vars" + ansible.builtin.include_vars: + file: vars.yml + tags: always + + tasks: + - name: "check for terraform" + ansible.builtin.command: which terraform + register: command + check_mode: false + failed_when: false + tags: always + +# ▓▒ start of block ▒▓ + - name: "prepare" + block: + + - name: "prepare | install pre-reqs" + ansible.builtin.apt: + pkg: + - gnupg + - software-properties-common + - genisoimage + update_cache: true + + - name: "prepare | download hashicorp gpg key" + ansible.builtin.shell: wget -O- https://apt.releases.hashicorp.com/gpg | gpg --dearmor | tee /usr/share/keyrings/hashicorp-archive-keyring.gpg >/dev/null + + - name: "prepare | add hashicorp repo" + ansible.builtin.apt_repository: + repo: "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com {{ ansible_distribution_release }} main" + state: present + + - name: "prepare | install terraform" + ansible.builtin.apt: + name: terraform + update_cache: true + + - name: "prepare | disable security for libvirt" + ansible.builtin.lineinfile: + path: "/etc/libvirt/qemu.conf" + line: 'security_device = "none"' + insertafter: '#security_device = "selinux"' + notify: restart libvirtd + + when: command.rc != 0 +# ▓▒ end of block ▒▓ + + - name: "upload project_files" + ansible.builtin.copy: + src: "{{ terraform_project }}" + dest: "{{ project_files }}" + check_mode: false + tags: upload + + - name: "creation" + block: + - name: "creation | create vm" + community.general.terraform: + project_path: '{{ project_files }}' + state: present + complex_vars: true + variables: + vm_name: "{{ vm_name }}" + vm_vcpus: "{{ vm_vcpus }}" + vm_mem: "{{ vm_mem }}" + force_init: true + + - name: "creation | shutdown vm & dumpxml" + ansible.builtin.shell: | + virsh shutdown {{ vm_name }} && \ + virsh dumpxml {{ vm_name }} > /labfs/{{ vm_name }}.xml + + - name: "creation | create cluster resource" + ansible.builtin.shell: | + pcs resource create {{ vm_name }} VirtualDomain \ + config=/labfs/{{ vm_name }}.xml \ + migration_transport=ssh \ + meta \ + allow-migrate=true + + rescue: + - name: "creation rescue | shutdown vm" + ansible.builtin.shell: | + virsh list --all | grep {{ vm_name }} | grep "shut off" >/dev/null || \ + virsh destroy {{ vm_name }} + + - name: "creation rescue | undefine vm" + ansible.builtin.shell: | + virsh undefine {{ vm_name }} + + - name: "creation rescue | remove vm files" + ansible.builtin.file: + path: "/labfs/{{ vm_name }}.{{ item }}" + state: absent + with_items: + - qcow2 + - xml + tags: + - create + + - name: "removal" + block: + - name: "removal | stop cluster resource" + ansible.builtin.shell: | + pcs resource disable {{ vm_name }} --wait + + - name: "removal | remove cluster resource" + ansible.builtin.shell: | + pcs resource delete {{ vm_name }} + + - name: "removal | undefine domain" + ansible.builtin.shell: | + virsh undefine {{ vm_name }} + + - name: "removal | remove vm files" + ansible.builtin.file: + path: "/labfs/{{ vm_name }}.{{ item }}" + state: absent + with_items: + - qcow2 + - xml + tags: + - never + - remove + - destroy + + - name: "cleanup" + block: + - name: "cleanup | remove project files" + ansible.builtin.file: + path: "{{ project_files }}" + state: absent + check_mode: false + + - name: "cleanup | remove cloud-init iso" + ansible.builtin.file: + path: "/labfs/commoninit.iso" + state: absent + check_mode: false + + - name: "cleanup | find tfplan files" + ansible.builtin.find: + paths: "/tmp/" + patterns: "tmp.*tfplan$" + use_regex: true + register: tfplan_files + check_mode: false + + - name: "cleanup | tidy up tfplan files" + ansible.builtin.file: + path: "{{ item }}" + state: absent + loop: "{{ tfplan_files.files|map(attribute='path')|list }}" + check_mode: false + tags: always + + handlers: + - name: "restart libvirtd" + ansible.builtin.service: + name: libvirtd + state: restarted diff --git a/homelab/vars.yml b/homelab/vars.yml @@ -0,0 +1,8 @@ +## modify for vm creation +vm_os: "debian12" # shortname as used in terraform dir +vm_name: "{{ vm_os }}" +vm_vcpus: "2" +vm_mem: "2048" + +terraform_project: "~src/infra_code/libvirt/{{ vm_os }}/" +project_files: "/tmp/terraform_project_files"