Skip to content

Commit

Permalink
feat: proxmox_vm_info - add network information for guests (ansible-c…
Browse files Browse the repository at this point in the history
…ollections#8471)

* feat: add network information for guests

- Uses agent information for qemu-vms
- Uses network information for lxc container

* chore: add changelog fragment

* fix: change default, add doc

* chore: clarify doc

* chore: add optional ,

* chore: fix pep8 indentation warning

* Update plugins/modules/proxmox_vm_info.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update plugins/modules/proxmox_vm_info.py

Co-authored-by: Felix Fontein <felix@fontein.de>

---------

Co-authored-by: Jan Wenzel <jan.wenzel@gonicus.de>
Co-authored-by: Felix Fontein <felix@fontein.de>
  • Loading branch information
3 people authored and Massl123 committed Feb 7, 2025
1 parent 0e29c83 commit d1d09d5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/8471-proxmox-vm-info-network.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- proxmox_vm_info - add ``network`` option to retrieve current network information (https://github.com/ansible-collections/community.general/pull/8471).
33 changes: 24 additions & 9 deletions plugins/modules/proxmox_vm_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@
- pending
default: none
version_added: 8.1.0
network:
description:
- Whether to retrieve the current network status.
- Requires enabled/running qemu-guest-agent on qemu VMs.
type: bool
default: false
version_added: 9.1.0
extends_documentation_fragment:
- community.general.proxmox.actiongroup_proxmox
- community.general.proxmox.documentation
Expand Down Expand Up @@ -172,7 +179,7 @@ def get_vms_from_cluster_resources(self):
msg="Failed to retrieve VMs information from cluster resources: %s" % e
)

def get_vms_from_nodes(self, cluster_machines, type, vmid=None, name=None, node=None, config=None):
def get_vms_from_nodes(self, cluster_machines, type, vmid=None, name=None, node=None, config=None, network=False):
# Leave in dict only machines that user wants to know about
filtered_vms = {
vm: info for vm, info in cluster_machines.items() if not (
Expand Down Expand Up @@ -201,17 +208,23 @@ def get_vms_from_nodes(self, cluster_machines, type, vmid=None, name=None, node=
config_type = 0 if config == "pending" else 1
# GET /nodes/{node}/qemu/{vmid}/config current=[0/1]
desired_vm["config"] = call_vm_getter(this_vm_id).config().get(current=config_type)
if network:
if type == "qemu":
desired_vm["network"] = call_vm_getter(this_vm_id).agent("network-get-interfaces").get()['result']
elif type == "lxc":
desired_vm["network"] = call_vm_getter(this_vm_id).interfaces.get()

return filtered_vms

def get_qemu_vms(self, cluster_machines, vmid=None, name=None, node=None, config=None):
def get_qemu_vms(self, cluster_machines, vmid=None, name=None, node=None, config=None, network=False):
try:
return self.get_vms_from_nodes(cluster_machines, "qemu", vmid, name, node, config)
return self.get_vms_from_nodes(cluster_machines, "qemu", vmid, name, node, config, network)
except Exception as e:
self.module.fail_json(msg="Failed to retrieve QEMU VMs information: %s" % e)

def get_lxc_vms(self, cluster_machines, vmid=None, name=None, node=None, config=None):
def get_lxc_vms(self, cluster_machines, vmid=None, name=None, node=None, config=None, network=False):
try:
return self.get_vms_from_nodes(cluster_machines, "lxc", vmid, name, node, config)
return self.get_vms_from_nodes(cluster_machines, "lxc", vmid, name, node, config, network)
except Exception as e:
self.module.fail_json(msg="Failed to retrieve LXC VMs information: %s" % e)

Expand All @@ -229,6 +242,7 @@ def main():
type="str", choices=["none", "current", "pending"],
default="none", required=False
),
network=dict(type="bool", default=False, required=False),
)
module_args.update(vm_info_args)

Expand All @@ -245,6 +259,7 @@ def main():
vmid = module.params["vmid"]
name = module.params["name"]
config = module.params["config"]
network = module.params["network"]

result = dict(changed=False)

Expand All @@ -256,12 +271,12 @@ def main():
vms = {}

if type == "lxc":
vms = proxmox.get_lxc_vms(cluster_machines, vmid, name, node, config)
vms = proxmox.get_lxc_vms(cluster_machines, vmid, name, node, config, network)
elif type == "qemu":
vms = proxmox.get_qemu_vms(cluster_machines, vmid, name, node, config)
vms = proxmox.get_qemu_vms(cluster_machines, vmid, name, node, config, network)
else:
vms = proxmox.get_qemu_vms(cluster_machines, vmid, name, node, config)
vms.update(proxmox.get_lxc_vms(cluster_machines, vmid, name, node, config))
vms = proxmox.get_qemu_vms(cluster_machines, vmid, name, node, config, network)
vms.update(proxmox.get_lxc_vms(cluster_machines, vmid, name, node, config, network))

result["proxmox_vms"] = [info for vm, info in sorted(vms.items())]
module.exit_json(**result)
Expand Down

0 comments on commit d1d09d5

Please sign in to comment.