Tasks automation via API (maybe using Ansible?)

Oct 10, 2022
48
2
13
I am researching how to automate tasks in the containers/VMs, but I don't like the idea of connecting via SSH to each machine and depending on the remotes to be connected on the same network.

Is it possible to connect to the containers/VMs via Ansible using the Proxmox API? Maybe there is a better tool for this job?
 
Not sure about Ansible for VMs there is the POST /api2/json/nodes/{node}/qemu/{vmid}/agent/exec [1] endpoint. For containers you could use the CLI command pct execute command. As far as I know that does not have an API equivalent though as it just uses lxc-attach under the hood.

[1]: https://pve.proxmox.com/pve-docs/api-viewer/index.html#/nodes/{node}/qemu/{vmid}/agent/exec
 
@sterzy I am looking for some kind of automation that can launch commands on multiple CT/VMs, maybe under the hood using `pct exec` , `pct console`, and such.

@liberodark that's good for provisioning, but I am in need of something to submit commands to multiple machines already provisioned. Do you know how Ansible (or something else) could do it?
 
Hi, i think you can use rundeck directly for that also
Bash:
pct enter
or
Bash:
pct execute
can help you for that too.

Best Regards
 
Last edited:
  • Like
Reactions: Machine Manager
Hi,
Just for example i send you this piece of code to launch a command on your CTs :

Bash:
#!/bin/bash
# List VMs
VMID_VM=$(qm list| awk '/[0-9]/ {print $1}')
# List CTs
VMID_CT=$(pct list| awk '/[0-9]/ {print $1}')
# Run Command on CTs
pct exec "$VMID_CT" -- bash -c "touch test_$VMID_CT"

PS : For VMs I might recommend using cloudinit.

Best Regards
 
Last edited:

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!