Skip to content
Merged
2 changes: 2 additions & 0 deletions changelogs/fragments/189-add-interfaces.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- virt - implement the gathering of Dom interface names and mac addresses as per FR https://github.com/ansible-collections/community.libvirt/issues/189
3 changes: 2 additions & 1 deletion plugins/doc_fragments/virt.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class ModuleDocFragment(object):
command:
description:
- In addition to state management, various non-idempotent commands are available.
choices: [ create, define, destroy, freemem, get_xml, info, list_vms, nodeinfo, pause, shutdown, start, status, stop, undefine, unpause, uuid, virttype ]
choices: [ create, define, destroy, freemem, get_xml, get_interfaces, info, list_vms, nodeinfo, pause, shutdown, start, status,
stop, undefine, unpause, uuid, virttype ]
type: str
"""

Expand Down
22 changes: 21 additions & 1 deletion plugins/modules/virt.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@
VIRT_UNAVAILABLE = 2

ALL_COMMANDS = []
VM_COMMANDS = ['create', 'define', 'destroy', 'get_xml', 'pause', 'shutdown', 'status', 'start', 'stop', 'undefine', 'unpause', 'uuid']
VM_COMMANDS = ['create', 'define', 'destroy', 'get_xml', 'get_interfaces', 'pause', 'shutdown', 'status', 'start', 'stop', 'undefine', 'unpause', 'uuid']
HOST_COMMANDS = ['freemem', 'info', 'list_vms', 'nodeinfo', 'virttype']
ALL_COMMANDS.extend(VM_COMMANDS)
ALL_COMMANDS.extend(HOST_COMMANDS)
Expand Down Expand Up @@ -315,6 +315,19 @@ def get_uuid(self, vmid):
vm = self.conn.lookupByName(vmid)
return vm.UUIDString()

def get_interfaces(self, vmid):
dom_xml = self.get_xml(vmid)
root = etree.fromstring(dom_xml)
interfaces = root.findall("./devices/interface")
interfaces_dict = {}
interfaces_dict['network_interfaces'] = {}
for interface in interfaces:
interfaces_dict["network_interfaces"].update(
{interface.find("source").get("bridge"):
{"mac": interface.find("mac").get("address"),
"pci_bus": interface.find("address").get("bus")}})
return interfaces_dict


class Virt(object):

Expand Down Expand Up @@ -490,6 +503,13 @@ def get_uuid(self, vmid):
self.__get_conn()
return self.conn.get_uuid(vmid)

def get_interfaces(self, vmid):
"""
Get Interface Name and Mac Address from xml
"""
self.__get_conn()
return self.conn.get_interfaces(vmid)


# A dict of interface types (found in their `type` attribute) to the
# corresponding "source" attribute name of their <source> elements
Expand Down