[SOLVED] Setting LXC user password with pct when calling from a script

Thane_DE

New Member
Oct 19, 2017
6
0
1
27
Hello everyone,

I am looking for a non-interactive way to set the password for the root user (or any user really) when creating a LXC container from the official Proxmox Ubuntu 16.04 template using pct. For some background: I am currently trying to automate most of my homelab, including LXC container deployment, by using Ansible. This means creating containers through pct instead of through the GUI

I was planning on calling the pct command from an Ansible playbook, which would then create a container, the root storage and adjust all other parameters according to the input I fed the playbook, including the root user password. However, while pct does have a --password parameter, that one asks for password interactively, something that does not play nice with a script. So instead, I am looking for a way to set the password non-interactively, ideally a command-line parameter from pct or something else that can be modified by ansible.

It looks like this used to be possible with OpenVZ containers (blog.droidzone.in/2013/08/13/change-a-containers-root-password-in-proxmox/) , but since those are a thing of the past that command does not do anything, unfortunatley.

For reference, my current pct call looks a little something like this:

Code:
pct create 118 local:vztmpl/ubuntu-16.04-standard_16.04-1_amd64.tar.gz --hostname NewContainer --storage local-lvm --ostype ubuntu --arch amd64 --unprivileged 1 --password

Again, I'd like to avoid --password, because doing interactive inputs in a non-interactive script is never pretty. Not setting a password at all does not seem to work either, given that I cannot log in into the LXC container if I remove the --password parameter.

Any help would be highly appreciated!

Cheers!
 
you have the following options
  • use the API instead of pct
  • use SSH keys instead of passwords
  • neither set SSH key nor password at create time, but setup afterwards with 'pct exec' (to run a command) or 'pct push' (to just push a file into the container)
 
Thanks for the quick reply

I didn't even realize that Proxmox had an API for creating LXC containers - neat. I think I will stick with pct for now though.

It looks like 'pct exec' does exactly what I need, calling something like chpasswd (or adding a non-root user directly) should do the trick. The documentation (https://pve.proxmox.com/pve-docs/pct.1.html) mentions adding extra parameters in an array, I'm assuming that this is just a regular bash array?

Either way, thanks for the help, highly appreciate it!

Best Regards
 
you can just do the usual "pct exec ID -- foocmd --bar-option --something argument1"
 
Perfect, thanks for the help!

Silent Update (1/2018): Ansible has a proxmox module that makes all of the above unnecessary. I'd recommend using that instead - or just use the VE API
 
Last edited: