Skip to content

Instantly share code, notes, and snippets.

@yashodhank
Created September 4, 2023 01:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yashodhank/9c0b0b3f24449202d3fbbdb5721c1561 to your computer and use it in GitHub Desktop.
Save yashodhank/9c0b0b3f24449202d3fbbdb5721c1561 to your computer and use it in GitHub Desktop.
This script automate the process to create random username and password and required role with privilages for Packer to work with Proxmox Host.
#!/bin/bash
# Function to generate a strong password
generate_password() {
cat /dev/urandom | tr -dc 'a-zA-Z0-9!@#$%^&*()-_+=~' | fold -w 16 | head -n 1
}
# Function to generate a random username
generate_username() {
cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 8 | head -n 1
}
# Step 1: Detect Proxmox Hostname or ask the user if detection fails
HOSTNAME=$(hostname -f)
if [ -z "$HOSTNAME" ]; then
read -p "Unable to detect Proxmox hostname. Please enter it: " HOSTNAME
fi
# Step 2: Ask the user for the username and password, generate them if not provided
read -p "Enter the username for the new user (Press Enter to generate a random username): " USERNAME
if [ -z "$USERNAME" ]; then
USERNAME=$(generate_username)
fi
read -sp "Enter the password for the new user (Press Enter to generate a random password): " PASSWORD
echo
if [ -z "$PASSWORD" ]; then
PASSWORD=$(generate_password)
fi
# Step 3: Execute the commands to add the user with the specified privileges
pveum useradd $USERNAME@pve
pveum passwd $USERNAME@pve <<EOF
$PASSWORD
EOF
# Adding role with the necessary privileges
PRIVILEGES="VM.Config.Disk,VM.Config.CPU,VM.Config.Memory,Datastore.AllocateSpace,Sys.Modify,VM.Config.Options,VM.Allocate,VM.Audit,VM.Console,VM.Config.CDROM,VM.Config.Network,VM.PowerMgmt,VM.Config.HWType,VM.Monitor"
pveum roleadd Packer -privs "$PRIVILEGES"
# Assigning the role to the user
pveum aclmod / -user $USERNAME@pve -role Packer
# Step 4: Display all necessary information to the script operator
echo -e "User addition successful!\n"
echo "Proxmox Hostname: $HOSTNAME"
echo "New User Username: $USERNAME"
echo "New User Password: $PASSWORD"
echo "New User Role: Packer"
echo -e "Privileges assigned to the role:\n$PRIVILEGES"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment