Skip to content

Containerlab Service Restart Task¤

task api name: restart_lab

The Containerlab service restart_lab task is designed to restart a specified lab. This task destroys the existing lab and redeploys it using the same topology file, ensuring a clean and consistent lab environment.

Warning

Restart task calls Containerlab deploy command with --reconfigure flag. Any non saved state will be lost. Use save task to save configuration as needed.

Containerlab Restart Task Overview¤

The restart_lab task provides the following features:

  • Lab Restart: Destroys the current lab and redeploys it using the original topology file.
  • Error Handling: Provides detailed error messages if the lab cannot be found or restarted.

Containerlab Restart Task Sample Usage¤

Below is an example of how to use the Containerlab restart task to restart a lab.

Example

Containerlab Restart Demo

nf#containerlab restart lab-name three-routers-lab
--------------------------------------------- Job Events -----------------------------------------------
05-May-2025 20:51:33.947 ee23b3ec4bfb474fac0a7e87e910862b job started
05-May-2025 20:51:34.011 INFO containerlab-worker-1 running containerlab.restart_lab  - 20:51:34 INFO Containerlab started version=0.67.0
05-May-2025 20:51:34.022 INFO containerlab-worker-1 running containerlab.restart_lab  - 20:51:34 INFO Parsing & checking topology file=three-routers-topology.yaml
05-May-2025 20:51:34.034 INFO containerlab-worker-1 running containerlab.restart_lab  - 20:51:34 INFO Destroying lab name=three-routers-lab
05-May-2025 20:51:35.527 INFO containerlab-worker-1 running containerlab.restart_lab  - 20:51:35 INFO Removed container name=clab-three-routers-lab-r2
05-May-2025 20:51:35.614 INFO containerlab-worker-1 running containerlab.restart_lab  - 20:51:35 INFO Removed container name=clab-three-routers-lab-r3
05-May-2025 20:51:35.659 INFO containerlab-worker-1 running containerlab.restart_lab  - 20:51:35 INFO Removed container name=clab-three-routers-lab-r1
05-May-2025 20:51:35.670 INFO containerlab-worker-1 running containerlab.restart_lab  - 20:51:35 INFO Removing host entries path=/etc/hosts
05-May-2025 20:51:35.681 INFO containerlab-worker-1 running containerlab.restart_lab  - 20:51:35 INFO Removing SSH config path=/etc/ssh/ssh_config.d/clab-three-routers-lab.conf
05-May-2025 20:51:35.790 INFO containerlab-worker-1 running containerlab.restart_lab  - 20:51:35 INFO Creating container name=r1
05-May-2025 20:51:35.813 INFO containerlab-worker-1 running containerlab.restart_lab  - 20:51:35 INFO Creating container name=r3
05-May-2025 20:51:36.402 INFO containerlab-worker-1 running containerlab.restart_lab  - 20:51:36 INFO Running postdeploy actions for Arista cEOS 'r3' node
05-May-2025 20:51:36.658 INFO containerlab-worker-1 running containerlab.restart_lab  - 20:51:36 INFO Created link: r3:eth3 ▪┄┄▪ r1:eth3
05-May-2025 20:51:36.669 INFO containerlab-worker-1 running containerlab.restart_lab  - 20:51:36 INFO Running postdeploy actions for Arista cEOS 'r1' node
05-May-2025 20:51:36.779 INFO containerlab-worker-1 running containerlab.restart_lab  - 20:51:36 INFO Created link: r1:eth1 ▪┄┄▪ r2:eth1
05-May-2025 20:51:36.821 INFO containerlab-worker-1 running containerlab.restart_lab  - 20:51:36 INFO Created link: r2:eth2 ▪┄┄▪ r3:eth2
05-May-2025 20:51:36.832 INFO containerlab-worker-1 running containerlab.restart_lab  - 20:51:36 INFO Running postdeploy actions for Arista cEOS 'r2' node
05-May-2025 20:52:03.895 INFO containerlab-worker-1 running containerlab.restart_lab  - 20:52:03 INFO Adding host entries path=/etc/hosts
05-May-2025 20:52:03.906 INFO containerlab-worker-1 running containerlab.restart_lab  - 20:52:03 INFO Adding SSH config for nodes path=/etc/ssh/ssh_config.d/clab-three-routers-lab.conf
05-May-2025 20:52:04.142 ee23b3ec4bfb474fac0a7e87e910862b job completed in 30.195 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="restart_lab",
        kwargs={
            "lab_name": "three-routers-lab",
        }
    )

    pprint.pprint(res)

    nf.destroy()

NORFAB Containerlab CLI Shell Reference¤

Below are the commands supported by the restart_lab task:

nf#man tree containerlab.restart
root
└── containerlab:    Containerlab service
    └── restart:    Restart lab devices
        ├── timeout:    Job timeout
        ├── workers:    Filter worker to target, default 'all'
        ├── verbose-result:    Control output details, default 'False'
        ├── lab-name:    Lab name to restart
        └── progress:    Display progress events, default 'True'
nf#

* - mandatory/required command argument

Python API Reference¤

Restart a specified Containerlab lab.

This method retrieves the lab details, destroys the existing lab, and redeploys it using the provided topology file.

Parameters:

Name Type Description Default
lab_name str

The name of the lab to restart.

required
timeout int

The timeout value for the operation in seconds. Defaults to None.

None

Returns:

Name Type Description
Result Result

An object containing the status of the operation, any errors encountered, and the result indicating whether the lab was successfully restarted.

Source code in norfab\workers\containerlab_worker.py
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
def restart_lab(self, lab_name: str, timeout: int = None) -> Result:
    """
    Restart a specified Containerlab lab.

    This method retrieves the lab details, destroys the existing lab, and redeploys it
    using the provided topology file.

    Args:
        lab_name (str): The name of the lab to restart.
        timeout (int, optional): The timeout value for the operation in seconds. Defaults to None.

    Returns:
        Result: An object containing the status of the operation, any errors encountered,
                and the result indicating whether the lab was successfully restarted.
    """
    ret = Result(task=f"{self.name}:restart_lab")

    # 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]

        # add needed env variables
        env = dict(os.environ)
        env["CLAB_VERSION_CHECK"] = "disable"

        # run destroy command
        args = [
            "containerlab",
            "deploy",
            "-f",
            "json",
            "-t",
            topology_file,
            "--reconfigure",
        ]
        ret = self.run_containerlab_command(
            args, cwd=topology_folder, timeout=timeout, ret=ret, env=env
        )

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

    return ret