Exemple #1
0
def main():
    # Decrypt the credentials for all devices from the encrypted file via Ansible vault
    credentials = get_credentials.get_credentials()

    # Instantiate Nornir with given config file
    nr = InitNornir(config_file="nornir_data/config.yaml")

    # Assign the decrypted credentials to default username/password values for the devices in Nornir inventory
    nr.inventory.defaults.username = credentials["username"]
    nr.inventory.defaults.password = credentials["password"]

    # Retrieve expected OSPF information
    ospf_expected_routes, ospf_expected_neighbors_dict = get_ospf_information()

    # Reset current ospf process to ensure that any changes are currently active for testing purposes
    ospf_reset_result = nr.run(task=reset_ospf,
                               name="OSPF PROCESS RESET STARTED")
    # print_result(ospf_reset_result)

    # Wait a minute, to allow the devices to complete OSPF process
    time.sleep(90)

    # Run OSPF database test to verify if every device has all of the expected OSPF routes
    ospf_routing_test_results = nr.run(task=ospf_routing_test,
                                       comparison_list=ospf_expected_routes)
    # print_result(ospf_routing_test_results)

    # Run OSPF neighbor test to verify if every device has the expected number of neighbors
    ospf_neighbor_test_results = nr.run(
        task=ospf_neighbor_test,
        ospf_expected_neighbors_dict=ospf_expected_neighbors_dict)
def get_native(task):
    credentials = get_credentials.get_credentials()
    nr = InitNornir(config_file="/home/kamil/Hons/nornir_data/config.yaml")
    nr.inventory.defaults.username = credentials["username"]
    nr.inventory.defaults.password = credentials["password"]

    looking_for_path = "/native"
    result = task.run(task=netconf_get,
                      filter_=looking_for_path,
                      filter_type="xpath")
    task.run(task=write_file,
             content=result.result,
             filename=f"/home/kamil/Hons/host_vars/native/{task.host}.xml")

    with open(f"/home/kamil/Hons/host_vars/native/{task.host}.xml") as fd:
        doc = xmltodict.parse(fd.read())
    doc2 = doc["rpc-reply"]["data"]
    for i in list(doc2["native"]):
        doc2["native"].update({"_operation": "replace"})
        doc2["native"].move_to_end("_operation", last=False)

    doc3 = get_all_values_dict(doc2)

    with open(f"/home/kamil/Hons/host_vars/native/{task.host}.json", "w") as f:
        f.write(json.dumps(doc3, indent=4, sort_keys=False))

    with open(f"/home/kamil/Hons/host_vars/native/{task.host}.json",
              "r") as ld:
        data = json.load(ld)

    with open(f"/home/kamil/Hons/host_vars/native/{task.host}.yaml",
              "w") as yf:
        yaml.safe_dump(data, yf, allow_unicode=True, sort_keys=False)

    return result
Exemple #3
0
def main():
    # Decrypt the credentials for all devices from the encrypted file via Ansible vault
    credentials = get_credentials.get_credentials()

    # Instantiate Nornir with given config file
    nr = InitNornir(config_file="nornir_data/config.yaml")

    # Assign the decrypted credentials to default username/password values for the devices in Nornir inventory
    nr.inventory.defaults.username = credentials["username"]
    nr.inventory.defaults.password = credentials["password"]

    reset_bgp_result = nr.run(task=reset_bgp, name="BGP PROCESS RESET STARTED")
    # print_result(reset_bgp_result)
    bgp_expected_routers, bgp_expected_networks = get_bgp_information()
    time.sleep(120)

    bgp_routers_test_results = nr.run(
        task=bgp_routers_test,
        bgp_expected_routers=bgp_expected_routers,
        name="BGP ROUTERS TEST STARTED")
    # print_result(bgp_routers_test)

    bgp_networks_test_results = nr.run(
        task=bgp_networks_test,
        bgp_expected_networks=bgp_expected_networks,
        name="BGP NETWORKS TEST STARTED")
