Skip to content

Commit 86a9828

Browse files
committed
Add '--suffix' parameter to 'netlab collect'
The 'netlab collect' command usually saves configuration files with the '.cfg' suffix (which can then be used in 'netlab up --reload' process). However, when building a multi-platform solution, it might be helpful to save the configuration files with a platform-specific suffix, for example 'eos.j2'. The '--suffix' parameter allows you to replace the standard 'cfg' suffix with something else As a few 'fetch-config' task lists used hardcoded '.cfg' suffix, most of them were either fixed to use the 'cfg_suffix' variable or (even better) set 'ansible_net_config' fact instead of creating the output file directly.
1 parent c2d61ad commit 86a9828

File tree

6 files changed

+23
-22
lines changed

6 files changed

+23
-22
lines changed

docs/netlab/collect.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,36 @@
11
(netlab-collect)=
22
# Collecting Device Configurations
33

4-
**netlab collect** command uses Ansible *device facts* modules (or an equivalent list of Ansible tasks) to collect device configurations and store them in specified output directory.
4+
The **netlab collect** command collects device configurations using Ansible *device facts* modules (or an equivalent list of Ansible tasks). The configurations are stored in the specified output directory (default: *config*).
55

6-
A single configuration file in the output directory is created for most network devices; multiple files are collected from FRR and Cumulus VX.
6+
A single configuration file in the output directory is created for most network devices; multiple files are collected from FRR and Cumulus VX. The configuration files have a `.cfg` suffix unless you specify a different suffix (without the leading dot) with the `--suffix` parameter.
77

8-
After collecting the device configurations, you can save them in a tar archive with the `--tar` option and clean up the working directory with `--cleanup` option.
8+
After collecting the device configurations, you can save them in a tar archive with the `--tar` option and clean up the working directory with the `--cleanup` option.
99

1010
```{tip}
11-
* **netlab collect** command is just a thin wrapper around an Ansible playbook which uses Ansible inventory created by **netlab create** or **netlab up** command.
11+
* The **netlab collect** command is just a thin wrapper around an Ansible playbook which uses the Ansible inventory created by the **netlab create** or **netlab up** command.
1212
* When executed with the `-i` option, **‌netlab collect** switches to the lab directory to execute the Ansible playbook, but stores the results within the directory from which it was executed.
1313
```
1414

1515
## Usage
1616

1717
```text
18-
usage: netlab collect [-h] [-v] [-q] [-o [OUTPUT]] [--tar TAR] [--cleanup] [-i INSTANCE]
18+
usage: netlab collect [-h] [-v] [-q] [-o [OUTPUT]] [--suffix SUFFIX] [--tar TAR]
19+
[--cleanup] [-i INSTANCE]
1920
2021
Collect device configurations
2122
2223
options:
2324
-h, --help show this help message and exit
2425
-v, --verbose Verbose logging
2526
-q, --quiet Run Ansible playbook and tar with minimum output
26-
-o [OUTPUT], --output [OUTPUT]
27+
-o, --output [OUTPUT]
2728
Output directory (default: config)
29+
--suffix SUFFIX Configure file(s) suffix (default: cfg)
2830
--tar TAR Create configuration tarball
2931
--cleanup Clean up config directory and modified configuration file after
3032
creating tarball
31-
-i INSTANCE, --instance INSTANCE
33+
-i, --instance INSTANCE
3234
Specify lab instance to collect configuration from
3335
3436
All other arguments are passed directly to ansible-playbook

netsim/ansible/collect-configs.ansible

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
gather_facts: false
66
vars:
77
config_dir: "{{ lookup('env','PWD') }}/{{ target|default('config') }}"
8+
cfg_suffix: "{{ suffix|default('cfg') }}"
89
node_provider: "{{ provider|default(netlab_provider) }}"
910
tasks:
1011
- name: Set variables that cannot be set with VARS
@@ -37,6 +38,6 @@
3738
- name: Copy collected configuration to '{{ config_dir }}' directory
3839
copy:
3940
content: "{{ ansible_net_config }}"
40-
dest: "{{ config_dir }}/{{ inventory_hostname }}.cfg"
41+
dest: "{{ config_dir }}/{{ inventory_hostname }}.{{ cfg_suffix }}"
4142
delegate_to: localhost
4243
when: ansible_net_config is defined

netsim/ansible/tasks/fetch-config/cumulus.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
register: config
99
- name: Save FRR configuration to {{ ofile }}
1010
vars:
11-
ofile: "{{ config_dir }}/{{ inventory_hostname }}.cfg"
11+
ofile: "{{ config_dir }}/{{ inventory_hostname }}.{{ cfg_suffix }}"
1212
copy:
1313
content: |
1414
{{ config.stdout }}

netsim/ansible/tasks/fetch-config/frr.yml

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,7 @@
88
command: cat /etc/frr/frr.conf
99
become: true
1010
register: config
11-
- name: Save FRR configuration to {{ ofile }}
12-
vars:
13-
ofile: "{{ config_dir }}/{{ inventory_hostname }}.cfg"
14-
copy:
15-
content: |
16-
{{ config.stdout }}
17-
dest: "{{ ofile }}"
18-
delegate_to: localhost
11+
- set_fact: ansible_net_config={{ config.stdout }}
1912

2013
- name: Collect FRR daemons configuration
2114
command: cat /etc/frr/daemons

netsim/ansible/tasks/fetch-config/srlinux.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,4 @@
1212
datastore: running
1313
register: get_result
1414

15-
- name: Save fetched config
16-
delegate_to: localhost
17-
ansible.builtin.copy:
18-
content: "{{ get_result.result[0] | to_nice_json }}"
19-
dest: "{{ config_dir }}/{{ inventory_hostname }}.cfg"
15+
- set_fact: ansible_net_config={{ get_result.result[0] | to_nice_json }}

netsim/cli/collect.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ def collect_parse(args: typing.List[str]) -> typing.Tuple[argparse.Namespace, ty
4040
nargs='?',
4141
default='config',
4242
help='Output directory (default: config)')
43+
parser.add_argument(
44+
'--suffix',
45+
dest='suffix',
46+
action='store',
47+
default='cfg',
48+
help='Configure file(s) suffix (default: cfg)')
4349
parser.add_argument(
4450
'--tar',
4551
dest='tar',
@@ -78,6 +84,9 @@ def run(cli_args: typing.List[str]) -> None:
7884

7985
rest = ['-e','target='+args.output ] + rest
8086

87+
if args.suffix:
88+
rest += ['-e','suffix='+args.suffix]
89+
8190
if args.tar and not args.quiet:
8291
external_commands.print_step(1,"Collecting device configurations")
8392

0 commit comments

Comments
 (0)