This Ansible playbook utilizes proxmoxer, cloud-init, and Qemu/KVM Virtual Machine Manager to deploy Virtual Machines.
Login to your proxmox node and run the following commands.
apt install -y python3-pip build-essential
pip3 install --upgrade pip
pip3 install virtualenv
pip3 install proxmoxer
Adjust the VM IDs below to your preference. You will need to su - root
to set the environment for qm command.
wget https://cloud.debian.org/images/cloud/bullseye/latest/debian-11-generic-amd64.qcow2
qm create 400 --name "debian-2022-template" --memory 4096 --net0 virtio,bridge=vmbr0 --cores 2
qm importdisk 400 ~/debian-11-generic-amd64.qcow2 local-lvm
qm set 400 --scsihw virtio-scsi-pci --virtio0 local-lvm:vm-400-disk-0
qm set 400 --boot c --bootdisk virtio0
qm set 400 --ide2 local-lvm:cloudinit
qm set 400 --agent 1
qm template 400
Replace the IP addresses below to match your desired environment.
qm clone 400 401 --name vm-ansible-host
qm set 401 --sshkey ~/.ssh/id_rsa.pub
qm set 401 --ipconfig0 ip=x.x.x.x/x,gw=x.x.x.x
The 2GB disk Proxmox gives you by default is insufficient to run this script. However, due to this bug Debian cloud-init images will get Kernel Panic upon first boot after using qm resize. So the VM will need to be started twice:
qm resize 401 virtio0 16G
qm start 401
qm stop 401
qm start 401
When using cloud-init, there is no password and no root login, but we can establish a connection using the SSH key we created in the last step.
From the proxmox node, ssh [email protected]
and do some basic setup, such as:
sudo adduser user
sudo usermod -aG sudo user
su - user
sudo sed -i 's/[#]*Port 22/Port 3333/g' /etc/ssh/sshd_config
sudo sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config
sudo service sshd restart
sudo passwd root
Then install some programs you'll need, set the environment, and clone the repo.
sudo apt install python3 python3-pip sshpass git -y
pip3 install ansible
echo 'export PATH=/home/user/.local/bin:$PATH' >>~/.bash_profile
source ~/.bash_profile
git clone https://github.com/Fosten/proxmox-deploy-project.git
Now that everything is installed, to deploy more VMs in the future, you can just repeat the commands below.
cd proxmox-deploy-project
nano hosts
Change the following (ssl_verify and ansible_ssh_extra_args can stay the same):
api_node= (Name of Proxmox Node)
api_user= (User with GUI access)
ansible_host= (IP of Proxmox Node)
ansible_user= (User with SSH access)
ansible_ssh_port= (Port to access SSH)
ansible_ssh_private_key_file= (Path to ssh key)
make install