How to use the nornir.plugins.tasks.text.template_file function in nornir

To help you get started, we’ve selected a few nornir examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github nornir-automation / nornir / tests / plugins / tasks / text / test_template_file.py View on Github external
def test_template_file_error_broken_file(self, nornir):
        results = nornir.run(text.template_file, template="broken.j2", path=data_dir)
        processed = False
        for result in results.values():
            processed = True
            assert isinstance(result.exception, TemplateSyntaxError)
        assert processed
        nornir.data.reset_failed_hosts()
github nornir-automation / nornir / tests / plugins / tasks / text / test_template_file.py View on Github external
def test_template_file(self, nornir):
        result = nornir.run(text.template_file, template="simple.j2", path=data_dir)

        assert result
        for h, r in result.items():
            assert h in r.result
            if h == "host3.group_2":
                assert "my_var: comes_from_all" in r.result
            if h == "host4.group_2":
                assert "my_var: comes_from_host4.group_2" in r.result
github twin-bridges / nornir_course / class5 / exercises / exercise5 / exercise5c.py View on Github external
def render_configurations(task):
    bgp = task.run(
        task=text.template_file, template="bgp.j2", path="nxos/", **task.host
    )
    intf = task.run(
        task=text.template_file, template="routed_int.j2", path="nxos/", **task.host
    )
    task.host["bgp_config"] = bgp.result
    task.host["intf_config"] = intf.result
github twin-bridges / nornir_course / class6 / exercises / exercise2 / exercise2b_2c / exercise2c.py View on Github external
def render_configurations(task):
    try:
        task.run(task=text.template_file, template="loopback.j2", path=".", **task.host)
    except NornirSubTaskError:
        task.results.pop()
        msg = "Encountered Jinja2 error"
        return Result(
            changed=False,
            diff=None,
            result=msg,
            host=task.host,
            failed=False,
            exception=None,
        )
github twin-bridges / nornir_course / class5 / collateral / jinja2_render / nornir_jinja2.py View on Github external
def my_task(task):
    result = task.run(
        task=text.template_file, template="interfaces.j2", path=".", **task.host
    )
    print(result[0].result)
github twin-bridges / nornir_course / class5 / exercises / exercise5 / exercise5b.py View on Github external
def render_configurations(task):
    bgp = task.run(
        task=text.template_file, template="bgp.j2", path="nxos/", **task.host
    )
    intf = task.run(
        task=text.template_file, template="routed_int.j2", path="nxos/", **task.host
    )
    task.host["bgp_config"] = bgp.result
    task.host["intf_config"] = intf.result
github cldeluna / nornir-config / nornir_config_create.py View on Github external
"""
    Nornir Task

    :param task:
    :param arg:
    :return:
    """

    # Define the Jinja2 template file we will use to build our custom commands for each device
    j2template = 'vlan_updates.j2'

    # Generate a unique text file of commands for each device in our inventory
    filename = "cfg-{}.txt".format(task.host)


    task.host["rendered_cfg"] = task.run(task=template_file, template=j2template, path='', info=arg)

    with open(filename,"w") as cfg_file:
        cfg_file.write(str(task.host['rendered_cfg'][0]))

    print("\nCreated Configuration file {} for device {} in local directory...".format(task.host,filename))
github twin-bridges / nornir_course / bonus1 / collateral / netmiko_napalm / bgp_project.py View on Github external
def render_configs(task):
    """Generate BGP configs from Jinja2 template."""
    template = "bgp.j2"
    result = task.run(task=text.template_file, template=template, path=".", **task.host)
    rendered_config = result[0].result
    task.host["rendered_config"] = rendered_config

    if DEBUG:
        print()
        print("*" * 40)
        print(task.host.name)
        print("-" * 8)
        print(rendered_config)
        print("*" * 40)
        print()
github twin-bridges / nornir_course / bonus2 / exercises / bgp_config_tool_final.py View on Github external
Args:
        task: nornir task object

    """
    prefix_list_config = task.run(
        task=template_file, path="templates", template="prefix_lists.j2", **task.host
    )
    task.host["prefix_list_config"] = prefix_list_config.result

    route_map_config = task.run(
        task=template_file, path="templates", template="route_maps.j2", **task.host
    )
    task.host["route_map_config"] = route_map_config.result

    bgp_config = task.run(
        task=template_file, path="templates", template="bgp.j2", **task.host
    )
    task.host["bgp_router_config"] = bgp_config.result
github twin-bridges / nornir_course / class6 / exercises / exercise2 / exercise2b_2c / exercise2b.py View on Github external
def render_configurations(task):
    try:
        task.run(task=text.template_file, template="loopback.j2", path=".", **task.host)
    except NornirSubTaskError:
        return "Templating error occurred, but its okay...."