Changes to reference template hostname until the vm is renamed

This commit is contained in:
2026-02-02 16:39:27 -05:00
parent 678625e634
commit 5426456a4d
4 changed files with 77 additions and 3 deletions

51
Jenkinsfile vendored
View File

@@ -42,6 +42,8 @@ pipeline {
environment { environment {
ANSIBLE_HOST_KEY_CHECKING = 'False' ANSIBLE_HOST_KEY_CHECKING = 'False'
ANSIBLE_FORCE_COLOR = 'true' ANSIBLE_FORCE_COLOR = 'true'
VM_TEMPLATE_HOSTNAME = 'ubuntu24vm'
LXC_TEMPLATE_HOSTNAME = 'ubuntu24lxc'
} }
stages { stages {
@@ -97,8 +99,10 @@ pipeline {
stage('Wait for Machine to Boot') { stage('Wait for Machine to Boot') {
steps { steps {
script { script {
def targetHost = "${params.HOSTNAME}.lan" // Use template hostname for ping since the cloned machine still has the template's hostname
echo "Waiting for ${targetHost} to become available..." 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 // Wait up to 3 minutes for the machine to respond to ping
timeout(time: 3, unit: 'MINUTES') { timeout(time: 3, unit: 'MINUTES') {
@@ -120,7 +124,8 @@ pipeline {
stage('Copy Jenkins SSH Key') { stage('Copy Jenkins SSH Key') {
steps { steps {
script { 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 // Use sshpass or expect to handle the initial connection
// This assumes the template has a default user that accepts the key // 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') { stage('Configure Machine') {
steps { steps {
script { script {

View File

@@ -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 }}"

View File

@@ -1,3 +1,4 @@
--- ---
lxc_template_id: "109" lxc_template_id: "109"
lxc_template_node: "homestrg1" lxc_template_node: "homestrg1"
lxc_template_hostname: "ubuntu24lxc"

View File

@@ -1,3 +1,4 @@
--- ---
vm_template_id: "133" vm_template_id: "133"
vm_template_node: "homestrg1" vm_template_node: "homestrg1"
vm_template_hostname: "ubuntu24vm"