4
0
proxmox-scripts/vm/create_from_template.sh

39 lines
1.3 KiB
Bash
Raw Normal View History

2024-05-11 20:30:37 +02:00
#!/bin/bash
2024-09-10 10:55:38 +02:00
TEMPLATE_VM_ID=202404001
2024-05-11 20:30:37 +02:00
INPUT_FILE=$1
2024-09-10 10:55:38 +02:00
POOL_NAME=$2
NEXT_VM_ID=$3
2024-05-11 20:30:37 +02:00
2024-09-10 10:55:38 +02:00
if ! command -v jq &> /dev/null; then
echo "jq is not installed. Please install it using 'apt install jq' to continue."
exit 1
fi
if [ -z "$INPUT_FILE" ] | [ -z "$POOL_NAME" ] | [ -z "$NEXT_VM_ID" ]; then
echo "Usage: $0 <path-to-input-file> <resource-pool-name> <start-vmid>"
2024-05-11 20:30:37 +02:00
echo "Input file format:"
2024-09-10 10:55:38 +02:00
echo -e "\tusername1"
echo -e "\tusername2"
2024-05-11 20:30:37 +02:00
echo -e "\t..."
exit 1
fi
2024-09-10 10:55:38 +02:00
POOL_EXISTS=$(pvesh get /pools --output-format=json | jq -r ".[] | select(.poolid == \"$POOL_NAME\") | .poolid")
2024-05-11 20:30:37 +02:00
2024-09-10 10:55:38 +02:00
if [ "$POOL_EXISTS" == "$POOL_NAME" ]; then
echo "Resource pool '$POOL_NAME' exists."
else
echo "Resource pool '$POOL_NAME' does not exist, creating..."
pvesh create /pools --poolid $POOL_NAME
fi
2024-05-11 20:30:37 +02:00
2024-09-10 10:55:38 +02:00
while read -r username; do
USERNAME=$(echo "$line")
echo "Creating VM for $username in $POOL_NAME"
qm clone $TEMPLATE_VM_ID $NEXT_VM_ID --name "$username" --full false --pool $POOL_NAME
2024-09-10 10:58:00 +02:00
qm set $NEXT_VM_ID --args "-fw_cfg name=\"opt/fr.iut-fbleau/hostname\",string=\"vm-$username\" -fw_cfg name=\"opt/fr.iut-fbleau/domainname\",string=\"vm.iut-fbleau.fr\""
2024-09-10 10:55:38 +02:00
pvesh set /access/acl --path /vms/$NEXT_VM_ID --roles PVEVMUser --users "$username@arda.lan"
2024-05-11 20:30:37 +02:00
((NEXT_VM_ID++))
done < "$INPUT_FILE"