This project will easily let you create new Libvirt virtual machines with Terraform in no time.
A bash script will automate the templating for the setup and deployment of the pools, VMs, and volumes and you can then run an Ansible script to properly install tools and stuff on your vms.
Version 0.5
Usage: ./helper.sh [create|addvm|deploy|destroy|clean]
./helper.sh create
./helper.sh addvm
..
..
./helper.sh deploy
./helper.sh destroy (or "clean" if you want to return to clean folder)
- create: initialize project, it will ask for a name
- addvm: creates new vm file, it will ask for cpu, mem, disk etc
- deploy: will deploy the terraform files
- destroy: will tier down created infra
- clean: will tier down terraform infra and delete files. Goes back to original state
Project is still requiring some slight tweaking for a new user, as it is not made to be 100% independent yet. WIP.
Since we want to run in the "user-mode" we do not have access to some network settings, so the default network used here is a bridge interface virbr1
.
From a root session, I created a NAT network which will be named virbr1
. This way, we can attach to it from the user-mode session without any root privileges.
You can create as many networks as you want in the system session, then reference to them in the Terraform variables, ready to be used in the project. The script does not ask for a network for now, just puts into the default virbr1
(can be changed/added in no time).
Create a folder ~/KVM
, where your new VM data will reside.
Ex: you name a VM chip01
?
After deployment you are ready to SSH into it with ssh debian@chip01
-
The libvirt project uses the current user context, nothing runs as root.
-
The pool gets generated dynamically per-project.
-
The common path chosen for the pools is
~/KVM
where the domains qcow files will be stored. Change in vars if wanted -
The
ssh_key_path
variable contains the path to the local ssh key to import into the newly generated VM. Change to your liking -
The first step is to download the debian cloudinit image file. Then libvirt will use this image as base image for all the other VMs created. This way the space used for 1..n VMs will be reduced.
For now the VMs will be using Debian 12.
The initial cloudinit config sets default passwords and your ssh key. (Default passwords are stupid, just set at your own risk or change it as you will )
Further post-install can be done in ansible.
ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -i VMNAME, -e "privkeypath=~/.ssh/<YOURKEYHERE>" ansible/playbook-base.yml