Files
ansible/roles/proxmox/tasks/56_provision_new_vm.yaml
Tuan-Dat Tran ef652fac20 refactor: yml -> yaml
Signed-off-by: Tuan-Dat Tran <tuan-dat.tran@tudattr.dev>
2025-11-07 20:44:14 +01:00

112 lines
3.3 KiB
YAML

---
- name: Debug proxmox_deploy_info
ansible.builtin.debug:
msg: "{{ proxmox_deploy_info }}"
- name: Get MAC Address of new machine
ansible.builtin.set_fact:
mac_address: "{{ proxmox_deploy_info.mac.net0 }}"
- name: Import disk
ansible.builtin.shell: |
qm importdisk {{ vm.vmid }} {{ proxmox_dirs.isos }}/{{ vm.boot_image }} {{ proxmox_storage }}
delegate_to: "{{ vm.node }}"
when: proxmox_deploy_info.changed
- name: Attach disk and cloud-init
ansible.builtin.shell: |
qm set {{ vm.vmid }} --scsi0 {{ proxmox_storage }}:{{ vm.vmid }}/vm-{{ vm.vmid }}-disk-0.raw --ide2 {{ proxmox_storage }}:cloudinit --boot order=scsi0
delegate_to: "{{ vm.node }}"
changed_when: true
- name: Resize scsi0 disk if needed
ansible.builtin.shell: |
qm resize {{ vm.vmid }} scsi0 {{ vm.disk_size }}G
delegate_to: "{{ vm.node }}"
- name: Start VM
community.proxmox.proxmox_kvm:
api_user: "{{ proxmox_api_user }}@pam"
api_token_id: "{{ proxmox_api_token_id }}"
api_token_secret: "{{ proxmox_api_token_secret }}"
api_host: "192.168.20.12"
node: "{{ vm.node }}"
vmid: "{{ vm.vmid }}"
state: started
- name: Retry stopping VM
ansible.builtin.include_tasks: ./57_stop_and_verify_vm.yaml
- name: Pause for 5 seconds for api
ansible.builtin.pause:
seconds: 5
- name: Start VM
community.proxmox.proxmox_kvm:
api_user: "{{ proxmox_api_user }}@pam"
api_token_id: "{{ proxmox_api_token_id }}"
api_token_secret: "{{ proxmox_api_token_secret }}"
api_host: "192.168.20.12"
node: "{{ vm.node }}"
vmid: "{{ vm.vmid }}"
state: started
- name: Wait for VM to appear on network
ansible.builtin.shell: |
nmap -sn -n -PR 192.168.20.0/24 | grep -B2 "{{ mac_address }}" | grep "Nmap scan report for"
register: vm_nmap_scan
retries: 30
delay: 5
until: vm_nmap_scan.stdout != ""
delegate_to: "{{ vm.node }}"
- name: Extract the IP address from Nmap output
ansible.builtin.set_fact:
vm_found_ip: "{{ vm_nmap_scan.stdout | regex_search('Nmap scan report for ([0-9\\.]+)', '\\1') | first }}"
- name: Append new VM to SSH config "{{ vm.name }}"
ansible.builtin.blockinfile:
path: "{{ ansible_env.HOME }}/.ssh/config_homelab"
marker: "# {mark} HOMELAB VM: {{ vm.name }} BLOCK"
block: |
Host {{ vm.name }}
HostName {{ vm_found_ip }}
Port 22
User {{ user }}
IdentityFile {{ pk_path }}
IdentityFile ~/.ssh/id_ed25519
IdentityFile ~/.ssh/id_ed25519-cert.pub
ProxyJump {{ vm.node }}
StrictHostKeyChecking no
# - name: Add VM to homelab_vms group in production.ini
# ansible.builtin.lineinfile:
# path: "{{ inventory_file }}"
# line: "{{ vm.name }}"
# insertafter: '^\[vms\]'
# create: true
# state: present
# delegate_to: localhost
- name: Copy VM check script to node
ansible.builtin.copy:
src: check_proxmox_vm.sh
dest: /usr/local/bin/check_proxmox_vm.sh
mode: '0755'
delegate_to: "{{ vm.node }}"
- name: Creates PATH-entry for crontab
ansible.builtin.cron:
name: PATH
env: true
job: /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
delegate_to: "{{ vm.node }}"
- name: Schedule VM check script
ansible.builtin.cron:
name: "Check VM {{ vm.name }}"
job: "/usr/local/bin/check_proxmox_vm.sh {{ vm.vmid }} {{ vm_found_ip }}"
minute: "*/5"
delegate_to: "{{ vm.node }}"