Skip to content

Commit e8ac65e

Browse files
authored
dcnm_inventory: Device not reachable fixes (#444)
* Fix device not reachable scenarios * Fix comments * Handle remove case * Fix github actions issue * Add clarifying comments * Add clarifying comments
1 parent 1d6f11c commit e8ac65e

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

plugins/modules/dcnm_inventory.py

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -849,12 +849,30 @@ def get_diff_replace_delete(self):
849849

850850
diff_delete = []
851851

852+
def check_have_c_in_want_list(have_c):
853+
for want_c in self.want_create:
854+
if have_c["switches"][0]["ipaddr"] == want_c["switches"][0]["ipaddr"]:
855+
return True
856+
857+
return False
858+
852859
def have_in_want(have_c):
853860
match_found = False
861+
862+
# Check to see if have is in the want list and if not return match_found(False)
863+
if not check_have_c_in_want_list(have_c):
864+
return match_found
854865
for want_c in self.want_create:
855866
match = re.search(r"\S+\((\S+)\)", want_c["switches"][0]["deviceIndex"])
856867
if match is None:
857-
continue
868+
# If we get here this is typically because the device was pre-provisioned
869+
# and the regex expression above will not match in this case.
870+
# We need to make an additionl check using ipaddr to see if the device
871+
# is already part of the fabric.
872+
if have_c["switches"][0]["ipaddr"] == want_c["switches"][0]["ipaddr"]:
873+
match = re.search(r"\S+\((\S+)\)", have_c["switches"][0]["deviceIndex"])
874+
if match is None:
875+
return match_found
858876
want_serial_num = match.groups()[0]
859877
if have_c["switches"][0]["serialNumber"] == want_serial_num:
860878
if (
@@ -883,7 +901,6 @@ def have_in_want(have_c):
883901
def get_diff_delete(self):
884902

885903
diff_delete = []
886-
887904
if self.config:
888905
for want_c in self.want_create:
889906
for have_c in self.have_create:
@@ -905,8 +922,21 @@ def get_diff_merge(self):
905922
found = False
906923
match = re.search(r"\S+\((\S+)\)", want_c["switches"][0]["deviceIndex"])
907924
if match is None:
908-
msg = "Switch with IP {0} is not reachable or is not a valid IP".format(want_c["seedIP"])
909-
self.module.fail_json(msg=msg)
925+
# If we don't have a match that means one of the following:
926+
# (1) The device has not been discovered and is not reachable or the IP address is not a valid IP
927+
# (2) The device has been discovered and added to the fabric but is currently not reachable
928+
# due to it being pre-provisioned or some other reason.
929+
want_c_already_discovered = False
930+
for have_c in self.have_create:
931+
# Check the have list to see if the device has already been discovered and if so
932+
# don't error but use the have list to create the match.
933+
if have_c["switches"][0]["ipaddr"] == want_c["switches"][0]["ipaddr"]:
934+
want_c_already_discovered = True
935+
match = re.search(r"\S+\((\S+)\)", have_c["switches"][0]["deviceIndex"])
936+
# Device is not part of the fabric so return the error.
937+
if not want_c_already_discovered:
938+
msg = "Switch with IP {0} is not reachable or is not a valid IP".format(want_c["seedIP"])
939+
self.module.fail_json(msg=msg)
910940
serial_num = match.groups()[0]
911941
for have_c in self.have_create:
912942
if (

0 commit comments

Comments
 (0)