Skip to content
Snippets Groups Projects
Verified Commit ce0711c2 authored by David Mehren's avatar David Mehren
Browse files

The big HOA refactor™ that enables updating existing VMs

parent 65f1a478
No related branches found
No related tags found
No related merge requests found
Pipeline #293441 failed
---
- name: Create temporary file
ansible.builtin.tempfile:
state: file
register: tempfile
check_mode: false
- name: Save SSH-keys to temporary file
ansible.builtin.copy:
content: "{{ proxmox_vm_sshkeys }}"
dest: "{{ tempfile.path }}"
mode: "0644"
- name: Generate basic 'qm set' command line
ansible.builtin.set_fact:
proxmox_create_vm_qm_args: "--sshkey {{ tempfile.path }} --ciuser {{ proxmox_vm_user }} --cores {{ proxmox_vm_cpu.cores }} --sockets {{ proxmox_vm_cpu.sockets }} --numa {{ proxmox_vm_cpu.numa }} --memory {{ proxmox_vm_memory }}" # noqa yaml[line-length]
- name: Generate ipconfig options
ansible.builtin.set_fact:
proxmox_create_vm_qm_args: "{{ proxmox_create_vm_qm_args }} --ipconfig{{ index }} {{ item }}"
loop: "{{ proxmox_vm_ipconfig_list }}"
loop_control:
index_var: index
- name: Generate cloud-init net options
ansible.builtin.set_fact:
proxmox_create_vm_qm_args: "{{ proxmox_create_vm_qm_args }} --net{{ index }} virtio,{{ item }}"
loop: "{{ proxmox_vm_net_list }}"
loop_control:
index_var: index
when: proxmox_vm_net is defined
- name: Generate startup order option
ansible.builtin.set_fact:
proxmox_create_vm_qm_args: "{{ proxmox_create_vm_qm_args }} --startup {{ proxmox_vm_startup }}"
when: proxmox_vm_startup is defined
- name: Generate cloud-init nameserver option
ansible.builtin.set_fact:
proxmox_create_vm_qm_args: "{{ proxmox_create_vm_qm_args }} --nameserver '{{ proxmox_vm_nameservers | join(' ') }}'"
when: proxmox_vm_nameservers is defined
- name: Generate cloud-init searchdomain option
ansible.builtin.set_fact:
proxmox_create_vm_qm_args: "{{ proxmox_create_vm_qm_args }} --searchdomain '{{ proxmox_vm_searchdomain }}'"
when: proxmox_vm_searchdomain is defined
- name: Generate ballooning options
ansible.builtin.set_fact:
proxmox_create_vm_qm_args: "{{ proxmox_create_vm_qm_args }} --balloon {{ proxmox_vm_min_memory }}"
when: proxmox_vm_min_memory is defined
- name: Generate hotplug options
ansible.builtin.set_fact:
proxmox_create_vm_qm_args: "{{ proxmox_create_vm_qm_args }} --hotplug {{ proxmox_vm_hotplug }}"
when: proxmox_vm_hotplug is defined
- name: Generate onboot options
ansible.builtin.set_fact:
proxmox_create_vm_qm_args: "{{ proxmox_create_vm_qm_args }} --onboot {{ proxmox_vm_onboot }}"
when: proxmox_vm_onboot is defined
- name: Get next free VM ID
ansible.builtin.command: pvesh get /cluster/nextid
register: vmid
changed_when: false
- name: Clone template # noqa no-changed-when
ansible.builtin.command: "qm clone {{ proxmox_vm_templateid }} {{ vmid.stdout }} --name {{ proxmox_vm_name }} --full true" # noqa yaml[line-length]
- name: Resize harddisk # noqa no-changed-when
ansible.builtin.command: "qm resize {{ vmid.stdout }} scsi0 {{ proxmox_vm_disksize }}"
- name: Set VM options # noqa no-changed-when
ansible.builtin.command: "qm set {{ vmid.stdout }} {{ proxmox_create_vm_qm_args }}"
- name: Start VM # noqa no-changed-when
ansible.builtin.command: "qm start {{ vmid.stdout }}"
- name: Enable HA for VM # noqa no-changed-when
ansible.builtin.command: "ha-manager add {{ vmid.stdout }}"
when: proxmox_vm_enable_ha is defined and proxmox_vm_enable_ha
--- ---
- name: Fail if required settings are not defined - name: Fail if required settings are not defined
ansible.builtin.fail: ansible.builtin.assert:
msg: >- that:
Required setting `{{ item }}` is not defined - proxmox_vm_templateid is defined
when: "vars.get(item, '') == ''" - proxmox_vm_name is defined
with_items: - proxmox_vm_sshkeys is defined
- proxmox_vm_templateid - proxmox_vm_ipconfig is defined
- proxmox_vm_name - proxmox_vm_user is defined
- proxmox_vm_sshkeys - proxmox_vm_disksize is defined
- proxmox_vm_ipconfig - proxmox_vm_cpu is defined
- proxmox_vm_user - proxmox_vm_cpu.cores is defined
- proxmox_vm_disksize - proxmox_vm_cpu.sockets is defined
- proxmox_vm_cpu - proxmox_vm_memory is defined
- proxmox_vm_memory fail_msg: "A required parameter is not defined"
quiet: true
- name: Fail if required CPU settings are not defined
ansible.builtin.fail: - name: Gather cluster resources
msg: >-
Required setting `proxmox_vm_cpu.{{ item }}` is not defined
when: "vars.get('proxmox_vm_cpu').get(item, '') == ''"
with_items:
- cores
- sockets
- name: Gather existing VMs
ansible.builtin.command: pvesh get /cluster/resources --output-format json ansible.builtin.command: pvesh get /cluster/resources --output-format json
register: proxmox_vm_resources register: proxmox_vm_resources_json
changed_when: false changed_when: false
when: proxmox_vm_existing is not defined or proxmox_vm_existing == [] check_mode: false # also run in check_mode
when: proxmox_vm_resources is not defined or proxmox_vm_existing == []
- name: Save existing VMs - name: Parse cluster resource JSON
ansible.builtin.set_fact: ansible.builtin.set_fact:
proxmox_vm_existing: "{{ proxmox_vm_resources.stdout | ansible.builtin.from_json | community.general.json_query('[?type==`qemu`].name') }}" # noqa yaml[line-length] proxmox_vm_resources: "{{ proxmox_vm_resources_json.stdout | ansible.builtin.from_json }}"
when: proxmox_vm_existing is not defined or proxmox_vm_existing == [] when: proxmox_vm_resources is not defined or proxmox_vm_existing == []
- name: Create new VM
when: proxmox_vm_name not in proxmox_vm_existing
block:
- name: Create temporary file
ansible.builtin.tempfile:
state: file
register: tempfile
- name: Save SSH-keys to temporary file - name: Extract VM facts
ansible.builtin.copy: ansible.builtin.set_fact:
content: "{{ proxmox_vm_sshkeys }}" proxmox_vm_existing: "{{ proxmox_vm_resources | community.general.json_query('[?type==`qemu`].name') }}" # noqa yaml[line-length]
dest: "{{ tempfile.path }}" proxmox_vm_node: "{{ proxmox_vm_resources | community.general.json_query(node_query) | first | default('') }}" # noqa yaml[line-length]
mode: "0644" proxmox_vm_id: "{{ proxmox_vm_resources | community.general.json_query(id_query) | first | default('') }}" # noqa yaml[line-length]
vars:
node_query: "[?name=='{{ proxmox_vm_name }}'].node"
id_query: "[?name=='{{ proxmox_vm_name }}'].vmid"
- name: Convert proxmox_vm_net to list - name: Convert proxmox_vm_net to list
ansible.builtin.set_fact: ansible.builtin.set_fact:
...@@ -55,74 +43,12 @@ ...@@ -55,74 +43,12 @@
- name: Convert proxmox_vm_ipconfig to list - name: Convert proxmox_vm_ipconfig to list
ansible.builtin.set_fact: ansible.builtin.set_fact:
proxmox_vm_ipconfig_list: "{{ proxmox_vm_ipconfig is string | ternary([proxmox_vm_ipconfig], proxmox_vm_ipconfig) }}" # noqa yaml[line-length] proxmox_vm_ipconfig_list: "{{ proxmox_vm_ipconfig is string | ternary([proxmox_vm_ipconfig], proxmox_vm_ipconfig) }}"
- name: Generate basic 'qm set' command line
ansible.builtin.set_fact:
proxmox_create_vm_qm_args: "--sshkey {{ tempfile.path }} --ciuser {{ proxmox_vm_user }} --cores {{ proxmox_vm_cpu.cores }} --sockets {{ proxmox_vm_cpu.sockets }} --numa {{ proxmox_vm_cpu.numa }} --memory {{ proxmox_vm_memory }}" # noqa yaml[line-length]
- name: Generate ipconfig options
ansible.builtin.set_fact:
proxmox_create_vm_qm_args: "{{ proxmox_create_vm_qm_args }} --ipconfig{{ index }} {{ item }}"
loop: "{{ proxmox_vm_ipconfig_list }}"
loop_control:
index_var: index
- name: Generate cloud-init net options
ansible.builtin.set_fact:
proxmox_create_vm_qm_args: "{{ proxmox_create_vm_qm_args }} --net{{ index }} virtio,{{ item }}"
loop: "{{ proxmox_vm_net_list }}"
loop_control:
index_var: index
when: proxmox_vm_net is defined
- name: Generate startup order option
ansible.builtin.set_fact:
proxmox_create_vm_qm_args: "{{ proxmox_create_vm_qm_args }} --startup {{ proxmox_vm_startup }}"
when: proxmox_vm_startup is defined
- name: Generate cloud-init nameserver option
ansible.builtin.set_fact:
proxmox_create_vm_qm_args: "{{ proxmox_create_vm_qm_args }} --nameserver '{{ proxmox_vm_nameservers | join(' ') }}'" # noqa yaml[line-length]
when: proxmox_vm_nameservers is defined
- name: Generate cloud-init searchdomain option
ansible.builtin.set_fact:
proxmox_create_vm_qm_args: "{{ proxmox_create_vm_qm_args }} --searchdomain '{{ proxmox_vm_searchdomain }}'"
when: proxmox_vm_searchdomain is defined
- name: Generate ballooning options
ansible.builtin.set_fact:
proxmox_create_vm_qm_args: "{{ proxmox_create_vm_qm_args }} --balloon {{ proxmox_vm_min_memory }}"
when: proxmox_vm_min_memory is defined
- name: Generate hotplug options
ansible.builtin.set_fact:
proxmox_create_vm_qm_args: "{{ proxmox_create_vm_qm_args }} --hotplug {{ proxmox_vm_hotplug }}"
when: proxmox_vm_hotplug is defined
- name: Generate onboot options
ansible.builtin.set_fact:
proxmox_create_vm_qm_args: "{{ proxmox_create_vm_qm_args }} --onboot {{ proxmox_vm_onboot }}"
when: proxmox_vm_onboot is defined
- name: Get next free VM ID
ansible.builtin.command: pvesh get /cluster/nextid
register: vmid
changed_when: false
- name: Clone template # noqa no-changed-when
ansible.builtin.command: "qm clone {{ proxmox_vm_templateid }} {{ vmid.stdout }} --name {{ proxmox_vm_name }} --full true" # noqa yaml[line-length]
- name: Resize harddisk # noqa no-changed-when - name: Create new VM
ansible.builtin.command: "qm resize {{ vmid.stdout }} scsi0 {{ proxmox_vm_disksize }}" when: proxmox_vm_name not in proxmox_vm_existing
include_tasks: create_vm.yml
- name: Set VM options # noqa no-changed-when
ansible.builtin.command: "qm set {{ vmid.stdout }} {{ proxmox_create_vm_qm_args }}"
- name: Start VM # noqa no-changed-when
ansible.builtin.command: "qm start {{ vmid.stdout }}"
- name: Enable HA for VM # noqa no-changed-when - name: Update existing VM
ansible.builtin.command: "ha-manager add {{ vmid.stdout }}" when: proxmox_vm_name in proxmox_vm_existing
when: proxmox_vm_enable_ha is defined and proxmox_vm_enable_ha include_tasks: update_vm.yml
---
- name: Set ipconfig options
ansible.builtin.lineinfile:
path: "/etc/pve/nodes/{{ proxmox_vm_node }}/qemu-server/{{ proxmox_vm_id }}.conf"
regexp: '^ipconfig{{ index }}'
line: "ipconfig{{ index }}: {{ item }}"
loop: "{{ proxmox_vm_ipconfig_list }}"
loop_control:
index_var: index
- name: Set net options
ansible.builtin.lineinfile:
path: "/etc/pve/nodes/{{ proxmox_vm_node }}/qemu-server/{{ proxmox_vm_id }}.conf"
regexp: '^net{{ index }}: virtio=([A-Z0-9:]{17}),.*$'
line: 'net{{ index }}: virtio=\1,{{ item }}'
backrefs: yes
loop: "{{ proxmox_vm_net_list }}"
loop_control:
index_var: index
when: proxmox_vm_net is defined
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment