-
Notifications
You must be signed in to change notification settings - Fork 214
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
[Bug]: Cannot assign existing IP to network interface #975
Comments
Hello, My 2cents here as I’m looking of kind of the same topic. |
I'm seeing exact same behaviour. My scenario is that sometimes I need to reserve bunch of IPs ahead of time to get network teams to update firewall rules (which can take days). Once firewall rules are in place I can then build VMs and I would like them to use reserved IPs. Based on below I would expect to be able to assign an existing IP to a VM if the state is set to present.
|
Issue SummaryThe current behavior of the Ansible netbox_ip_address module does not align with the NetBox API's expectations for assigning IP addresses. Specifically, the API uses assigned_object_type and assigned_object_id to identify the target of the IP assignment, whereas the Ansible module accepts assigned_object and interface, which are more descriptive names. Proposed SolutionTo align the Ansible module with the NetBox API, we need to convert the human-readable names provided by the user into the appropriate assigned_object_type and assigned_object_id values. This can be achieved by modifying the NetboxIpamModule.run() function in the netbox_ipam.py file. A psudo-code exemple for the fix to the issue: def run(self):
...
assigned_object = data.get("assigned_object")
if assigned_object:
# Determine the type and ID of the assigned object
interface_name = assigned_object.get("name")
if assigned_object.get("device"):
device_name = assigned_object.get("device")
device_type = DCIM_INTERFACE
elif assigned_object.get("virtual_machine"):
device_name = assigned_object.get("virtual_machine")
device_type = VIRTUALIZATION_VIRTUAL_MACHINE
else:
raise Exception("Invalid assigned_object parameters. Device or virtual machine must be specified.")
# Fetch the interface object based on the type and name
interface = netbox_object.get(device_type, device_name, interface_name)
if not interface:
raise Exception(f"No interface found with device '{device_name}' and name '{interface_name}'")
# Update the data dictionary with the required fields for the API
data.update({
"assigned_object_type": device_type,
"assigned_object_id": interface.id
})
# Process the 'interface' argument similarly
interface = data.get("interface")
if interface:
interface_name = interface.get("name")
device_name = data.get("device")
device_type = DCIM_INTERFACE
# Fetch the interface object based on the type and name
interface = netbox_object.get(device_type, device_name, interface_name)
if not interface:
raise Exception(f"No interface found with device '{device_name}' and name '{interface_name}'")
# Update the data dictionary
data.update({
"assigned_object_type": device_type,
"assigned_object_id": interface.id
})
...
# Update the NetBox instance with the modified data
update_netbox(data) Explanation
Footnotes |
BTW If anyone want's to take these changes and make a pull request, she/he is welcome to do so. |
So I made some more debugging and wanted to document what i found. first of all there is no problem with assigned_object, I started debugging it for real and saw it was working fine. |
Ansible NetBox Collection version
v3.12.0
Ansible version
NetBox version
v3.4.7
Python version
3.10
Steps to Reproduce
If an IP address already exists in our Netbox, it cannot be assigned to an interface. The module always instructs the Netbox to create a new IP. This results in a
Duplicate IP address
exception, whenEnforce unique space
is enabled, or in a duplicate IP.Expected Behavior
The existing IP should be modified and should be attached to the specified interface.
Observed Behavior
The VRF
my_company-RFC1918
uses the optionEnforce unique space
, that is why the assignment fails with aDuplicate IP address
, because the module tries to create a new IP instead of modifying the existing one:The text was updated successfully, but these errors were encountered: