netbox-sync icon indicating copy to clipboard operation
netbox-sync copied to clipboard

Problem choosing primary IP for VirtualMachine when IP and interface changes

Open plevart opened this issue 3 years ago • 2 comments

Hi, I'm getting following warnings while synchronizing a VMWare VirtualMachine which was modified to change interface and IP:

2022-04-24 22:00:17,144 - WARNING: Current interface 'vNIC 1 (Marand (VLAN 144))
(aadc.marand.si)' for IP '172.30.111.102/24' and this one 'vNIC 1 (Virtualizacija (VLAN 111))
(supportpal.integralis.si)' are both enabled. IP assignment skipped because it is unclear which one is the
correct one!

While I would like to keep the old IP in "quarantine" for 30 days (marked with tag Netbox-synced: Orphaned), I would also like the change of primary IP of the VirtualMachine to be picked up immediately. Wouldn't it be better for Primary IP selection logic to ignore orphaned IPs in situations where they cause ambiguities?

plevart avatar Apr 25 '22 17:04 plevart

Hi, I'm sorry. I don't really understand what you mean with quarantine the IP. So the IP is not deleted?

Currently it seems the same IP iss assigned to two different VMs with active (enabled) interfaces. The script can't determine which own was desired to be the correct one.

bb-Ricardo avatar Apr 25 '22 18:04 bb-Ricardo

What I mean by "quarantine" is exactly that the old IP is not deleted yet when it disappears on the VMWare side. I have this setting:

prune_delay_in_days = 30

Oh and I should add that this old IP was mistakenly assigned to a new VirtualMachine a few days after it was taken down on the primary VirtualMachine. This was a mistake on the human side as NetBox was not consulted 1st. So the WARNING message above is not about the ambiguity between two IPs (and interfaces), one of them orphaned, still being assigned to the same VirtualMachine and consequently preventing the logic to select the primary IP for that VM? I thought it was just that and not the fact that the same old IP was reused on another VM prematurely.

So old IP (and interface) is kept assigned to the primary VirtualMachine, but it is marked with tag Netbox-synced: Orphaned. Because it is left in the NetBox, it is considered "reserved" or I call that "quarantined" for another 30 days before being finally released. That's a good thing since it should prevent immediate re-use and confusion on the side of clients that may still be using the old IP. So I'm asking whether it would be possible to disambiguate such situations by ignoring "orphaned" IPs when there is ambiguity.

plevart avatar Apr 26 '22 07:04 plevart

Hi, long time since I wrote last.

I'm finally getting around to fix some bugs again.

To understand your issue/workflow correctly:

  • you assign the Netbox-synced: Orphaned manually to an IP address which is meant to be released.
  • and the IP should be assigned to the new interface if it has the Netbox-synced: Orphane tag assigned?

bb-Ricardo avatar Dec 28 '22 23:12 bb-Ricardo

It was a long time ago, but If I remember correctly what I meant was that I do the following manually on the VMWare side:

  • remove Interface and IP address A from a virtual machine
  • add Interface and IP address B to the same virtual machine

What netbox-sync does to data in Netbox after those two changes in VMWare is the following:

  • interface and IP address A are marked with Netbox-synced: Orphaned
  • interface and IP address B are added to the virtual machine
  • the warning is logged and primary IP is not assigned to the virtual machine: ... "WARNING: Current interface 'vNIC 1 (Marand (VLAN 144)) (aadc.marand.si)' for IP '172.30.111.102/24' and this one 'vNIC 1 (Virtualizacija (VLAN 111)) (supportpal.integralis.si)' are both enabled. IP assignment skipped because it is unclear which one is the correct one!"

What should happen instead of warning is that netbox-sync chooses interface and address B as the new primary IP for the virtual machine and not even consider the Netbox-synced: Orphaned interface/IP address A as the candidate.

plevart avatar Dec 31 '22 09:12 plevart

Hi, I just pushed another commit which should fix exactly this use case. Can you check out the development branch and test it again? Thank you.

bb-Ricardo avatar Jan 12 '23 14:01 bb-Ricardo

Hey @plevart: just pushed another commit to development. Would you be able to pull development branch and test if this fixes your issue?

bb-Ricardo avatar Jan 23 '23 20:01 bb-Ricardo

Just give me a little time since currently I don't have a VM to play with (remove and add interfaces). I'll have to create one.

plevart avatar Jan 24 '23 07:01 plevart

Hi, sure, would be great if you could confirm the fix and then I could close this issue. Trying to release a new version soon.

