Update .gitignore and refactor Ubuntu template playbook to use role for Proxmox template management

This commit is contained in:
Nikholas Pcenicni
2026-03-27 03:48:32 -04:00
parent 87e71dcd8a
commit 51d55af86e
23 changed files with 676 additions and 65 deletions

View File

@@ -2,71 +2,15 @@
- name: Create Ubuntu Cloud-Init Template
hosts: proxmox
become: yes
vars:
template_id: 9000
template_name: ubuntu-2204-cloud
# URL for Ubuntu 22.04 Cloud Image (Jammy)
image_url: "https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img"
image_name: "ubuntu-22.04-server-cloudimg-amd64.img"
storage_pool: "local-lvm"
memory: 2048
cores: 2
# Override defaults if needed
image_alias: ubuntu-22.04
storage_pool: local-lvm
tasks:
- name: Check if template already exists
command: "qm status {{ template_id }}"
register: vm_status
failed_when: false
changed_when: false
- name: Fail if template ID exists
fail:
msg: "VM ID {{ template_id }} already exists. Please choose a different ID or delete the existing VM."
when: vm_status.rc == 0
- name: Download Ubuntu Cloud Image
get_url:
url: "{{ image_url }}"
dest: "/tmp/{{ image_name }}"
mode: '0644'
- name: Install libguestfs-tools (required for virt-customize if needed, optional)
apt:
name: libguestfs-tools
state: present
ignore_errors: yes
- name: Create VM with hardware config
command: >
qm create {{ template_id }}
--name "{{ template_name }}"
--memory {{ memory }}
--core {{ cores }}
--net0 virtio,bridge=vmbr0
--scsihw virtio-scsi-pci
--ostype l26
--serial0 socket --vga serial0
- name: Import Disk
command: "qm importdisk {{ template_id }} /tmp/{{ image_name }} {{ storage_pool }}"
- name: Attach Disk to SCSI
command: "qm set {{ template_id }} --scsi0 {{ storage_pool }}:vm-{{ template_id }}-disk-0"
- name: Add Cloud-Init Drive
command: "qm set {{ template_id }} --ide2 {{ storage_pool }}:cloudinit"
- name: Set Boot Order
command: "qm set {{ template_id }} --boot c --bootdisk scsi0"
- name: Resize Disk (Optional, e.g. 10G)
command: "qm resize {{ template_id }} scsi0 10G"
ignore_errors: yes
- name: Convert to Template
command: "qm template {{ template_id }}"
- name: Remove Downloaded Image
file:
path: "/tmp/{{ image_name }}"
state: absent
- name: Run Proxmox Template Manage Role
include_role:
name: proxmox_template_manage

View File

@@ -0,0 +1,33 @@
---
- name: Hello World Provisioning
hosts: localhost # Run API calls from control node
gather_facts: no
vars_files:
- "../inventory/hosts.ini" # Load connection details if needed manually, OR rely on inventory
vars:
# Target Proxmox Details (override from inventory/extra vars)
proxmox_api_host: "192.168.50.100"
proxmox_api_user: "root@pam"
proxmox_api_password: "Hemroid8" # Consider moving to Vault!
proxmox_node: "mercury"
# VM Spec
vmid: 101
vm_name: "hello-world-vm"
template_name: "ubuntu-2204-cloud"
ci_user: "ubuntu"
# Replace with your actual public key or pass via -e "ssh_key=..."
ssh_keys:
- "{{ ssh_key | default('ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI...') }}"
tasks:
- name: Run Proxmox Provision Role
include_role:
name: proxmox_provision
vars:
vmid: "{{ vmid }}"
vm_name: "{{ vm_name }}"
template_name: "{{ template_name }}"
ci_user: "{{ ci_user }}"
ssh_keys: "{{ ssh_keys }}"

View File

@@ -0,0 +1,23 @@
---
# Defaults for proxmox_provision role
# Connection Details (fallbacks, but ideally inherited from inventory group_vars)
proxmox_api_host: "{{ ansible_host | default(inventory_hostname) }}"
proxmox_node: "{{ inventory_hostname }}"
# VM Details
vmid: 0 # 0 lets Proxmox choose next available, or specify fixed ID
vm_name: "new-vm"
template_name: "ubuntu-2204-cloud"
vm_memory: 2048
vm_cores: 2
vm_storage: "local-lvm"
vm_net_bridge: "vmbr0"
# Cloud Init / User Data
ci_user: "ubuntu"
# ssh_keys should be a list of public keys
ssh_keys: []
# State
vm_state: started

View File

