From 5426456a4d89ed9fcfcdceef6eb1612b5ffe934e Mon Sep 17 00:00:00 2001 From: jerick Date: Mon, 2 Feb 2026 16:39:27 -0500 Subject: [PATCH] Changes to reference template hostname until the vm is renamed --- Jenkinsfile | 51 +++++++++++++++++++++++++++-- playbooks/set_hostname.yml | 27 +++++++++++++++ roles/proxmox_lxc/defaults/main.yml | 1 + roles/proxmox_vm/defaults/main.yml | 1 + 4 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 playbooks/set_hostname.yml diff --git a/Jenkinsfile b/Jenkinsfile index 3804b55..934fd78 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -42,6 +42,8 @@ pipeline { environment { ANSIBLE_HOST_KEY_CHECKING = 'False' ANSIBLE_FORCE_COLOR = 'true' + VM_TEMPLATE_HOSTNAME = 'ubuntu24vm' + LXC_TEMPLATE_HOSTNAME = 'ubuntu24lxc' } stages { @@ -97,8 +99,10 @@ pipeline { stage('Wait for Machine to Boot') { steps { script { - def targetHost = "${params.HOSTNAME}.lan" - echo "Waiting for ${targetHost} to become available..." + // Use template hostname for ping since the cloned machine still has the template's hostname + def templateHost = params.PROVISION_TYPE == 'VM' ? env.VM_TEMPLATE_HOSTNAME : env.LXC_TEMPLATE_HOSTNAME + def targetHost = "${templateHost}.lan" + echo "Waiting for ${targetHost} (template hostname) to become available..." // Wait up to 3 minutes for the machine to respond to ping timeout(time: 3, unit: 'MINUTES') { @@ -120,7 +124,8 @@ pipeline { stage('Copy Jenkins SSH Key') { steps { script { - def targetHost = "${params.HOSTNAME}.lan" + def templateHost = params.PROVISION_TYPE == 'VM' ? env.VM_TEMPLATE_HOSTNAME : env.LXC_TEMPLATE_HOSTNAME + 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 @@ -131,6 +136,46 @@ pipeline { } } + stage('Set Hostname') { + steps { + script { + def templateHost = params.PROVISION_TYPE == 'VM' ? env.VM_TEMPLATE_HOSTNAME : env.LXC_TEMPLATE_HOSTNAME + 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 { diff --git a/playbooks/set_hostname.yml b/playbooks/set_hostname.yml new file mode 100644 index 0000000..31c18e0 --- /dev/null +++ b/playbooks/set_hostname.yml @@ -0,0 +1,27 @@ +--- +- name: Set hostname on newly provisioned machine + hosts: all + become: true + gather_facts: false + + tasks: + - name: Set the hostname + ansible.builtin.hostname: + name: "{{ new_hostname }}" + + - name: Update /etc/hosts with new hostname + ansible.builtin.lineinfile: + path: /etc/hosts + regexp: '^127\.0\.1\.1' + line: "127.0.1.1 {{ new_hostname }}" + state: present + + - name: Restart systemd-hostnamed to apply changes + ansible.builtin.systemd: + name: systemd-hostnamed + state: restarted + ignore_errors: true + + - name: Display new hostname + ansible.builtin.debug: + msg: "Hostname set to {{ new_hostname }}" diff --git a/roles/proxmox_lxc/defaults/main.yml b/roles/proxmox_lxc/defaults/main.yml index 247b33c..11c8a0c 100644 --- a/roles/proxmox_lxc/defaults/main.yml +++ b/roles/proxmox_lxc/defaults/main.yml @@ -1,3 +1,4 @@ --- lxc_template_id: "109" lxc_template_node: "homestrg1" +lxc_template_hostname: "ubuntu24lxc" diff --git a/roles/proxmox_vm/defaults/main.yml b/roles/proxmox_vm/defaults/main.yml index b9e739e..99eacd3 100644 --- a/roles/proxmox_vm/defaults/main.yml +++ b/roles/proxmox_vm/defaults/main.yml @@ -1,3 +1,4 @@ --- vm_template_id: "133" vm_template_node: "homestrg1" +vm_template_hostname: "ubuntu24vm"