Skip to content

Print Diff for DCNM_Inventory Results, Fix for Issue #90 #465

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

Merged
merged 3 commits into from
Jul 15, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions plugins/modules/dcnm_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,7 @@ def __init__(self, module):
self.node_migration = False
self.nd_prefix = "/appcenter/cisco/ndfc/api/v1/lan-fabric"
self.switch_snos = []
self.diff_input_format = []

self.result = dict(changed=False, diff=[], response=[])

Expand Down Expand Up @@ -1811,6 +1812,120 @@ def failure(self, resp):

self.module.fail_json(msg=res)

def format_diff(self):
"""
Format the diff for inventory operations
"""
diff = []
create_list = []
preprovision_list = []
bootstrap_list = []
rma_list = []
delete_list = []

# Add created switches to the diff
for create in self.diff_create:
if create.get("switches") and len(create["switches"]) > 0:
switch = create["switches"][0]
item = {
"ip_address": switch.get("ipaddr"),
"serial_number": switch.get("serialNumber"),
"role": create.get("role"),
"platform": switch.get("platform"),
"version": switch.get("version"),
"hostname": switch.get("sysName"),
"preserve_config": create.get("preserveConfig", False)
}
create_list.append(item)

if create_list:
create_item = {
"action": "create",
"switches": create_list
}
diff.append(create_item)

# Add POAP switches to the diff
for poap in self.want_create_poap:
item = {
"ip_address": poap.get("ipAddress"),
"role": poap.get("role"),
"hostname": poap.get("hostname"),
"platform": poap.get("model"),
"version": poap.get("version")
}
if poap.get("serialNumber"):
item["serial_number"] = poap.get("serialNumber")
bootstrap_list.append(item)
if poap.get("preprovisionSerial"):
item["preprovision_serial"] = poap.get("preprovisionSerial")
preprovision_list.append(item)

if bootstrap_list:
diff.append({
"action": "bootstrap",
"bootstrap_switches": bootstrap_list
})
if preprovision_list:
diff.append({
"action": "preprovision",
"preprovision_switches": preprovision_list
})

# Add RMA switches to the diff
for rma in self.want_create_rma:
item = {
"ip_address": rma.get("ipAddress"),
"old_serial": rma.get("oldSerialNumber"),
"new_serial": rma.get("newSerialNumber"),
"platform": rma.get("model"),
"version": rma.get("version")
}
rma_list.append(item)

if rma_list:
diff.append({
"action": "rma",
"rma_switches": rma_list
})

# Add deleted switches to the diff
for serial in self.diff_delete:
ip_address = None
hostname = None
for have_c in self.have_create:
if have_c["switches"][0]["serialNumber"] == serial:
ip_address = have_c["switches"][0]["ipaddr"]
hostname = have_c["switches"][0]["sysName"]
role = have_c["switches"][0]["role"]
platform = have_c["switches"][0]["platform"]
version = have_c["switches"][0]["version"]
break

item = {
"serial_number": serial
}
if ip_address:
item["ip_address"] = ip_address
if hostname:
item["hostname"] = hostname
if role:
item["role"] = role
if platform:
item["platform"] = platform
if version:
item["version"] = version

delete_list.append(item)

if delete_list:
diff.append({
"action": "delete",
"switches": delete_list
})

self.diff_input_format = diff


def main():
"""main entry point for module execution"""
Expand Down Expand Up @@ -1943,6 +2058,8 @@ def main():
if module.params["deploy"]:
dcnm_inv.config_deploy()

dcnm_inv.format_diff()
dcnm_inv.result["diff"] = dcnm_inv.diff_input_format
module.exit_json(**dcnm_inv.result)


Expand Down