Skip to content

Commit d2aac14

Browse files
updated save_running_config example
1 parent 7b2a917 commit d2aac14

File tree

3 files changed

+88
-5
lines changed

3 files changed

+88
-5
lines changed

save_running_config_to_file/README.md

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,27 @@
1+
## Important Information
2+
Minimum version requirements:
3+
4+
- v20.8 pyATS
5+
- v20.8 Genie
6+
- v20.8.3b0 genie.libs.sdk
7+
- v20.8.2b2 genie.libs.clean
8+
9+
If the versions of your packages do not meet the above minimums, upgrade using the steps below:
10+
11+
# First upgrade the pyats installation to 20.8
12+
pip install --upgrade pyats[full]
13+
14+
# Next upgrade genie.libs.sdk and genie.libs.clean to the beta
15+
pip install --upgrade --pre genie.libs.sdk genie.libs.clean
16+
17+
118
## Getting Started
219
1. Clone this repository into `<path/to/pyATS>/`
320

421
2. Create a [testbed datafile](https://pubhub.devnetcloud.com/media/pyats/docs/topology/schema.html#production-yaml-schema)
5-
that contains devices in which a backup is required.
22+
that contains devices in which a backup is required.
23+
24+
**note:** One device must have the alias 'uut' (does not matter which one).
625

726
3. Run the script which will, connect to all devices in the testbed datafile, gather the running configurations,
827
and save that to a file named `<path/to/pyATS>/solutions_examples/save_running_config_to_file/config_backup_<dev_name>.txt`
@@ -12,8 +31,10 @@ and save that to a file named `<path/to/pyATS>/solutions_examples/save_running_c
1231
1332
# Execute the script
1433
pyats run job job.py --testbed-file <path/to/testbed.yaml>
34+
35+
**note**: For this example only ios, iosxe, iosxr, and nxos device are supported. Any other devices will be skipped.
1536

16-
4. If everything worked as expected the report at the end should be `PASSED` for every step:
37+
4. If everything worked as expected, the report at the end should be `PASSED` for every step:
1738

1839
2020-09-03T10:25:21: %EASYPY-INFO: +------------------------------------------------------------------------------+
1940
2020-09-03T10:25:21: %EASYPY-INFO: | Task Result Details |
@@ -35,7 +56,31 @@ and save that to a file named `<path/to/pyATS>/solutions_examples/save_running_c
3556
Now we have a text file containing a backup of the running configuration from all devices defined in the testbed datafile.
3657
One idea is using [pyATS Clean](https://pubhub.devnetcloud.com/media/genie-docs/docs/clean/index.html)
3758
to restore our configurations on a later date. This can be helpful if your network was working one day and not another.
38-
Simply replace to current configs with the backed up configs!
59+
Simply replace the current configs with the backed up configs!
60+
61+
1. Edit the `clean.yaml` template provided in `<path/to/pyATS>/solutions_examples/save_running_config_to_file/datafiles`
62+
to include your devices and the path to the config_backup_<dev_name>.txt file. You can add as many devices that are in your testbed file.
63+
64+
2. Run clean to restore your configs.
65+
66+
# Navigate to cloned repository
67+
cd <path/to/pyATS>/solutions_examples/save_running_config_to_file
68+
69+
# Run clean
70+
kleenex -testbed_file <path/to/testbed.yaml> -clean_file datafiles/clean.yaml
71+
72+
3. If everything worked successfully the report at the end should be `Passed` for every step:
73+
74+
2020-09-05T08:18:10: %AETEST-INFO: +----------------------------------------------------------+
75+
2020-09-05T08:18:10: %AETEST-INFO: | STEPS Report |
76+
2020-09-05T08:18:10: %AETEST-INFO: +----------------------------------------------------------+
77+
2020-09-05T08:18:10: %AETEST-INFO: STEP 1 - Apply configuration to device <dev> after reload Passed
78+
2020-09-05T08:18:10: %AETEST-INFO: STEP 2 - Copy running-config to startup-config on device <dev> Passed
79+
2020-09-05T08:18:10: %AETEST-INFO: STEP 3 - Allow configuration to stabilize on device <dev> Passed
80+
2020-09-05T08:18:10: %AETEST-INFO: ------------------------------------------------------------
81+
2020-09-05T08:18:10: %AETEST-INFO: The result of section apply_configuration is => PASSED
82+
2020-09-05T08:18:10: %KLEENEX-INFO: Finished cleaning device '<dev>' using 'DeviceClean'.
83+
2020-09-05T08:18:11: %ROOT-INFO: Clean finished!
3984

4085
[pyATS Clean](https://pubhub.devnetcloud.com/media/genie-docs/docs/clean/index.html) has many other features worth looking into.
4186
It can load new images onto various devices, recover devices from rommon prompt, and much much more.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
cleaners:
2+
DeviceClean:
3+
module: genie.libs.clean
4+
# List of devices to clean goes here
5+
devices: [<device_name>]
6+
7+
devices:
8+
# The device name
9+
<device_name>:
10+
11+
# This is the connect stage
12+
connect:
13+
14+
# This is the apply_configuration stage
15+
apply_configuration:
16+
configuration_from_file: </path/to/config_backup_<dev>.txt>
17+
config_timeout: 120
18+
19+
# The order we want to run the stages in
20+
order:
21+
- 'connect'
22+
- 'apply_configuration'
23+

save_running_config_to_file/triggers/triggers.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,19 @@ def saving_config(self, steps, testbed, exclude_config_that_matches_regex):
2929
"configuration.\nError: {}".format(str(e)))
3030

3131
step.passed("Gathered the running configuration for: {}".format(
32-
[name for name, _ in configurations]))
32+
[name for name, config in configurations if isinstance(config, str)]))
3333

3434
with steps.start("Saving all running configurations that were "
3535
"gathered"):
3636

3737
for dev, config in configurations:
38+
39+
# Skip any devices where the api to gather the running
40+
# config does not exist
41+
if isinstance(config, AttributeError):
42+
log.warning(config)
43+
continue
44+
3845
filename = 'config_backup_{}.txt'.format(dev)
3946

4047
with open(filename, 'w') as f:
@@ -54,13 +61,21 @@ def get_config(dev, exclude):
5461
5562
Returns:
5663
(device name ('str'), configuration ('str'))
64+
65+
if api not found:
66+
(device name ('str'), exception ('exception'))
5767
"""
5868

5969
exclude = exclude.get(dev.os)
6070

6171
log.info("The following regex is used to exclude lines of "
6272
"configuration for '{}' devices: {}"
6373
.format(dev.os, exclude))
74+
try:
75+
out = dev.api.get_valid_config_from_running_config(exclude=exclude)
76+
except AttributeError:
77+
return dev.name, AttributeError("Cannot get the config from '{}'. "
78+
"The API is not developed for '{}'"
79+
.format(dev.name, dev.os))
6480

65-
out = dev.api.get_valid_config_from_running_config(exclude=exclude)
6681
return dev.name, out

0 commit comments

Comments
 (0)