@@ -0,0 +1,39 @@
---
- name: Provision VM from Template
community.general.proxmox_kvm:
api_host: "{{ proxmox_api_host }}"
api_user: "{{ proxmox_api_user }}"
api_password: "{{ proxmox_api_password }}"
# Use remote host verification if you have valid certs, else ignore
validate_certs: false
node: "{{ proxmox_node }}"
vmid: "{{ vmid if vmid | int > 0 else omit }}"
name: "{{ vm_name }}"
clone: "{{ template_name }}"
full: true # Full clone
cores: "{{ vm_cores }}"
memory: "{{ vm_memory }}"
storage: "{{ vm_storage }}"
net:
net0: "virtio,bridge={{ vm_net_bridge }}"
# Cloud Init
ciuser: "{{ ci_user }}"
sshkeys: "{{ ssh_keys | join('\n') }}"
ipconfig:
ipconfig0: "ip=dhcp"
state: "{{ vm_state }}"
register: vm_provision_result
- name: Debug Provision Result
debug:
var: vm_provision_result
# Note: Waiting for SSH requires knowing the IP.
# If qemu-guest-agent is installed in the template, we can fetch it.
# Otherwise, we might need a fixed IP or DNS check.

View File

@@ -0,0 +1,41 @@
---
# Defaults for proxmox_template_manage role
# Target Proxmox Node (where commands run)
proxmox_node: "{{ inventory_hostname }}"
# Storage Pool for Disks
storage_pool: local-lvm
# Template ID and Name
template_id: 9000
template_name: ubuntu-2204-cloud-template
# Hardware Specs
template_memory: 2048
template_cores: 2
# Image Source
# Options: 'list' (use image_alias) or 'url' (use custom_image_url)
image_source_type: list
image_list:
ubuntu-22.04:
url: "https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img"
filename: "ubuntu-22.04-server-cloudimg-amd64.img"
ubuntu-24.04:
url: "https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img"
filename: "ubuntu-24.04-server-cloudimg-amd64.img"
debian-12:
url: "https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-generic-amd64.qcow2"
filename: "debian-12-generic-amd64.qcow2"
image_alias: ubuntu-22.04
custom_image_url: ""
custom_image_name: "custom-image.img"
# Cloud Init / SSH
# Optional: Embed a default admin key into the template
embed_admin_ssh_key: false
admin_ssh_key: ""

View File

@@ -0,0 +1,90 @@
---
- name: Resolve Image Variables (List)
set_fact:
_image_url: "{{ image_list[image_alias].url }}"
_image_name: "{{ image_list[image_alias].filename }}"
when: image_source_type == 'list'
- name: Resolve Image Variables (URL)
set_fact:
_image_url: "{{ custom_image_url }}"
_image_name: "{{ custom_image_name }}"
when: image_source_type == 'url'
- name: Check if template already exists
command: "qm status {{ template_id }}"
register: vm_status
failed_when: false
changed_when: false
- name: Fail if template ID exists
fail:
msg: "VM ID {{ template_id }} already exists. Please delete it or choose a different ID."
when: vm_status.rc == 0
- name: Download Cloud Image
get_url:
url: "{{ _image_url }}"
dest: "/tmp/{{ _image_name }}"
mode: '0644'
- name: Install libguestfs-tools (for virt-customize if needed)
apt:
name: libguestfs-tools
state: present
ignore_errors: yes
- name: Create VM with hardware config
command: >
qm create {{ template_id }}
--name "{{ template_name }}"
--memory {{ template_memory }}
--core {{ template_cores }}
--net0 virtio,bridge=vmbr0
--scsihw virtio-scsi-pci
--ostype l26
--serial0 socket --vga serial0
- name: Import Disk
command: "qm importdisk {{ template_id }} /tmp/{{ _image_name }} {{ storage_pool }}"
- name: Attach Disk to SCSI
command: "qm set {{ template_id }} --scsi0 {{ storage_pool }}:vm-{{ template_id }}-disk-0"
- name: Add Cloud-Init Drive
command: "qm set {{ template_id }} --ide2 {{ storage_pool }}:cloudinit"
- name: Set Boot Order
command: "qm set {{ template_id }} --boot c --bootdisk scsi0"
- name: Configure Cloud-Init (Optional Admin Key)
block:
- name: Prepare SSH Keys File
copy:
content: "{{ admin_ssh_key }}"
dest: "/tmp/ssh_key_{{ template_id }}.pub"
mode: '0600'
- name: Set SSH Keys on Template
command: "qm set {{ template_id }} --sshkeys /tmp/ssh_key_{{ template_id }}.pub"
- name: Cleanup Key File
file:
path: "/tmp/ssh_key_{{ template_id }}.pub"
state: absent
when: embed_admin_ssh_key | bool and admin_ssh_key | length > 0
- name: Set Cloud-Init IP Config (DHCP)
command: "qm set {{ template_id }} --ipconfig0 ip=dhcp"
- name: Resize Disk (to Minimum 10G)
command: "qm resize {{ template_id }} scsi0 10G"
ignore_errors: yes
- name: Convert to Template
command: "qm template {{ template_id }}"
- name: Remove Downloaded Image
file:
path: "/tmp/{{ _image_name }}"
state: absent