You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Because of some extensive work (ansible-collections/community.general#8424) by @mietzen, the ansible community.general collection1 now supports managing Proxmox LXCs using PCT over SSH (community.general.proxmox_pct_remote2).
This enables management of Proxmox containers without the need to setup SSH access to the containers themselves.
Below I will show you how to set this up, and give an example of how to use it. Hope this will be useful to you!
Prerequisites
Before we begin, ensure you have the following:
A Proxmox server with LXC containers.
Ansible installed on your local machine.
SSH access to your Proxmox server.
Step-by-Step Guide
Step 1: Install/update the community.general collection
The proxmox_pct_remote connection module is part of the community.general collection from version 10.3.0 onwards. You can install/update it using the following command:
It is possible to create a static or dynamic inventory. Below is an example of how to create a dynamic inventory using proxmox.inventory3 (for a static example see2)):
it is also possible to use environment variables or API tokens for the password, see the documentation3.
the keyed_groups groups proxmox LXCs/VMs by their tags, so for example, all containers tagged with 'docker' will be in the ansible hosts group proxmox_tags_docker
the compose section is key here, we can use it to add some default variables to the hosts in this inventory. In this case we specify that we want to use the community.general.proxmox_pct_remote connection plugin over SSH to the Proxmox host (192.168.2.3 in my case). Note the double quotes in the compose section, this is because these are Jinja expressions IIRC.
Test the inventory and community.general.proxmox_pct_remote
You can test the inventory by running the following command:
ansible-inventory --list
To test the community.general.proxmox_pct_remote connection plugin, you can run the following command (proxmox_all_running is a group that is created by the inventory plugin):
ansible proxmox_all_running -m ping
(Optional) Speeding it up
By default, the dynamic inventory will be recreated every time you run a playbook. This can be slow if you have many containers. To speed it up, you can cache the inventory, by adding the following to your ansible.cfg:
In addition, the PCT over SSH it quite slow, because the paramiko ssh plugin (used by this plugin) doesn't support persistent connections. You can use parallel execution to speed it up a bit, by adding the following to your ansible.cfg:
[defaults]forks = 8 # Number of parallel processes to use
Also gathering facts can be slow, you can disable it by setting gather_facts: false in your playbook. (it can even crash ansible if you have many containers, because many devices are shared between containers, in that case you can use the gather_subset option to limit the facts gathered, for example gather_subset: ["!devices"]).
Example use
You can now use the inventory to run regular playbooks on your Proxmox containers. For example, I use the following playbook (based on https://docs.portainer.io/start/upgrade/docker) to update portainer and the portainer_agent container to 2.27.1 on all my LXCs I tagged in proxmox with docker:
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Introduction
Because of some extensive work (ansible-collections/community.general#8424) by @mietzen, the ansible
community.general
collection1 now supports managing Proxmox LXCs using PCT over SSH (community.general.proxmox_pct_remote
2).This enables management of Proxmox containers without the need to setup SSH access to the containers themselves.
Below I will show you how to set this up, and give an example of how to use it. Hope this will be useful to you!
Prerequisites
Before we begin, ensure you have the following:
Step-by-Step Guide
Step 1: Install/update the
community.general
collectionThe
proxmox_pct_remote
connection module is part of thecommunity.general
collection from version 10.3.0 onwards. You can install/update it using the following command:Step 2: Create an inventory of your Proxmox LXCs
It is possible to create a static or dynamic inventory. Below is an example of how to create a dynamic inventory using
proxmox.inventory
3 (for a static example see2)):Some notes:
keyed_groups
groups proxmox LXCs/VMs by their tags, so for example, all containers tagged with 'docker' will be in the ansible hosts groupproxmox_tags_docker
compose
section is key here, we can use it to add some default variables to the hosts in this inventory. In this case we specify that we want to use thecommunity.general.proxmox_pct_remote
connection plugin over SSH to the Proxmox host (192.168.2.3 in my case). Note the double quotes in the compose section, this is because these are Jinja expressions IIRC.Test the inventory and
community.general.proxmox_pct_remote
You can test the inventory by running the following command:
To test the
community.general.proxmox_pct_remote
connection plugin, you can run the following command (proxmox_all_running
is a group that is created by the inventory plugin):(Optional) Speeding it up
By default, the dynamic inventory will be recreated every time you run a playbook. This can be slow if you have many containers. To speed it up, you can cache the inventory, by adding the following to your
ansible.cfg
:In addition, the PCT over SSH it quite slow, because the paramiko ssh plugin (used by this plugin) doesn't support persistent connections. You can use parallel execution to speed it up a bit, by adding the following to your
ansible.cfg
:Also gathering facts can be slow, you can disable it by setting
gather_facts: false
in your playbook. (it can even crash ansible if you have many containers, because many devices are shared between containers, in that case you can use thegather_subset
option to limit the facts gathered, for examplegather_subset: ["!devices"]
).Example use
You can now use the inventory to run regular playbooks on your Proxmox containers. For example, I use the following playbook (based on https://docs.portainer.io/start/upgrade/docker) to update portainer and the
portainer_agent
container to 2.27.1 on all my LXCs I tagged in proxmox withdocker
:Enjoy!
Footnotes
https://galaxy.ansible.com/ui/repo/published/community/general/ ↩
https://docs.ansible.com/ansible/latest/collections/community/general/proxmox_pct_remote_connection.html ↩ ↩2
https://docs.ansible.com/ansible/latest/collections/community/general/proxmox_inventory.html ↩ ↩2
Beta Was this translation helpful? Give feedback.
All reactions