Exemple #4
0
def main():
    # Decrypt the credentials for all devices from the encrypted file via Ansible vault
    credentials = get_credentials.get_credentials()

    # Instantiate Nornir with given config file
    nr = InitNornir(config_file="nornir_data/config.yaml")
    # Assign the decrypted credentials to default username/password values for the devices in Nornir inventory
    nr.inventory.defaults.username = credentials["username"]
    nr.inventory.defaults.password = credentials["password"]

    # Lock the configuration of all devices to prevent other users from issuing any changes
    lock = nr.run(task=config_lock)
    print_result(lock)

    # Load host variables, build templates for all specified features and push to the candidate configuration
    results = nr.run(task=load_vars)
    print_result(results)

    # Verify if any config build or config push has failed
    # If everything was successful, commit the configuration from candidate to running
    # Otherwise, discard the candidate configuration and issue a fail report
    failed_changes = nr.data.failed_hosts
    if failed_changes:
        revert = nr.run(task=config_discard)
        print_result(revert)
        for device in failed_changes:
            fail_report(device, results[device].exception,)
    else:
        commit_config = nr.run(task=config_commit)
        print_result(commit_config)

    # Unlock the devices configuration
    unlock = nr.run(task=config_unlock)
    print_result(unlock)
def main():
    credentials = get_credentials.get_credentials()
    nr = InitNornir(config_file="nornir_data/config.yaml")
    nr.inventory.defaults.username = credentials["username"]
    nr.inventory.defaults.password = credentials["password"]
    #nr = nr.filter(F(groups__contains="test_subject"))
    lock = nr.run(task=config_lock)
    print_result(lock)
    results = nr.run(task=load_vars)
    print_result(results)
    failed_changes = nr.data.failed_hosts
    if failed_changes:
        revert = nr.run(task=config_discard)
        print_result(revert)
        for device in failed_changes:
            fail_report(
                device,
                results[device].exception,
            )
    else:
        commit_config = nr.run(task=config_commit)
        print_result(commit_config)

    unlock = nr.run(task=config_unlock)
    print_result(unlock)
Exemple #6
0
def main():
    credentials = get_credentials.get_credentials()
    nr = InitNornir(config_file="nornir_data/config.yaml")
    nr.inventory.defaults.username = credentials["username"]
    nr.inventory.defaults.password = credentials["password"]

    vpn_test_results = nr.run(task=vpn_test, )
    print_result(vpn_test_results)
Exemple #7
0
def main():
    credentials = get_credentials.get_credentials()
    nr = InitNornir(config_file="nornir_data/config.yaml")
    nr.inventory.defaults.username = credentials["username"]
    nr.inventory.defaults.password = credentials["password"]

    expected_interfaces = get_interface_information()
    interface_test_results = nr.run(
        task=interface_test, expected_interfaces=expected_interfaces
    )
Exemple #8
0
def main():
    # Decrypt the credentials for all devices from the encrypted file via Ansible vault
    credentials = get_credentials.get_credentials()

    # Instantiate Nornir with given config file
    nr = InitNornir(config_file="nornir_data/config.yaml")

    # Assign the decrypted credentials to default username/password values for the devices in Nornir inventory
    nr.inventory.defaults.username = credentials["username"]
    nr.inventory.defaults.password = credentials["password"]

    # Run the VPN test with Nornir
    vpn_test_results = nr.run(task=vpn_test, )
def main():

    list_of_features_to_get = [
        get_ntp, get_router, get_ip, get_interface, get_crypto
    ]
    credentials = get_credentials.get_credentials()
    nr = InitNornir(config_file="/home/kamil/Hons/nornir_data/config.yaml")
    nr.inventory.defaults.username = credentials["username"]
    nr.inventory.defaults.password = credentials["password"]
    #nr = nr.filter(F(groups__contains="test_subject"))

    for feature in list_of_features_to_get:
        result = nr.run(task=feature)
        print_result(result)
Exemple #10
0
def main():

    list_of_features_to_get = [
        "router", "ntp", "interface", "crypto", "enable", "ip", "line"
    ]

    credentials = get_credentials.get_credentials()
    nr = InitNornir(config_file="/home/kamil/Hons/nornir_data/config.yaml")
    nr.inventory.defaults.username = credentials["username"]
    nr.inventory.defaults.password = credentials["password"]
    #spine = nr.filter(F(groups__contains="spine"))

    for feature in list_of_features_to_get:
        result = nr.run(task=get_feature, feature=feature)
        print_result(result)
Exemple #11
0
def main():
    # Decrypt the credentials for all devices from the encrypted file via Ansible vault
    credentials = get_credentials.get_credentials()

    # Instantiate Nornir with given config file
    nr = InitNornir(config_file="nornir_data/config.yaml")

    # Assign the decrypted credentials to default username/password values for the devices in Nornir inventory
    nr.inventory.defaults.username = credentials["username"]
    nr.inventory.defaults.password = credentials["password"]

    # Retrieve expected interfaces information (shut and up states)
    expected_interfaces = get_interface_information()

    # Run a test to verify if the interfaces that are expected to be either shut or up, are in the expected state.
    interface_test_results = nr.run(task=interface_test,
                                    expected_interfaces=expected_interfaces)
