-
-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #156 from nornir-automation/napalm_kwargs
allow passing options to napalm getters
- Loading branch information
Showing
5 changed files
with
103 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,54 @@ | ||
import copy | ||
|
||
from nornir.core.task import Result | ||
|
||
|
||
def napalm_get(task, getters): | ||
def napalm_get(task, getters, getters_options=None, **kwargs): | ||
""" | ||
Gather information from network devices using napalm | ||
Arguments: | ||
getters (list of str): getters to use | ||
getters_options (dict of dicts): When passing multiple getters you | ||
pass a dictionary where the outer key is the getter name | ||
and the included dictionary represents the options to pass | ||
to the getter | ||
**kwargs: will be passed as they are to the getters | ||
Examples: | ||
Simple example:: | ||
> nr.run(task=napalm_get, | ||
> getters=["interfaces", "facts"]) | ||
Passing options using ``**kwargs``:: | ||
> nr.run(task=napalm_get, | ||
> getters=["config"], | ||
> retrieve="all") | ||
Passing options using ``getters_options``:: | ||
> nr.run(task=napalm_get, | ||
> getters=["config", "interfaces"], | ||
> getters_options={"config": {"retrieve": "all"}}) | ||
Returns: | ||
:obj:`nornir.core.task.Result`: | ||
* result (``dict``): dictionary with the result of the getter | ||
""" | ||
device = task.host.get_connection("napalm") | ||
getters_options = getters_options or {} | ||
|
||
if isinstance(getters, str): | ||
getters = [getters] | ||
|
||
result = {} | ||
for g in getters: | ||
options = copy.deepcopy(kwargs) | ||
options.update(getters_options.get(g, {})) | ||
getter = g if g.startswith("get_") else "get_{}".format(g) | ||
method = getattr(device, getter) | ||
result[g] = method() | ||
result[g] = method(**options) | ||
return Result(host=task.host, result=result) |
1 change: 1 addition & 0 deletions
1
...tasks/networking/mocked/napalm_get/test_napalm_getters_multiple_with_options/get_config.1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"candidate": "asdadasd"} |
16 changes: 16 additions & 0 deletions
16
.../tasks/networking/mocked/napalm_get/test_napalm_getters_multiple_with_options/get_facts.1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"fqdn": "localhost", | ||
"hostname": "localhost", | ||
"interface_list": [ | ||
"Ethernet1", | ||
"Ethernet2", | ||
"Ethernet3", | ||
"Ethernet4", | ||
"Management1" | ||
], | ||
"model": "vEOS", | ||
"os_version": "4.15.5M-3054042.4155M", | ||
"serial_number": "", | ||
"uptime": "...", | ||
"vendor": "Arista" | ||
} |
1 change: 1 addition & 0 deletions
1
...s/tasks/networking/mocked/napalm_get/test_napalm_getters_single_with_options/get_config.1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"candidate": "asdadasd"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters