diff --git a/roles/deploy_vm_on_proxmox/tasks/create_vm.yaml b/roles/deploy_vm_on_proxmox/tasks/create_vm.yaml new file mode 100644 index 0000000..52bbc27 --- /dev/null +++ b/roles/deploy_vm_on_proxmox/tasks/create_vm.yaml @@ -0,0 +1,73 @@ +--- + +- name: Create a new VM with minimal options only if it doesn't exist + become: false + community.proxmox.proxmox_kvm: + validate_certs: false + node: proxmox-server + api_user: root@pam + api_host: 192.168.0.2 + api_token_id: ansible + api_token_secret: "{{ proxmox_token_secret }}" + + clone: debian-13-cloud-init-template + name: "{{ vm_hostname }}" + storage: main + format: qcow2 + full: true + timeout: 500 + +- name: Sleep for 20 seconds to ensure that Proxmox has cloned the VM + become: false + ansible.builtin.wait_for: + timeout: 20 + delegate_to: localhost + changed_when: false + +- name: Update the cloned VM with cloud-init configuration + community.proxmox.proxmox_kvm: + validate_certs: false + node: proxmox-server + api_user: root@pam + api_host: 192.168.0.2 + api_token_id: ansible + api_token_secret: "{{ proxmox_token_secret }}" + + name: "{{ vm_hostname }}" + # Cloud-init parameters + update: true + ide: + ide2: "local:cloudinit,format=qcow2" + ciuser: "ansible" + cipassword: "{{ ansible_become_passwd }}" + ipconfig: + ipconfig0: "ip={{ vm_ip_address }},gw=192.168.0.1" + +- name: Tweak the hardware settings + become: false + community.proxmox.proxmox_kvm: + validate_certs: false + node: proxmox-server + api_user: root@pam + api_host: 192.168.0.2 + api_token_id: ansible + api_token_secret: "{{ proxmox_token_secret }}" + + name: "{{ vm_hostname }}" + cores: 4 + memory: 4096 # Minimal value for debian based OS for smooth workflow + update: true + +- name: Make sure the VM has started + become: false + community.proxmox.proxmox_kvm: + validate_certs: false + node: proxmox-server + api_user: root@pam + api_host: 192.168.0.2 + api_token_id: ansible + api_token_secret: "{{ proxmox_token_secret }}" + + name: "{{ vm_hostname }}" + state: started + diff --git a/roles/deploy_vm_on_proxmox/tasks/main.yaml b/roles/deploy_vm_on_proxmox/tasks/main.yaml index 5cce14f..07f5a53 100644 --- a/roles/deploy_vm_on_proxmox/tasks/main.yaml +++ b/roles/deploy_vm_on_proxmox/tasks/main.yaml @@ -17,87 +17,9 @@ - name: Create a new VM with minimal options only if it doesn't exist when: vm_status is failed or 'does not exist in cluster' in vm_status.msg - become: false - community.proxmox.proxmox_kvm: - validate_certs: false - node: proxmox-server - api_user: root@pam - api_host: 192.168.0.2 - api_token_id: ansible - api_token_secret: "{{ proxmox_token_secret }}" + ansible.builtin.include_tasks: create_vm.yaml - clone: debian-13-cloud-init-template - name: "{{ vm_hostname }}" - storage: main - format: qcow2 - full: true - timeout: 500 - -- name: Sleep for 20 seconds to ensure that Proxmox has cloned the VM - become: false - ansible.builtin.wait_for: - timeout: 20 - delegate_to: localhost - changed_when: false - -- name: Update the cloned VM with cloud-init configuration - community.proxmox.proxmox_kvm: - validate_certs: false - node: proxmox-server - api_user: root@pam - api_host: 192.168.0.2 - api_token_id: ansible - api_token_secret: "{{ proxmox_token_secret }}" - - name: "{{ vm_hostname }}" - # Cloud-init parameters - update: true - ide: - ide2: "local:cloudinit,format=qcow2" - ciuser: "ansible" - cipassword: "{{ ansible_become_passwd }}" - ipconfig: - ipconfig0: "ip={{ vm_ip_address }},gw=192.168.0.1" - -- name: Sleep for 20 seconds just to make sure that all settings are applied - become: false - ansible.builtin.wait_for: - timeout: 20 - delegate_to: localhost - changed_when: false - -- name: Tweak the hardware settings - become: false - community.proxmox.proxmox_kvm: - validate_certs: false - node: proxmox-server - api_user: root@pam - api_host: 192.168.0.2 - api_token_id: ansible - api_token_secret: "{{ proxmox_token_secret }}" - - name: "{{ vm_hostname }}" - cores: 4 - memory: 4096 # Minimal value for debian based OS for smooth workflow - update: true - -- name: Make sure the VM has started - become: false - community.proxmox.proxmox_kvm: - validate_certs: false - node: proxmox-server - api_user: root@pam - api_host: 192.168.0.2 - api_token_id: ansible - api_token_secret: "{{ proxmox_token_secret }}" - - name: "{{ vm_hostname }}" - state: started - -- name: Sleep for 120 seconds just to make sure that all settings are applied - become: false - ansible.builtin.wait_for: - timeout: 120 - delegate_to: localhost - changed_when: false +- name: Start the VM only if needed + when: '"does not exist in cluster" in vm_status.msg or vm_status.status != "running"' + ansible.builtin.include_tasks: start_vm.yaml diff --git a/roles/deploy_vm_on_proxmox/tasks/start_vm.yaml b/roles/deploy_vm_on_proxmox/tasks/start_vm.yaml new file mode 100644 index 0000000..455bf6f --- /dev/null +++ b/roles/deploy_vm_on_proxmox/tasks/start_vm.yaml @@ -0,0 +1,22 @@ +--- + +- name: Make sure the VM has started + become: false + community.proxmox.proxmox_kvm: + validate_certs: false + node: proxmox-server + api_user: root@pam + api_host: 192.168.0.2 + api_token_id: ansible + api_token_secret: "{{ proxmox_token_secret }}" + + name: "{{ vm_hostname }}" + state: started + +- name: Sleep for 120 seconds to ensure that Proxmox has cloned the VM + become: false + ansible.builtin.wait_for: + timeout: 120 + delegate_to: localhost + changed_when: false +