ansible_data_generator.py 1.43 KB
Newer Older
Attila Farkas's avatar
Attila Farkas committed
1
2
3
4
from modules.device_creator import open_file

INTERFACE_FILE = "name_mapping/interface.yml"

5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def create_network_map(net_mappings, router_mappings):
    """ Creates a structure with network topology for Jinja2 template. """

    routings = []

    for router_mapping in router_mappings:
        routing = dict()
        routing["router_name"] = router_mapping["router"]
        routing["router_ip"] = router_mapping["ip"]
        routings.append(routing)

    return routings


def _find_router_ip(network_name, router_mappings):
    for router_mapping in router_mappings:
        if router_mapping["network"] == network_name:
            return router_mapping["ip"]


Attila Farkas's avatar
Attila Farkas committed
25
26
27
28
29
30
31
32
33
34
35
36
def _find_interface(host_name, hosts):

    for host in hosts:
        if host["name"] == host_name:
            interfaces = open_file(INTERFACE_FILE)
            if host["base_box"] in interfaces:
                return interfaces[host["base_box"]]

    return "eth1"


def create_host_map(net_mappings, router_mappings, host_list):
37
38
39
40
41
42
43
    """ Creates a structure with hosts and their primary routers ip """

    hosts = []

    for net_mapping in net_mappings:
        host = dict()
        host["host_name"] = net_mapping["host"]
Attila Farkas's avatar
Attila Farkas committed
44
        host["host_ip"] = net_mapping["ip"]
45
46
        host["router_ip"] = _find_router_ip(
            net_mapping["network"], router_mappings)
Attila Farkas's avatar
Attila Farkas committed
47
        host["interface"] = _find_interface(net_mapping["host"], host_list)
48
49
        hosts.append(host)
    return hosts