95 lines
2.9 KiB
YAML
95 lines
2.9 KiB
YAML
---
|
|
- name: Create VM
|
|
community.general.proxmox_kvm:
|
|
api_user: root@pam
|
|
api_password: "{{ vault.pve.aya01.root.sudo }}"
|
|
api_host: "192.168.20.12"
|
|
agent: true
|
|
name: "{{ vm.name }}"
|
|
vmid: "{{ vm.vmid }}"
|
|
node: "{{ vm.node }}"
|
|
cores: "{{ vm.cores }}"
|
|
memory: "{{ vm.memory }}"
|
|
net: "{{ vm.net }}"
|
|
scsihw: "virtio-scsi-pci"
|
|
ostype: "l26"
|
|
tags: "{{ proxmox_tags }}"
|
|
description: "Created via Ansible with cloud-init"
|
|
boot: "order=scsi0"
|
|
cpu: "x86-64-v2-AES"
|
|
ciuser: "{{ vm.ciuser }}"
|
|
cipassword: "{{ vm_secrets[proxmox_secrets_prefix + '_' + vm.name.replace('-', '_')] }}"
|
|
ipconfig:
|
|
ipconfig0: "ip=dhcp"
|
|
sshkeys: "{{ vm.sshkeys }}"
|
|
register: 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 }}"
|
|
|
|
- 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 }}"
|
|
|
|
- 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.general.proxmox_kvm:
|
|
api_user: root@pam
|
|
api_password: "{{ vault.pve.aya01.root.sudo }}"
|
|
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: Debug IP address
|
|
ansible.builtin.debug:
|
|
msg: "Found VM IP address: {{ vm_found_ip }}"
|
|
|
|
- name: Define SSH config block
|
|
ansible.builtin.set_fact:
|
|
ssh_entry: |
|
|
Host {{ vm.name }}
|
|
HostName {{ vm_found_ip }}
|
|
Port 22
|
|
User tudattr
|
|
IdentityFile /media/veracrypt1/genesis
|
|
ProxyJump {{ vm.node }}
|
|
|
|
- name: Append new VM to SSH config
|
|
ansible.builtin.blockinfile:
|
|
path: "{{ ansible_env.HOME }}/.ssh/config_homelab"
|
|
marker: "# {mark} HOMELAB VMS BLOCK"
|
|
block: |
|
|
{{ ssh_entry }}
|
|
|
|
- name: Add the new VM to the proxmox_nodes group in production.ini
|
|
ansible.builtin.lineinfile:
|
|
path: "../inventory.ini"
|
|
line: "{{ proxmox_inventory_entry }}"
|
|
insertafter: "[proxmox_nodes]"
|
|
state: present
|