Skip to content

Add primary_ip_interface option to nb_inventory#1555

Open
jorgenspange wants to merge 3 commits into
netbox-community:develfrom
jorgenspange:feature/1530/primary-ip-interface
Open

Add primary_ip_interface option to nb_inventory#1555
jorgenspange wants to merge 3 commits into
netbox-community:develfrom
jorgenspange:feature/1530/primary-ip-interface

Conversation

@jorgenspange

@jorgenspange jorgenspange commented Apr 25, 2026

Copy link
Copy Markdown

Summary

  • Adds a new primary_ip_interface boolean option to the nb_inventory plugin that exposes the interface name of the primary IP as a host var (primary_ip_interface)
  • When only this option is set, a targeted fetch queries only the primary IP IDs already present on the fetched devices/VMs — rather than all IP addresses in NetBox — keeping overhead minimal (~0.6s in testing)
  • When used alongside interfaces: true, dns_name: true, or ansible_host_dns_name: true, the existing full IP fetch is reused with no duplicate requests

Closes #1530

Test plan

  • Unit tests updated and passing (test_group_extractors)
  • Changelog fragment added (changelogs/fragments/1530-primary-ip-interface.yml)
  • Enable primary_ip_interface: true in an inventory config and verify the primary_ip_interface host var is populated with the correct interface name
  • Verify hosts without a primary IP, or whose primary IP is not assigned to an interface, have no primary_ip_interface var set
  • Verify no regression when primary_ip_interface: false (default)

Adds a new `primary_ip_interface` boolean option that exposes the
interface name of the primary IP as a host var, without requiring
`interfaces: true` which fetches all interfaces and is significantly
slower.

When only `primary_ip_interface` is set, a targeted fetch queries only
the primary IP IDs already present on the fetched devices/VMs, rather
than all IP addresses in NetBox.

Closes netbox-community#1530
@jorgenspange

Copy link
Copy Markdown
Author

@sc68cal Would you be able to take a look at this one, when you have got the time?

@sc68cal

sc68cal commented Apr 27, 2026

Copy link
Copy Markdown
Contributor

This seems like something that can already be done, via an nb_lookup query, for those that require it. Can you discuss why this should be used instead of a nb_lookup query?

@jorgenspange

Copy link
Copy Markdown
Author

I have to admit that I am not familiar with the nb_lookup query, where would I find the documentation for this?

@jorgenspange

Copy link
Copy Markdown
Author

I think I got it now, looking into the nb_lookup plugin. Using the lookup_plugin would require me to run this task for all my hosts while having it in the inventory plugin makes primary_ip_interface available as a native host var before any play runs, just like primary_ip4 already. This is also consistent with how dns_name is already handled in the inventory plugin.

@jorgenspange

Copy link
Copy Markdown
Author

I think I got it now, looking into the nb_lookup plugin. Using the lookup_plugin would require me to run this task for all my hosts while having it in the inventory plugin makes primary_ip_interface available as a native host var before any play runs, just like primary_ip4 already. This is also consistent with how dns_name is already handled in the inventory plugin.

Any thoughts on this @sc68cal?

@sc68cal

sc68cal commented May 8, 2026

Copy link
Copy Markdown
Contributor

My concern is that we already have a couple of different ways that people want to connect to devices, by using different settings in the netbox inventory plugin. I don't think we should continue to add more and more different ways. If you have some preferred way to connect to your device based on netbox data that is not primary_ip4 or dns_name, you should use the nb_query plugin with add_host or set host_vars from the inventory data. I don't think we should keep growing the complexity in nb_inventory for each different way that people want to connect to devices that deviates from primary_ip4

@jorgenspange

Copy link
Copy Markdown
Author

@sc68cal This is not used to connect to the device really, what I am mostly interested in is to retreive the primary interface name, so that I can populate this in different variables I have in my configuration.

@sc68cal

sc68cal commented May 15, 2026

Copy link
Copy Markdown
Contributor

what I am mostly interested in is to retreive the primary interface name, so that I can populate this in different variables I have in my configuration.

That's a usecase that the nb_query plugin is meant for.

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.

[Feature]: Retrieve primary_interface_name variable from netbox.netbox.nb_inventory plugin.

2 participants