def test_nornsible_task_explicit_task(): testargs = ["somescript", "-l", "localhost", "-t", "custom_task_example_2"] with patch.object(sys, "argv", testargs): nr = InitNornir( inventory={ "plugin": "nornir.plugins.inventory.simple.SimpleInventory", "options": { "host_file": f"{TEST_DIR}_test_nornir_inventory/basic/hosts.yaml", "group_file": f"{TEST_DIR}_test_nornir_inventory/basic/groups.yaml", }, }, logging={"enabled": False}, ) nr = InitNornsible(nr) print(nr.inventory.hosts) tasks = [custom_task_example, custom_task_example_2] task_results = [] for task in tasks: task_results.append(nr.run(task=task)) assert task_results[0]["localhost"].result == "Task skipped!" assert task_results[1]["localhost"].result == "Hello, world!" assert task_results[0][ "delegate"].result == "Task skipped, delegate host!" assert task_results[1][ "delegate"].result == "Task skipped, delegate host!"
def main(): nr = InitNornir(config_file="config.yaml") nr = InitNornsible(nr) print(nr.config.core.num_workers) agg_result = nr.run(task=my_custom_task_1) print_result(agg_result) agg_result = nr.run(task=my_custom_task_10) print_result(agg_result)
def main(): tasks = [render_configs, display_configs, deploy_configs, backup_configs] nr = InitNornir(config_file="nornir_data/config.yaml") nr = InitNornsible(nr) for task in tasks: task_result = nr.run(task=task) process_results(nr, task_result)
def main(): # Create Nornir Object, parse with nornsible CLI arguments nr = InitNornir(config_file=NORNIR_CONFIG_FILE) nr = InitNornsible(nr) # Template Configs nr.run(task=render_template) # Deploy templated config to switches, print diff, confirm replace nr.run(task=replace_config)
def main(): nr = InitNornir(config_file=NORNIR_CONFIG_FILE) nr = InitNornsible(nr) nr.run(task=get_file_system) nr.run(task=get_images_in_flash) nr.run(task=get_running_image) nr.run(task=get_images_to_remove) nr.run(task=remove_old_images) nr.run(task=copy_primary_image) nr.run(task=set_boot_vars) nr.run(task=verify) nr.run(task=print_results, num_workers=1)
def main(): tasks = [ gather_reachable_interfaces, validate_reachability, validate_ospf_peers, validate_bgp_neighbors, validate_routes, ] nr = InitNornir(config_file="nornir_data/config.yaml") nr = InitNornsible(nr) for task in tasks: task_result = nr.run(task=task) process_results(nr, task_result)
def test_set_nornsible_do_nothing(): testargs = ["somescript"] with patch.object(sys, "argv", testargs): initial_nr = InitNornir( inventory={ "plugin": "nornir.plugins.inventory.simple.SimpleInventory", "options": { "host_file": f"{TEST_DIR}_test_nornir_inventory/hosts.yaml", "group_file": f"{TEST_DIR}_test_nornir_inventory/groups.yaml", }, }, logging={"enabled": False}, ) nornsible_nr = InitNornsible(initial_nr) assert nornsible_nr == initial_nr
def test_set_nornsible_limit_group_invalid_disable_delegate(): testargs = ["somescript", "-g", "eos1234", "-d"] with patch.object(sys, "argv", testargs): nr = InitNornir( inventory={ "plugin": "nornir.plugins.inventory.simple.SimpleInventory", "options": { "host_file": f"{TEST_DIR}_test_nornir_inventory/hosts.yaml", "group_file": f"{TEST_DIR}_test_nornir_inventory/groups.yaml", }, }, logging={"enabled": False}, ) nr = InitNornsible(nr) assert set(nr.inventory.hosts.keys()) == set()
def test_set_nornsible_workers(): testargs = ["somescript", "-w", "10"] with patch.object(sys, "argv", testargs): nr = InitNornir( inventory={ "plugin": "nornir.plugins.inventory.simple.SimpleInventory", "options": { "host_file": f"{TEST_DIR}_test_nornir_inventory/hosts.yaml", "group_file": f"{TEST_DIR}_test_nornir_inventory/groups.yaml", }, }, logging={"enabled": False}, ) nr = InitNornsible(nr) assert nr.config.core.num_workers == 10
def test_nornsible_task_skip_task_disable_delegate(): testargs = ["somescript", "-l", "localhost", "-s", "custom_task_example", "-d"] with patch.object(sys, "argv", testargs): nr = InitNornir( inventory={ "plugin": "nornir.plugins.inventory.simple.SimpleInventory", "options": { "host_file": f"{TEST_DIR}_test_nornir_inventory/hosts.yaml", "group_file": f"{TEST_DIR}_test_nornir_inventory/groups.yaml", }, }, logging={"enabled": False}, ) nr = InitNornsible(nr) task_result = nr.run(task=custom_task_example) assert set(task_result.keys()) == {"localhost"} assert task_result["localhost"].result == "Task skipped!"
def test_set_nornsible_limit_exclude_host(): testargs = ["somescript", "-l", "!sea-eos-1,!upper-host"] with patch.object(sys, "argv", testargs): nr = InitNornir( inventory={ "plugin": "nornir.plugins.inventory.simple.SimpleInventory", "options": { "host_file": f"{TEST_DIR}_test_nornir_inventory/basic/hosts.yaml", "group_file": f"{TEST_DIR}_test_nornir_inventory/basic/groups.yaml", }, }, logging={"enabled": False}, ) nr = InitNornsible(nr) assert set(nr.inventory.hosts.keys()) == { "localhost", "delegate", "sea-nxos-1" }
def main(): # Initialize Nornir objects, filter with nornsible, remove delegate from nornsible returned hosts nr = InitNornir(config_file=NORNIR_CONFIG_FILE) nr = InitNornsible(nr) nr.inventory.hosts.pop('delegate') # Get credentials and assign to nornir inventory objects if they are not already specified update_creds(nr) # Get Configs from devices. Print Results nr.run(task=get_configs) nr.run(task=print_failed_hosts, on_good=False, on_failed=True, num_workers=1) nr.run(task=print_get_cfg_results, num_workers=1) # Commit to git git_commit()
def main(): nr = InitNornir(config_file=NORNIR_CONFIG_FILE) nr = InitNornsible(nr) nr.run(task=commit_config) nr.run(task=reboot_device) nr.run(task=ping_until_up)