mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
[PR #8471/2574cb0d backport][stable-9] feat: proxmox_vm_info - add network information for guests (#8505)
feat: proxmox_vm_info - add network information for guests (#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>
(cherry picked from commit 2574cb0dea
)
Co-authored-by: Jan Wenzel <jan@coffeelover.de>
This commit is contained in:
parent
7cac741e77
commit
fe3a3a7638
2 changed files with 26 additions and 9 deletions
2
changelogs/fragments/8471-proxmox-vm-info-network.yml
Normal file
2
changelogs/fragments/8471-proxmox-vm-info-network.yml
Normal file
|
@ -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).
|
|
@ -57,6 +57,13 @@ options:
|
|||
- 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
|
||||
|
@ -172,7 +179,7 @@ class ProxmoxVmInfoAnsible(ProxmoxAnsible):
|
|||
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 (
|
||||
|
@ -201,17 +208,23 @@ class ProxmoxVmInfoAnsible(ProxmoxAnsible):
|
|||
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)
|
||||
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue