Trying to write a script to do some auto configuration for network devices using data from NetBox. I want the users to be able to input a list of sites and have the script check if those sites are valid. Code is this:
while True:
sites = [str(x) for x in input("Which site(s) need to be configured: ").split(', ')]
for site in sites:
if not nb.dcim.sites.filter(name=sites):
print(f"{site} not a valid site")
continue
else:
print(f"{site} is valid")
break
When running it, the code correctly loops if the site is not valid. But when a valid site is entered it still loops. See output below
» python sites.py
Which site(s) need to be configured: x
x not a valid site
Which site(s) need to be configured: x
x not a valid site
Which site(s) need to be configured: x
x not a valid site
Which site(s) need to be configured: SITE1
SITE1 is valid
Which site(s) need to be configured: SITE1
SITE1 is valid
I think the while True
needs to be something else perhaps?
or the break
is not working as expected