Skip to content

Implement gathering of Interface and Mac from powered off domain #190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Feb 15, 2025

Conversation

stratus-ss
Copy link
Contributor

@stratus-ss stratus-ss commented Jan 23, 2025

SUMMARY

There are use cases where you need to get the mac address of a domain in order to provide it to PXE, Sushy-tools or other related tools. There should be a way to get this information without powering on the machine

Fixes #189

ISSUE TYPE
  • Feature Pull Request
COMPONENT NAME

This is an addition to the plugin virt.py which allows the ansible module to process the xml generated by get_xml in order to return the interfaces and their associate mac addresses

This makes use of etree.fromstring to get the xml. A dictionary is returned with the primary key being network_interfaces with a nested dict containing the interface name as the key for an additional dict containing the mac address and the pci bus:

interface_dict = {"network_interfaces": {"bridge0": {"mac": "00:00:00:00", "pci_bus": "00x02"} } } 
ADDITIONAL INFORMATION

A simple playbook

---
- name: get network interfaces
  gather_facts: false
  hosts: localhost
  tasks:
  - name: register vm info
    community.libvirt.virt:
      command: get_interfaces
      name: "sno"
    register: vminfo

  - name: debug result
    debug: 
      msg: "{{ vminfo }}"

And the output:

PLAY [get UUID] ******************************************************************************************************

TASK [register vm info] **********************************************************************************************
ok: [localhost]

TASK [debug result] **************************************************************************************************
ok: [testhost] => {
    "msg": {
        "changed": false,
        "failed": false,
        "network_interfaces": {
            "interface_1": {
                "mac": "52:54:00:94:e4:a0",
                "pci_bus": "0x01",
                "source": "default",
                "type": "NAT"
            },
            "interface_2": {
                "mac": "52:54:00:94:e4:a1",
                "pci_bus": "0x02",
                "source": "default",
                "type": "NAT"
            }
        }
    }
}

@stratus-ss stratus-ss marked this pull request as draft January 24, 2025 00:40
@stratus-ss stratus-ss marked this pull request as ready for review January 24, 2025 01:25
Copy link
Contributor

@Andersson007 Andersson007 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stratus-ss thanks for the contribution!

@csmart do i understand it correctly that we can't run integration tests in containers for this collection?

@stratus-ss
Copy link
Contributor Author

@stratus-ss thanks for the contribution!

@csmart do i understand it correctly that we can't run integration tests in containers for this collection?

In my other PR, i uploaded a dockerfile that runs my integration tests. Does your question refer to running integration tests in your CI or whether its actually possible?

@stratus-ss
Copy link
Contributor Author

I added integration tests as well as fedora and ubuntu Dockerfiles for reference as to how I stood up the containers.

Tests run like this
Ubuntu:

ansible-test integration virt_interfaces   --docker "quay.io/sovens/ansible/integration-testing/libvirt-ubuntu:24.04"   --docker-privileged   --python 3.12

Fedora:

 ansible-test integration virt_interfaces   --docker "quay.io/sovens/ansible/integration-testing/libvirt-fedora:40"   --docker-privileged   --python 3.13 

@csmart
Copy link
Collaborator

csmart commented Feb 8, 2025

@stratus-ss thanks, this looks good! I'm wondering whether the module should return NIC information for all VMs, if name is left off? I can see a use case for getting all the networking information for a set of VMs, and then doing something with them all. Perhaps a list of dicts of something with the UUID and name?..

@csmart
Copy link
Collaborator

csmart commented Feb 8, 2025

@csmart do i understand it correctly that we can't run integration tests in containers for this collection?

@Andersson007 we can in principle, but I don't know if we can in the Azure infra... I might have to dig into them some more.

@stratus-ss
Copy link
Contributor Author

This has been updated to better handle multiple nics in the testing. We can simply extend the template and the test if we want to test for all interface types

@csmart csmart merged commit 6587071 into ansible-collections:main Feb 15, 2025
23 checks passed
@csmart
Copy link
Collaborator

csmart commented Feb 15, 2025

Thanks @stratus-ss !

@Andersson007
Copy link
Contributor

@stratus-ss @csmart thanks for the contribution!

dseeley pushed a commit to dseeley/community.libvirt that referenced this pull request Jun 13, 2025
# By Chris Smart (7) and others
# Via Chris Smart
* upstream/main: (21 commits)
  Update galaxy to next expected release
  Release 1.4.0 commit (ansible-collections#196)
  AZP: add 2.19, remove 2.16 (ansible-collections#195)
  Implement ansible module for management of libvirt volumes (ansible-collections#180)
  implement the removal of volumes when a domain is undefined (ansible-collections#191)
  Implement gathering of Interface and Mac from powered off domain (ansible-collections#190)
  Implement --check support for the libvirt.virt module (ansible-collections#183)
  updated doc fragment to add the uuid
  adding changelog fragment as per request
  Adding the ability to get the domuuid of a specified domain
  fix
  AZP matrix: remove stable-2.15 testing
  Update galaxy to next expected release
  Release 1.3.1 commit
  libvirt_lxc: Add configuration for libvirt_lxc_noseclabel (ansible-collections#182)
  AZP: update test matrix
  CI: add support for 2.19 (ansible-collections#179)
  README: remove old communication section (ansible-collections#178)
  README: Add Communication section with Forum information (ansible-collections#176)
  ci: update test container to 6.0.0
  ...
dseeley pushed a commit to dseeley/community.libvirt that referenced this pull request Jun 15, 2025
* Refactor of virt_volume.py to fix many issues

# By Chris Smart (7) and others
# Via Chris Smart
* upstream/main: (21 commits)
  Update galaxy to next expected release
  Release 1.4.0 commit (ansible-collections#196)
  AZP: add 2.19, remove 2.16 (ansible-collections#195)
  Implement ansible module for management of libvirt volumes (ansible-collections#180)
  implement the removal of volumes when a domain is undefined (ansible-collections#191)
  Implement gathering of Interface and Mac from powered off domain (ansible-collections#190)
  Implement --check support for the libvirt.virt module (ansible-collections#183)
  updated doc fragment to add the uuid
  adding changelog fragment as per request
  Adding the ability to get the domuuid of a specified domain
  fix
  AZP matrix: remove stable-2.15 testing
  Update galaxy to next expected release
  Release 1.3.1 commit
  libvirt_lxc: Add configuration for libvirt_lxc_noseclabel (ansible-collections#182)
  AZP: update test matrix
  CI: add support for 2.19 (ansible-collections#179)
  README: remove old communication section (ansible-collections#178)
  README: Add Communication section with Forum information (ansible-collections#176)
  ci: update test container to 6.0.0
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FR: Return Interfaces and Macs for Domains that are powered off
3 participants