QuestionQ102

VerifiedDevice-Level Network Automation

An engineer must enforce hostname compliance on a Cisco IOS XE router through Python over RESTCONF. The engineer must read the current hostname and, if it does not match, set the desired value with a RESTCONF PUT to the correct YANG leaf. Drag the code snippets into the boxes to build the artifact. Not all options are used.

Options may be used more than once.

Drag & Drop
import requests

requests.packages.urllib3.disable_warnings()

base = "https://192.0.2.10"
auth = ("netops", "Cisco12345")
headers = {"Accept": "application/yang-data+json", "Content-Type": "application/yang-data+json"}

get_url = f"{base}/restconf///"
r = requests.get(get_url, headers=headers, auth=auth, verify=False)
r.raise_for_status()
current = r.json().get("", "")
desired="BR-EDGE"

if current != desired:
    payload = {"Cisco-IOS-XE-native:hostname": desired}
    put_url = f"{base}/restconf///"
    p = requests.put(put_url, headers=headers, auth=auth, json=payload, verify=False)
    p.raise_for_status()
    print("Hostname updated.")
else:
    print("Hostname already compliant.")
Explanation

Cisco IOS XE RESTCONF exposes native configuration data at data/Cisco-IOS-XE-native:native; the hostname resource is the hostname leaf. Retrieving and updating that same leaf URI implements the compliance check and correction.

Learn more

Community Discussion

No comments yet. Be the first to start the discussion!