Creating VMs via API using cloudinit config

WelshDev

New Member
Apr 12, 2023
2
0
1
Hi all,

I've been using Proxmox for a while now but only recently set out to automate some things.
Specifically, I'm interacting with the API to create VMs and I'm having some trouble with cloudinit.

What am I trying to achieve?
I'd like to have a VM template which has a bunch of stuff pre-installed e.g. rsync, git, nano, etc.
Using the API, I'd like to one-click fire up a new VM and give it a new randomly generated root password. It should also setup networking using an IP from my assigned IPV4 range.

Here's what I've done...
  • Created a new Ubuntu VM, updated it and installed a few things (git, rsync, nano, etc)
  • Shut that down and converted it into a template (VM ID: 102)
  • Via the API, I'm cloning the template and passing in a cloudinit username, password and some network config. Specifically, these are the params being sent to the API endpoint...
    • ciuser => root
    • cipassword => myrandompassword
    • ipconfig0 => ip=XX.XX.XX.XX/32,gw=XX.XX.XX.XX,ip6=dhcp
    • net0 => virtio=02:00:00:8a:53:b9,bridge=vmbr0,firewall=1
  • Start up the VM
The first problem
When I start the newly created VM, my new password doesn't work. In fact, the original password used on my Ubuntu template works.
It seems cloudinit hasn't worked

The second problem
Networking isn't working. In the "Hardware" tab in the "Network Device (net0)" entry I can see the value as set via the "net0" param above. Within the console if I do "ip addr show" this mac address is also shown. Furthermore, the IP address provided above is also shown against the "ens18" interface.

Furthermore, it looks like this isn't as fresh as I would have liked. The new VM has my command history from the template.
I suppose what I'm trying to achieve is a perfectly clean VM with a new root password and the network configured.

Any help would be greatly appreciated!
 
It sounds like you havent sufficiently cleaned (de-configured) your image to be a template. For example, some portions of cloud-init are only run once. You need to remove certain flags to have it run again. Obviously the fact that your history is there indicates you haven't pre-cleaned the image.

The search term you are looking for is "golden image". The process is not Proxmox specific, a good start is:
https://docs.openstack.org/image-guide/ubuntu-image.html
or https://maas.io/docs/how-to-customise-images

Good luck


Blockbridge : Ultra low latency all-NVME shared storage for Proxmox - https://www.blockbridge.com/proxmox
 

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!