Thank you

bb-Ricardo avatar Jan 24 '23 08:01 bb-Ricardo

any updates?

bb-Ricardo avatar Feb 14 '23 09:02 bb-Ricardo

Let me try it now...

plevart avatar Feb 14 '23 17:02 plevart

Here we go... I created new VM with a NIC in VLAN 144 and an IP address from that VLAN, then executed netbox-sync:

...
2023-02-14 18:10:33,911 - INFO: Query data from vCenter: 'vc1.marand.si'
2023-02-14 18:10:55,717 - INFO: Created new virtual machine object: testvm.marand.si
2023-02-14 18:10:55,718 - INFO: Created new virtual machine interface object: vNIC 1 (Marand (VLAN 144)) (testvm.marand.si)
2023-02-14 18:10:55,721 - INFO: Created new IP address object: 10.99.9.4/22
2023-02-14 18:10:58,500 - INFO: Parsing objects which were marked to be reevaluated
2023-02-14 18:10:58,505 - INFO: Virtual machine 'testvm.marand.si' attribute 'tags' changed from '[]' to '['NetBox-synced', 'Source: vc1']'
2023-02-14 18:10:58,509 - INFO: Virtual machine interface 'vNIC 1 (Marand (VLAN 144)) (testvm.marand.si)' attribute 'tags' changed from '[]' to '['NetBox-synced', 'Source: vc1']'
2023-02-14 18:10:58,515 - INFO: Ip address '10.99.9.4/22' attribute 'tags' changed from '[]' to '['NetBox-synced', 'Source: vc1']'
2023-02-14 18:10:58,560 - INFO: Updating changed data in NetBox
2023-02-14 18:10:58,676 - INFO: Creating new NetBox 'IP address' object '10.99.9.4/22' with data: {'tags': [{'name': 'NetBox-synced'}, {'name': 'Source: vc1'}], 'address': '10.99.9.4/22', 'tenant': 1, 'assigned_object_type': 'virtualization.vminterface'}
2023-02-14 18:10:58,754 - INFO: NetBox successfully created IP address object '10.99.9.4/22'.
2023-02-14 18:10:58,754 - INFO: Ip address '10.99.9.4/22' attribute 'assigned_object_id' changed from 'None' to 'vNIC 1 (Marand (VLAN 144)) (testvm.marand.si)'
2023-02-14 18:10:58,761 - INFO: Creating new NetBox 'virtual machine' object 'testvm.marand.si' with data: {'tags': [{'name': 'NetBox-synced'}, {'name': 'Source: vc1'}], 'name': 'testvm.marand.si', 'cluster': 1, 'status': 'active', 'memory': 4096, 'vcpus': 2, 'disk': 20, 'platform': 13, 'tenant': 1, 'role': 1}
2023-02-14 18:10:58,882 - INFO: NetBox successfully created virtual machine object 'testvm.marand.si'.
2023-02-14 18:10:58,882 - INFO: Virtual machine 'testvm.marand.si' attribute 'primary_ip4' changed from 'None' to '10.99.9.4/22'
2023-02-14 18:10:58,888 - INFO: Creating new NetBox 'virtual machine interface' object 'vNIC 1 (Marand (VLAN 144)) (testvm.marand.si)' with data: {'tags': [{'name': 'NetBox-synced'}, {'name': 'Source: vc1'}], 'name': 'vNIC 1 (Marand (VLAN 144))', 'virtual_machine': 171, 'mac_address': '00:50:56:A9:EA:C2', 'description': 'Network adapter 1 (VirtualVmxnet3) (vlan ID: 144)', 'enabled': True, 'mtu': 1500, 'mode': 'access', 'untagged_vlan': 4}
2023-02-14 18:10:58,998 - INFO: NetBox successfully created virtual machine interface object 'vNIC 1 (Marand (VLAN 144))'.
2023-02-14 18:10:59,005 - INFO: Updating NetBox 'IP address' object '10.99.9.4/22' with data: {'assigned_object_type': 'virtualization.vminterface', 'assigned_object_id': 181}
2023-02-14 18:10:59,088 - INFO: Updating NetBox 'virtual machine' object 'testvm.marand.si' with data: {'primary_ip4': 218}
2023-02-14 18:10:59,262 - INFO: Pruning orphaned data in NetBox
2023-02-14 18:10:59,274 - INFO: Completed NetBox Sync in 29 seconds

