kvm/bolt_vm_automation/tasks/system_setup.sh

51 lines
1.5 KiB
Bash

#!/bin/bash
# Using Bolt's environment variables
IP="${PT_ip_with_cidr}"
HOSTNAME="${PT_hostname}"
DHCP="${PT_dhcp}"
GATEWAY="${PT_gateway}"
NAMESERVER1="${PT_nameserver1}"
NAMESERVER2="${PT_nameserver2}"
NAMESERVER3="${PT_nameserver3}"
# Check if all required parameters are provided
if [ -z "$IP" ] || [ -z "$HOSTNAME" ] || [ -z "$DHCP" ] || [ -z "$GATEWAY" ] || [ -z "$NAMESERVER1" ] || [ -z "$NAMESERVER2" ] || [ -z "$NAMESERVER3" ]; then
echo "Missing required parameters. All parameters must be provided."
exit 1
fi
# Create the new netplan configuration
sudo tee /etc/netplan/50-cloud-init.yaml << EOL
network:
version: 2
ethernets:
enp1s0:
dhcp4: ${DHCP}
EOL
# If DHCP is false, add static IP configuration
if [ "$DHCP" = "false" ]; then
sudo tee -a /etc/netplan/50-cloud-init.yaml << EOL
addresses:
- ${IP}
routes:
- to: default
via: ${GATEWAY}
nameservers:
addresses: [${NAMESERVER1}, ${NAMESERVER2}, ${NAMESERVER3}]
EOL
fi
# Set the hostname
sudo hostnamectl set-hostname "${HOSTNAME}"
echo "${HOSTNAME}" | sudo tee /etc/hostname > /dev/null
# Update /etc/hosts
sudo sed -i "s/127.0.1.1.*/127.0.1.1\t${HOSTNAME}/" /etc/hosts
echo "System configuration completed successfully"
# Apply network configuration in the background and exit before it takes effect
nohup bash -c "(sleep 2 && sudo netplan apply) &" > /dev/null 2>&1
exit 0