[python]Dissecting AggregatedResult

I felt it is worth to take some of my sleeping time to document how to dissect AggregatedResult object after a nornir task is executed.

napalm_get with getters=[“config”]

This are my user inputs:

Hostname of router: r1
IP address of router: 192.168.100.101
Username of 192.168.100.101: nwadmin
Password of 192.168.100.101:
Enable password of 192.168.100.101:

example:

with InitNornir(inventory={
    "plugin": "nornir.plugins.inventory.simple.SimpleInventory",
    "options": {
        "host_file": tmp_name
    }
}) as nr:
    result = nr.run(task=napalm_get,
                    getters=["config"],
                    retrieve="all")

After nornir task is executed an AggregatedResult object which looks like below:

AggregatedResult (napalm_get): {'r1': MultiResult: [Result: "napalm_get"]}

AggregatedResult is a dictionary like object, in this example there is only one host, in real situation there may be multiple hosts, to get to a specific host do this result["r1"], you will get the below:

MultiResult: [Result: "napalm_get"]

The MultiResult object is a list like object, so to refer to the actual result do this result["r1"][0], you will get a json object like this:
j1
But this is not an ideal information, so to move further I need the dictionary, do this result["r1"][0].result which has the same result with .result but the object is changed from json to dictionary hence you can access the keys recursively to get the actual configuration, hence do this result["r1"][0].result["config"]["startup"], however it may fail because python may interpret that you are trying to do a string dicing, so if you encounter this error TypeError: string indices must be integers use dict.get() then do this result["r1"][0].result["config"].get("startup")
you will get the full configuration of cisco which looks like this:
j2

To find out the failure status, do this result["r1"][0].failed, False means the status is success, True means the task has failed.

To find out the change status do this result["r1"][0].changed, the return value is a boolean True means device has changed configuration, False means no changes made to device.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s