All well. Then I powered down VM, switched the NIC to VLAN 128 and booted up the VM which initialized some other IP from VLAN 128. Then I ran netbox-sync again...

...
2023-02-14 18:16:15,362 - INFO: Query data from vCenter: 'vc1.marand.si'
2023-02-14 18:16:31,565 - INFO: Virtual machine interface 'vNIC 1 (Integralis (VLAN 128)) (testvm.marand.si)' attribute 'name' changed from 'vNIC 1 (Marand (VLAN 144))' to 'vNIC 1 (Integralis (VLAN 128))'
2023-02-14 18:16:31,565 - INFO: Virtual machine interface 'vNIC 1 (Integralis (VLAN 128)) (testvm.marand.si)' attribute 'description' changed from 'Network adapter 1 (VirtualVmxnet3) (vlan ID: 144)' to 'Network adapter 1 (VirtualVmxnet3) (vlan ID: 128)'
2023-02-14 18:16:31,567 - INFO: Created new IP address object: 172.30.137.3/20
2023-02-14 18:16:31,569 - INFO: IP address is no longer assigned to vNIC 1 (Integralis (VLAN 128)) (testvm.marand.si) and therefore removed from this interface
2023-02-14 18:16:31,569 - INFO: Setting attribute 'primary_ip4' for 'testvm.marand.si' to None
2023-02-14 18:16:31,569 - INFO: Setting attribute 'assigned_object_id' for '10.99.9.4/22' to None
2023-02-14 18:16:31,569 - INFO: Setting attribute 'assigned_object_type' for '10.99.9.4/22' to None
2023-02-14 18:16:31,569 - INFO: Virtual machine interface 'vNIC 1 (Integralis (VLAN 128)) (testvm.marand.si)' attribute 'untagged_vlan' changed from '144 (Posta)' to '128 (Posta)'
2023-02-14 18:16:39,751 - INFO: Parsing objects which were marked to be reevaluated
2023-02-14 18:16:39,765 - INFO: Ip address '10.99.9.4/22' attribute 'tags' changed from '['NetBox-synced', 'Source: vc1']' to '['NetBox-synced', 'NetBox-synced: Orphaned', 'Source: vc1']'
2023-02-14 18:16:39,767 - INFO: Ip address '172.30.137.3/20' attribute 'tags' changed from '[]' to '['NetBox-synced', 'Source: vc1']'
2023-02-14 18:16:44,820 - INFO: Updating changed data in NetBox
2023-02-14 18:16:44,820 - INFO: Updating NetBox 'virtual machine' object 'testvm.marand.si' with data: {'primary_ip4': None}
2023-02-14 18:16:44,951 - INFO: Updating NetBox 'IP address' object '10.99.9.4/22' with data: {'assigned_object_id': None, 'assigned_object_type': None}
2023-02-14 18:16:45,035 - INFO: Updating NetBox 'IP address' object '10.99.9.4/22' with data: {'tags': [{'name': 'NetBox-synced: Orphaned'}, {'name': 'NetBox-synced'}, {'name': 'Source: vc1'}]}
2023-02-14 18:16:45,127 - INFO: Creating new NetBox 'IP address' object '172.30.137.3/20' with data: {'tags': [{'name': 'NetBox-synced'}, {'name': 'Source: vc1'}], 'address': '172.30.137.3/20', 'assigned_object_id': 181, 'tenant': 1, 'assigned_object_type': 'virtualization.vminterface'}
2023-02-14 18:16:45,202 - INFO: NetBox successfully created IP address object '172.30.137.3/20'.
2023-02-14 18:16:45,212 - INFO: Updating NetBox 'virtual machine interface' object 'vNIC 1 (Integralis (VLAN 128)) (testvm.marand.si)' with data: {'name': 'vNIC 1 (Integralis (VLAN 128))', 'description': 'Network adapter 1 (VirtualVmxnet3) (vlan ID: 128)', 'untagged_vlan': 3}
2023-02-14 18:16:45,358 - INFO: Pruning orphaned data in NetBox
2023-02-14 18:16:45,366 - INFO: Completed NetBox Sync in 34 seconds

All well this time too. Previous IP gets orphaned, new IP gets assigned to the VM and primary IP address of VM gets set correctly. As far as my problems are concerned, this version fixes them. Thanks for being patient with me.

Regards, Peter

plevart avatar Feb 14 '23 18:02 plevart

Ahh, great news. Thanks for the testing effort. Will close this issue.

bb-Ricardo avatar Feb 14 '23 19:02 bb-Ricardo