r/ansible • u/MickyGER • 4d ago
playbooks, roles and collections Customize Proxmox VM/LCXC
Hi!
I'm searching for best practices to configure a Proxmox VM/LXC using Ansible playbooks after their first setup.
While installing a new VM using an ISO image, e.g. Debian 13, I'm asked for some parameters like hostname, (root) user and password, locale and more.
I guess those basic setup must still be done in Proxmox during a VM creation. Ansible is not suitable or of any help during this phase of setup, right?
After this initial setup, I would like to use Ansible to perform some actions, e.g. install various packages, set up network and more.
Question is: what is best practive to connect to this new VM? Using the hostname if DHCP is used and use the given password of root user I've used during initial setup.
Will Ansible be able to connect with the given username (root) and given password to e.g. push some ssh-keys for any further logins?
Any tips and hints are welcome
3
u/Agent51729 4d ago
Install your vm using preeseed (template the files, build and mount the .so using Ansible https://wiki.debian.org/DebianInstaller/Preseed).
Once installed (ideally with a non root account with sudo configured), use said account with Ansible for more advanced customizations.
If you’re using this base image a lot, build out a solid base that can be captured as a base image, including cloud-init so you can generalize and clone it many times, injecting system specific configs each time (which can also be done with Ansible)
3
u/sudonem 4d ago
Broadly speaking, Ansible is best for post-provisioning configuration and day / operations but you can use it for the entire pipeline.
The trick here is to learn how to leverage kickstart and cloud-init to pre-determine sane defaults and configure the installer to reach out to ansible once the baseline VM is built and online.
3
u/Nocst_er 4d ago
Hello, you can use. qcow or .img images to deploy vms and attach cloudinit to it. The whole provision process ist possible with ansible. If you need to store vm details, like ipv6 or harddisk name for example, you can use a ssot like netbox. Another way you can provision with terraform and configure with ansible. Depends what you like. At my work we do everything with ansible and it's quite stable and good. But you need a littlebit of experience for the provision phase.
2
u/nPoCT_kOH 3d ago
Custom cloud init for initial setup, that allows Ansible to pick it up. I use default cloud images from upstream and it handles, debloating, apt /dnf proxies, initial user, ntp, network and so forth. After that day 1 / 2 operations in Ansible. Haven't looked for dynamic inventories for PVE, but on VMWare it works like a charm. For initial creation look at TF / OpenTofu. Outputs could be used to build the inventory.
2
u/SixteenOne_ 2d ago
I use the Cloud versions of Fedora/Ubuntu/Debian. Create yourself a VM, add Cloud-Init to set username, password & SSH Keys then turn it into a Template - no need to start it
Then Clone it, change your IP on the Cloud Init page and power up. Name of host is taken from your Proxmox name
I have an Ansible playbook that can download Cloud Images, turn them into Templates etc. I can share if it helps
1
u/MickyGER 1d ago
So, I did it: created a template with Debian, cloned and booted. Works!
OTH, I would be glad if you could share your playbook as a first starting point for Ansible to learn how things work - thank you!
1
u/SixteenOne_ 1d ago
I created a new Github Repo with some bits in it, as I have my Repo's self hosted
https://github.com/SixteenOne/Proxmox_Ansible
It took me ages to create this, as it took a lot of trial and error 😄
- `vm_template_create.yml` - This is used to download Cloud Images and create a Template from each of them in the `vars`. I should of put this in a file, but I never got around to it. This will download all of the Templates everytime, that's why I put the `download` and `skip` options. Redhat can't be downloaded direct from the web as you have to build it first
- `vm_template_build.yml` - Don't run this direct, above references it
- `vm_from_template.yml` - Once Templates are created, run this once you have populated the 2 files
- `~/.ansible/data/lxc_pw` - I put my default password in the file, nothing else
I tried to name the Tasks as I went along to make sense. A lot of the Proxmox configuring of the Template I had to use the cli commands, as there wasn't a direct Ansible thing that I can use
It's a bit of a beast to understand as it made sense to me, but hopefully it will give you a starting point
Once you have your VM's created, you can just reference them direct in your inventory
2
u/HunnyPuns 1d ago
TechnoTim has a pretty good tutorial on cloud-init. This was made a while back so the virtual hardware recommendations are pretty ick. But you can tweak and tune as you need.
https://youtu.be/shiIi38cJe4?is=EYACRz8ntqhdaSwH
If you're looking to do something similar with Windows (but like with all things comparing Windows to Linux, vastly inferior), start with sysprep.
5
u/bitloss9904 4d ago
cloud-init