pipeline { agent any parameters { choice( name: 'PROVISION_TYPE', choices: ['VM', 'LXC'], description: 'Select whether to provision a VM or LXC container' ) choice( name: 'TARGET_NODE', choices: ['homeapp1', 'homeapp2', 'homestrg1', 'homeflux1'], description: 'Select the Proxmox node to provision on' ) string( name: 'HOSTNAME', defaultValue: '', description: 'Hostname for the new VM/LXC (required)' ) string( name: 'CPU_CORES', defaultValue: '2', description: 'Number of CPU cores' ) string( name: 'RAM_GB', defaultValue: '2', description: 'RAM in GB' ) booleanParam( name: 'INSTALL_DOCKER', defaultValue: false, description: 'Install Docker and Docker Compose' ) booleanParam( name: 'INSTALL_NFS', defaultValue: false, description: 'Install NFS and mount NFSFolder' ) } environment { ANSIBLE_HOST_KEY_CHECKING = 'False' ANSIBLE_FORCE_COLOR = 'true' } stages { stage('Validate Parameters') { steps { script { if (!params.HOSTNAME?.trim()) { error("HOSTNAME is required") } if (!params.CPU_CORES.isInteger() || params.CPU_CORES.toInteger() < 1) { error("CPU_CORES must be a positive integer") } if (!params.RAM_GB.isInteger() || params.RAM_GB.toInteger() < 1) { error("RAM_GB must be a positive integer") } } } } stage('Install Ansible Collections') { steps { sh 'ansible-galaxy collection install -r requirements.yml -p ./collections --force' } } stage('Provision VM/LXC') { steps { withCredentials([string(credentialsId: 'proxmox-resource-creator', variable: 'PROXMOX_TOKEN')]) { script { // Parse the token: format is user@realm!tokenid=secret def tokenParts = PROXMOX_TOKEN.split('!') def apiUser = tokenParts[0] def tokenIdAndSecret = tokenParts[1].split('=') def apiTokenId = tokenIdAndSecret[0] def apiTokenSecret = tokenIdAndSecret[1] sh """ ansible-playbook playbooks/provision.yml \ -e "proxmox_api_user=${apiUser}" \ -e "proxmox_api_token_id=${apiTokenId}" \ -e "proxmox_api_token_secret=${apiTokenSecret}" \ -e "provision_type=${params.PROVISION_TYPE}" \ -e "target_node=${params.TARGET_NODE}" \ -e "vm_hostname=${params.HOSTNAME}" \ -e "cpu_cores=${params.CPU_CORES}" \ -e "ram_gb=${params.RAM_GB}" """ } } } } stage('Wait for Machine to Boot') { steps { script { // Use template hostname for ping since the cloned machine still has the template's hostname def templateHost = params.PROVISION_TYPE == 'VM' ? 'ubuntu24vm' : 'ubuntu24lxc' def targetHost = "${templateHost}.lan" echo "Waiting for ${targetHost} (template hostname) to become available..." // Wait up to 5 minutes for the machine to respond to ping timeout(time: 5, unit: 'MINUTES') { waitUntil { // Flush DNS cache before each ping attempt sh(script: "sudo systemd-resolve --flush-caches 2>/dev/null || sudo resolvectl flush-caches 2>/dev/null || sudo nscd -i hosts 2>/dev/null || true", returnStatus: true) def result = sh( script: "ping -c 1 ${targetHost} > /dev/null 2>&1", returnStatus: true ) return result == 0 } } // Additional wait for SSH to be ready sleep(time: 30, unit: 'SECONDS') } } } stage('Copy Jenkins SSH Key') { steps { script { def templateHost = params.PROVISION_TYPE == 'VM' ? 'ubuntu24vm' : 'ubuntu24lxc' def targetHost = "${templateHost}.lan" // Use sshpass or expect to handle the initial connection // This assumes the template has a default user that accepts the key sh """ sudo -u jenkins ssh-copy-id -i /var/lib/jenkins/.ssh/id_ed25519.pub -o StrictHostKeyChecking=no jenkins@${targetHost} """ } } } stage('Set Hostname') { steps { script { def templateHost = params.PROVISION_TYPE == 'VM' ? 'ubuntu24vm' : 'ubuntu24lxc' def currentHost = "${templateHost}.lan" def newHostname = params.HOSTNAME // Create a temporary inventory file with the current host writeFile file: 'temp_inventory.yml', text: """--- all: hosts: new_host: ansible_host: ${currentHost} ansible_user: jenkins ansible_ssh_private_key_file: /var/lib/jenkins/.ssh/id_ed25519 """ sh """ ansible-playbook playbooks/set_hostname.yml \ -i temp_inventory.yml \ -e "new_hostname=${newHostname}" """ // Wait for the machine to come back up with the new hostname echo "Waiting for ${newHostname}.lan to become available..." sleep(time: 30, unit: 'SECONDS') timeout(time: 3, unit: 'MINUTES') { waitUntil { def result = sh( script: "ping -c 1 ${newHostname}.lan > /dev/null 2>&1", returnStatus: true ) return result == 0 } } } } } stage('Configure Machine') { steps { script { def targetHost = "${params.HOSTNAME}.lan" // Create a temporary inventory file with the new host writeFile file: 'temp_inventory.yml', text: """--- all: hosts: new_host: ansible_host: ${targetHost} ansible_user: jenkins ansible_ssh_private_key_file: /var/lib/jenkins/.ssh/id_ed25519 """ sh """ ansible-playbook playbooks/configure.yml \ -i temp_inventory.yml \ -e "install_docker=${params.INSTALL_DOCKER}" \ -e "install_nfs=${params.INSTALL_NFS}" """ } } } } post { always { // Clean up temporary inventory file sh 'rm -f temp_inventory.yml || true' } success { echo "Successfully provisioned ${params.PROVISION_TYPE} '${params.HOSTNAME}' on ${params.TARGET_NODE}" echo "Machine is accessible at ${params.HOSTNAME}.lan" } failure { echo "Failed to provision ${params.PROVISION_TYPE} '${params.HOSTNAME}'" } } }