def main():
    # Select which get functions would you like to run to retrieve the devices' configs
    # Each of the features has a corresponding function, which retrieves key information about each feature
    # The key information is then stored in a host_var file, which combines config from all features
    list_of_features_to_get = [
        get_ntp, get_router, get_ip, get_interface, get_crypto
    ]

    # Decrypt the credentials for all devices from the encrypted file via Ansible vault
    credentials = get_credentials.get_credentials()

    # Instantiate Nornir with given config file
    nr = InitNornir(config_file="nornir_data/config.yaml")
    nr.inventory.defaults.username = credentials["username"]
    nr.inventory.defaults.password = credentials["password"]

    # Retrieve all of the specified features from the list_of_features_to_get
    for feature in list_of_features_to_get:
        result = nr.run(task=feature)
        print_result(result)
Exemple #13
0
def main():

    credentials = get_credentials.get_credentials()
    nr = InitNornir(config_file="nornir_data/config.yaml")
    nr.inventory.defaults.username = credentials["username"]
    nr.inventory.defaults.password = credentials["password"]

    ospf_expected_routes, ospf_expected_neighbors_dict = get_ospf_information()
    ospf_reset_result = nr.run(task=reset_ospf,
                               name="OSPF PROCESS RESET STARTED")
    # print_result(ospf_reset_result)
    time.sleep(60)

    ospf_routing_test_results = nr.run(task=ospf_routing_test,
                                       comparison_list=ospf_expected_routes)
    # print_result(ospf_routing_test_results)

    ospf_neighbor_test_results = nr.run(
        task=ospf_neighbor_test,
        ospf_expected_neighbors_dict=ospf_expected_neighbors_dict)
Exemple #14
0
def main():

    requests.packages.urllib3.disable_warnings()
    headers = {
        "Content-Type": "application/yang-data+json",
        "Accept":
        "application/yang-data+json, application/yang-data.errors+json",
    }

    credentials = get_credentials.get_credentials()
    nr = InitNornir(config_file="/home/kamil/Hons/nornir_data/config.yaml")
    nr.inventory.defaults.username = credentials["username"]
    nr.inventory.defaults.password = credentials["password"]

    #spine = nr.filter(F(groups__contains="spine"))
    #native_result = spine.run(task=native_conf, name="PUSHING NATIVE VIA RESTCONF", headers=headers,)
    native_result = nr.run(task=native_conf,
                           name="PUSHING NATIVE VIA RESTCONF",
                           headers=headers)

    print_result(native_result)
Exemple #15
0
def main():
    credentials = get_credentials.get_credentials()
    nr = InitNornir(config_file="/home/kamil/Hons/nornir_data/config.yaml")
    nr.inventory.defaults.username = credentials["username"]
    nr.inventory.defaults.password = credentials["password"]
    #spine = nr.filter(F(groups__contains="spine"))

    lock = nr.run(task=config_lock)
    print_result(lock)

    for feature in Features_to_push:
        results = nr.run(task=edit_nc_config_from_yaml, feature=feature)
        print_result(results)

    failed_changes = nr.data.failed_hosts
    if failed_changes:
        revert = nr.run(task=config_discard)
        print_result(revert)
    else:
        commit_config = nr.run(task=config_commit)
        print_result(commit_config)

    unlock = nr.run(task=config_unlock)
    print_result(unlock)
Exemple #16
0
import requests
import json
from nornir import InitNornir
from nornir_utils.plugins.functions import print_result
from supporting_scripts import get_credentials
import yaml

credentials = get_credentials.get_credentials()
nr = InitNornir(config_file="/home/kamil/Hons/nornir_data/config.yaml")
nr.inventory.defaults.username = credentials["username"]
nr.inventory.defaults.password = credentials["password"]
requests.packages.urllib3.disable_warnings()

headers = {
    'Accept': 'application/yang-data+json',
    'Content-Type': 'application/yang-data+json'
}


def testconf(task):
    url = f"https://{task.host.hostname}/restconf/data/native/"
    response = requests.get(url,
                            headers=headers,
                            auth=(task.host.username, task.host.password),
                            verify=False).json()
    with open(f"/home/kamil/Hons/host_vars/RESTCONF_native/{task.host}.json",
              "w") as jsn:
        json.dump(response, jsn, indent=4, sort_keys=False)
    with open(f"/home/kamil/Hons/host_vars/RESTCONF_native/{task.host}.yaml",
              "w") as yml:
        yaml.safe_dump(response, yml, sort_keys=False)