I’ve been playing with Ansible lately as part of my RHCA studies, and found a nice way of using dynamic inventory to manage my Proxmox homelab.
Installation
On the Ansible control node, change to the folder where playbooks are stored:
$ cd /home/ansible/playbooks
Download the script from https://github.com/xezpeleta/Ansible-Proxmox-inventory.
$ git clone https://github.com/xezpeleta/Ansible-Proxmox-inventory.git
Make it executable:
$ chmod 0755 ./proxmox.py
Configuration
Try listing dynamic inventory:
$ ./proxmox.py \ --url=https://pve.lisenet.com:8006/ \ --username=root@pam \ --password=changeme \ --list --pretty { "all": { "hosts": [ "ansible5.hl.local", "proxy1.hl.local", "ansible2.hl.local", "rhel70-tmpl", "monitoring.hl.local", "storage1.hl.local", "admin2.hl.local", "katello.hl.local", "db1.hl.local", "web2.hl.local", "proxy2.hl.local", "ansible3.hl.local", "db2.hl.local", "ldap2.hl.local", "syslog.hl.local", "admin1.hl.local", "web1.hl.local", "ansible4.hl.local", "ldap1.hl.local", "ansible1.hl.local", "backup.hl.local", "storage2.hl.local" ] }, [...]
Create a file setenv.sh
with environment variables to store Proxmox credentials:
#!/bin/bash export PROXMOX_URL=https://pve.lisenet.com:8006/ export PROXMOX_USERNAME=root@pam export PROXMOX_PASSWORD=changeme
Execute commands from filename in the current shell environment:
$ source ./setenv.sh
List dynamic inventory without passing credentials on the CLI:
$ ./proxmox.py --list --pretty
Use inventory with Ansible:
$ ansible all -i ./proxmox.py -m ping -u ansible ansible5.hl.local | SUCCESS => { "changed": false, "ping": "pong" } proxy1.hl.local | SUCCESS => { "changed": false, "ping": "pong" } [...]
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.128.133|:443… connected.
HTTP request sent, awaiting response… 404 Not Found
2020-07-09 07:57:14 ERROR 404: Not Found.
Thanks, I’ve updated the link.
get error TypeError: the JSON object must be str, not ‘bytes’
./proxmox.py –list Traceback (most recent call last): File “./proxmox.py”, line 391, in main() File “./proxmox.py”, line 376, in main data = main_list(options, config_path) File “./proxmox.py”, line 248, in main_list proxmox_api.auth() File “./proxmox.py”, line 146, in auth validate_certs=self.options.validate)) File “/usr/lib/python3.5/json/__init__.py”, line 268, in load parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw) File “/usr/lib/python3.5/json/__init__.py”, line 312, in loads s.__class__.__name__)) TypeError: the JSON object must be str, not ‘bytes’
Python 3.5.3
does it work for you?
I keep getting back the wrong IP address for my VMs.
I eventually got it working by changing the proxmox.py at about lines 260-262 to this,
pattern = re.compile(“192”)
if pattern.search(ip_address[‘ip-address’]):
system_info.ip_address = ip_address[‘ip-address’]
the addresses on my network start with 192, so i want those ones.
someone with better Python skills might be able to do something better :-)