Skip to content

Containerlab Service Save Task¤

task api name: save

The Containerlab service save task is used to save the configuration of devices in a specified lab. This task ensures that the current state of the lab is preserved, allowing users to back up configurations for future use or restoration.

Containerlab Save Task Overview¤

The save task provides the following features:

  • Configuration Backup: Saves the current configuration of all devices in the lab.
  • Lab-Specific: Operates on a specified lab, ensuring targeted configuration management.

Containerlab Save Task Sample Usage¤

Below is an example of how to use the Containerlab save task to back up a lab's configuration.

Example

Containerlab Save Demo

nf#containerlab save lab-name three-routers-lab
--------------------------------------------- Job Events -----------------------------------------------
05-May-2025 20:48:25.322 7ffd783a18ee4db7b92d1643ef8b3390 job started
05-May-2025 20:48:25.391 INFO containerlab-worker-1 running containerlab.save  - 20:48:25 INFO Parsing & checking topology file=three-routers-topology.yaml
05-May-2025 20:48:25.551 INFO containerlab-worker-1 running containerlab.save  - 20:48:25 INFO saved cEOS configuration from r2 node to
/home/norfabuser/norfab/tests/nf_containerlab/__norfab__/files/worker/containerlab-worker-1/topologies/containerlab/clab-three-routers-lab/r2/flash/startup-config
05-May-2025 20:48:25.905 INFO containerlab-worker-1 running containerlab.save  - 20:48:25 INFO saved cEOS configuration from r3 node to
/home/norfabuser/norfab/tests/nf_containerlab/__norfab__/files/worker/containerlab-worker-1/topologies/containerlab/clab-three-routers-lab/r3/flash/startup-config
05-May-2025 20:48:26.285 INFO containerlab-worker-1 running containerlab.save  - 20:48:26 INFO saved cEOS configuration from r1 node to
/home/norfabuser/norfab/tests/nf_containerlab/__norfab__/files/worker/containerlab-worker-1/topologies/containerlab/clab-three-routers-lab/r1/flash/startup-config
05-May-2025 20:48:26.510 7ffd783a18ee4db7b92d1643ef8b3390 job completed in 1.188 seconds

--------------------------------------------- Job Results --------------------------------------------

containerlab-worker-1:
    ----------
    three-routers-lab:
        True
nf#
import pprint

from norfab.core.nfapi import NorFab

if __name__ == '__main__':
    nf = NorFab(inventory="inventory.yaml")
    nf.start()

    client = nf.make_client()

    res = client.run_job(
        service="containerlab",
        task="save",
        kwargs={
            "lab_name": "three-routers-lab",
        }
    )

    pprint.pprint(res)

    nf.destroy()

NORFAB Containerlab CLI Shell Reference¤

Below are the commands supported by the save task:

nf# man tree containerlab.save
root
└── containerlab:    Containerlab service
    └── save:    Perform configuration save for all containers running in a lab
        ├── timeout:    Job timeout
        ├── workers:    Filter worker to target, default 'all'
        ├── verbose-result:    Control output details, default 'False'
        ├── lab-name:    Lab name to save configurations for
        └── progress:    Display progress events, default 'True'
nf#

* - mandatory/required command argument

Python API Reference¤

Saves the config of a specified lab devices by invoking the containerlab save command.

Parameters:

Name Type Description Default
lab_name str

The name of the lab to save.

required
timeout int

The maximum time in seconds to wait for the operation to complete. Defaults to None.

None

Returns:

Name Type Description
Result Result

An object containing the outcome of the save operation. If successful, result will contain a dictionary with the lab name as the key and True as the value. If unsuccessful, failed will be set to True, and errors will contain a list of error messages.

Source code in norfab\workers\containerlab_worker.py
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
def save(self, lab_name: str, timeout: int = None) -> Result:
    """
    Saves the config of a specified lab devices by invoking the `containerlab save` command.

    Args:
        lab_name (str): The name of the lab to save.
        timeout (int, optional): The maximum time in seconds to wait for the operation
            to complete. Defaults to None.

    Returns:
        Result: An object containing the outcome of the save operation. If successful,
            `result` will contain a dictionary with the lab name as the key and `True`
            as the value. If unsuccessful, `failed` will be set to True, and `errors`
            will contain a list of error messages.
    """
    ret = Result(task=f"{self.name}:save")

    # get lab details
    inspect = self.inspect(timeout=timeout, lab_name=lab_name, details=True)

    if not inspect.result:
        ret.failed = True
        ret.errors = [f"'{lab_name}' lab not found"]
        ret.result = {lab_name: False}
    else:
        topology_file = inspect.result[0]["Labels"]["clab-topo-file"]
        topology_folder = os.path.split(topology_file)[0]

        # run destroy command
        args = ["containerlab", "save", "-t", topology_file]
        ret = self.run_containerlab_command(
            args, cwd=topology_folder, timeout=timeout, ret=ret
        )

        if not ret.failed:
            ret.result = {lab_name: True}

    return ret