pynautobot icon indicating copy to clipboard operation
pynautobot copied to clipboard

setting primary IP failes when IP assigments are removed and re-assigned

Open veritas-sot opened this issue 8 months ago • 3 comments

Environment:

nautobot 2.2.5 pynautobot: 2.2.0

Details:

When I remove all existing IP assignmets of an interface, then re-assign all IP addresses to the interface and set the primary IP of the device, the primary IP is NOT set. Using the rest API works well. This could be similar to issue 197!

The follwing code does not work, it deletes the primary IP of the device

# get device, interface and IP
device = nautobot.dcim.devices.get(name='lab.local')
interface = nautobot.dcim.interfaces.get(device_id=device.id, name='Loopback0')
ip_address = nautobot.ipam.ip_addresses.get(address='192.168.2.1')

# get list of assigments
id_list = nautobot.ipam.ip_address_to_interface.filter(interface=interface.id, ip_address=ip_address.id)

# remove assignments
for assignment in id_list:
    assignment.delete()

# re-assign
assigned = nautobot.ipam.ip_address_to_interface.create({'interface': interface.id, 'ip_address': ip_address.id} )

# save
save = device.save()
# set primary
success = device.primary_ip4 = ip_address
#save
device.save()

When I add the following line to the code

device = nautobot.dcim.devices.get(name='lab.local')

right before setting the primary IP, it works.

I was asked to open this issue (https://networktocode.slack.com/archives/C01NWPK6WHL/p1718816339526429)

veritas-sot avatar Jun 24 '24 13:06 veritas-sot