def InitNornir( config_file: str = "", dry_run: bool = False, **kwargs: Any, ) -> Nornir: """ Arguments: config_file(str): Path to the configuration file (optional) dry_run(bool): Whether to simulate changes or not configure_logging: Whether to configure logging or not. This argument is being deprecated. Please use logging.enabled parameter in the configuration instead. **kwargs: Extra information to pass to the :obj:`nornir.core.configuration.Config` object Returns: :obj:`nornir.core.Nornir`: fully instantiated and configured """ ConnectionPluginRegister.auto_register() if config_file: config = Config.from_file(config_file, **kwargs) else: config = Config.from_dict(**kwargs) data = GlobalState(dry_run=dry_run) config.logging.configure() return Nornir( inventory=load_inventory(config), runner=load_runner(config), config=config, data=data, )
def cli(ctx, pg_bar, print_result, print_stat, *args, **kwargs): ConnectionPluginRegister.auto_register() # 'None' = None kwargs.update({k: v for k, v in zip(kwargs, _json_loads(kwargs.values()))}) current_func_params = next(ctx.obj["queue_functions_generator"]) # try to pass not all arguments, but only the necessary ones if kwargs == list(current_func_params.values())[0]: function, parameters = list(current_func_params.items())[0] else: param_iterator = iter(current_func_params.values()) params = list(next(param_iterator).items()) function, parameters = list(current_func_params)[0], { key: value for key, value in kwargs.items() if (key, value) not in params } # get the current Nornir object from Commands chain or from temp.pkl file try: nr = ctx.obj["nornir"] except KeyError: nr = _pickle_to_hidden_file("temp.pkl", mode="rb", dump=False) nr.config.logging.configure() # pg_bar option if pg_bar: with tqdm( total=len(nr.inventory.hosts), desc="processing: ", ) as pb: result = nr.run( task=multiple_progress_bar, method=function, pg_bar=pb, **parameters, ) print() else: result = nr.run( task=function, **parameters, ) ctx.obj["result"] = result # print_result option if print_result: pr(result) print() # print statistic if print_stat: ps(nr, result)
def _get_trusted_untrusted(task): ConnectionPluginRegister.auto_register() # Get parameters in format: # [ { 'description': 'NNI', # 'mac_address': 'xxxx-yyyy-zzzz', # 'mtu': '', # 'name': 'Ethernet0/0/1'},] intfs = task.run( task=netmiko_send_command, name="interfaces list", command_string="disp int", use_textfsm=True, textfsm_template=os.path.join( os.getcwd(), "nornir_cli/custom_commands/templates/disp_int.template"), ) # Get trusted interfaces task.host["trusted"] = [ i["name"] for i in intfs.result if "NNI" in i["description"] ] # Get untrusted interfaces task.host["untrusted"] = [ i["name"] for i in intfs.result if "NNI" not in i["description"] and not i["mtu"] ] # Render j2 template template = task.run( task=template_file, path="nornir_cli/custom_commands/templates", template="dhcp_snooping.j2", ) # Configure commands from j2 template task.host["template"] = template.result task.run( task=netmiko_send_config, name="Configure dhcp snooping", config_commands=template.result, cmd_verify=False, exit_config_mode=False, )
def teardown_method(self, method): ConnectionPluginRegister.deregister_all() ConnectionPluginRegister.auto_register()
def cli(ctx, pg_bar, show_result, *args, **kwargs): ConnectionPluginRegister.auto_register() # 'None' = None kwargs.update({k: v for k, v in zip(kwargs, _json_loads(kwargs.values()))}) current_func_params = next(ctx.obj["queue_functions_generator"]) # try to pass not all arguments, but only the necessary ones if kwargs == list(current_func_params.values())[0]: function, parameters = list(current_func_params.items())[0] else: param_iterator = iter(current_func_params.values()) params = list(next(param_iterator).items()) function, parameters = list(current_func_params)[0], { key: value for key, value in kwargs.items() if (key, value) not in params } # get the current Nornir object from Commands chain or from temp.pkl file try: nr = ctx.obj["nornir"] except KeyError: nr = _pickle_to_hidden_file("temp.pkl", mode="rb", dump=False) nr.config.logging.configure() # pg_bar option if pg_bar: with tqdm( total=len(nr.inventory.hosts), desc="processing: ", ) as pb: task = nr.run( task=multiple_progress_bar, method=function, pg_bar=pb, **parameters, ) print() else: task = nr.run( task=function, **parameters, ) # show_result option if show_result: print_result(task, severity_level=logging.DEBUG) print() # show statistic ch_sum = 0 for host in nr.inventory.hosts: f, ch = (task[host].failed, task[host].changed) ch_sum += int(ch) click.secho( f"{host:<50}: ok={not f:<15} changed={ch:<15} failed={f:<15}", fg=_get_color(f, ch), bold=True, ) print() f_sum = len(nr.data.failed_hosts) ok_sum = len(nr.inventory.hosts) - f_sum for state, summary, color in zip(("OK", "CHANGED", "FAILED"), (ok_sum, ch_sum, f_sum), ("green", "yellow", "red")): click.secho( f"{state:<8}: {summary}", fg=color, bold=True, ) print()