Skip to content
Snippets Groups Projects
Commit 1ee42377 authored by Attila Farkas's avatar Attila Farkas
Browse files

add aliases

parent e187cb1e
No related branches found
No related tags found
1 merge request!7Resolve Refactoring
from modules.file_manager import generate_file
from modules.ansible_vars_generator import generate_ansible_vars
def _create_inventory(input_definitions):
""" Creates an inventory file with host groups. """
host_names = []
for host in input_definitions["hosts"]:
host_names.append(host["name"])
router_names = []
for router in input_definitions["routers"]:
router_names.append(router["name"])
generate_file("inventory", "provisioning/inventory.ini", hosts=host_names, routers=router_names)
def _create_config(input_definitions, flags):
""" Creates a file with common variables for all roles. """
hosts = []
for host in input_definitions["hosts"]:
new_host = dict()
new_host["name"] = host["name"]
hosts.append(new_host)
routers = []
for router in input_definitions["routers"]:
new_router = dict()
new_router["name"] = router["name"]
routers.append(new_router)
generate_file("config", "base_provisioning/config.yml", hosts=hosts, routers=routers)
def _create_config_playbooks(input_definitions, flags):
""" Generates playbooks and roles for basic device configuration. """
# TODO create playbooks
for device in input_definitions["hosts"] + input_definitions["routers"]:
generate_file("separate_devices", "base_provisioning/roles/" + device["name"] + "/tasks/main.yml")
# TODO create other playbooks
return
......@@ -51,15 +26,12 @@ def _create_user_playbooks(input_definitions):
generate_file("user_separate_routers", "provisioning/roles/" + router["name"] + "/tasks/main.yml", router_name=router["name"])
def generate_playbooks(input_definitions, flags):
""" Generates ansible playbooks.
""" Generates ansible vars and playbooks.
:param definitions: device definitions structure
:param flags: command line input flags
"""
_create_inventory(input_definitions)
_create_config(input_definitions, flags)
generate_ansible_vars(input_definitions, flags)
_create_config_playbooks(input_definitions, flags)
_create_user_playbooks(input_definitions)
from modules.file_manager import generate_file, dump_to_yaml
from conf.border_router import BORDER_ROUTER_NETWORK_NAME
def _create_inventory(input_definitions):
""" Creates an inventory file with host groups. """
host_names = []
for host in input_definitions["hosts"]:
host_names.append(host["name"])
router_names = []
for router in input_definitions["routers"]:
router_names.append(router["name"])
generate_file("inventory", "provisioning/inventory.ini", hosts=host_names, routers=router_names)
def _find_networks_of_device(name, input_definitions):
""" Returns a list of network names in which the device have an
interface.
"""
networks = []
for net_mapping in input_definitions["net_mappings"]:
if net_mapping["host"] == name:
networks.append(net_mapping["network"])
if len(networks) == 1:
return networks
if len(networks) > 1:
print("Error: Hosts can have only one interface.")
raise AttributeError
for router_mapping in input_definitions["router_mappings"]:
if router_mapping["router"] == name:
networks.append(router_mapping["network"])
return networks
def _add_aliases(device_name, input_definitions, flags):
""" Generates aliases for the given device. """
home_networks = _find_networks_of_device(device_name, input_definitions)
aliases = dict()
for host_mapping in input_definitions["net_mappings"]:
aliases[host_mapping["host"]] = host_mapping["ip"]
continue
for router_mapping in input_definitions["router_mappings"]:
if router_mapping["network"] in home_networks:
aliases[router_mapping["router"]] = router_mapping["ip"]
continue
for router in input_definitions["routers"]:
if router["name"] not in aliases:
# TODO uncomment when br flag is active
# if "border_router" not in flags:
# print("Error: " + device_name + " has no connection to " + router["name"] + " .")
# raise AttributeError
for router_mapping in input_definitions["router_mappings"]:
if router_mapping["router"] == router["name"] and router_mapping["network"] == BORDER_ROUTER_NETWORK_NAME:
aliases[router_mapping["router"]] = router_mapping["ip"]
continue
for router in input_definitions["routers"]:
if router["name"] not in aliases:
print("Error: " + device_name + " has no connection to " + router["name"] + " .")
raise AttributeError
return aliases
def _generate_device_vars(input_definitions, flags):
""" Generates vars files for all devices separately. """
for target_host in input_definitions["hosts"]:
variables = dict()
variables["aliases"] = _add_aliases(target_host["name"], input_definitions, flags)
dump_to_yaml(variables, "base_provisioning/roles/" + target_host["name"] + "/vars/main.yml")
for target_router in input_definitions["routers"]:
variables = dict()
variables["aliases"] = _add_aliases(target_router["name"], input_definitions, flags)
dump_to_yaml(variables, "base_provisioning/roles/" + target_router["name"] + "/vars/main.yml")
def _create_inventory(input_definitions):
""" Creates an inventory file with host groups. """
host_names = []
for host in input_definitions["hosts"]:
host_names.append(host["name"])
router_names = []
for router in input_definitions["routers"]:
router_names.append(router["name"])
generate_file("inventory", "provisioning/inventory.ini", hosts=host_names, routers=router_names)
def _generate_hosts_vars(input_definitions, flags):
""" Generates vars file for all hosts. """
return
def _generate_routers_vars(input_definitions, flags):
""" Generates vars file for all routers. """
return
def _find_ip(device_name, input_definitions):
""" Returns a dictionary with all network names and ips of a device. """
networks = dict()
for host_mapping in input_definitions["net_mappings"]:
if host_mapping["host"] == device_name:
networks[host_mapping["network"]] = host_mapping["ip"]
for router_mapping in input_definitions["router_mappings"]:
if router_mapping["router"] == device_name:
networks[router_mapping["network"]] = router_mapping["ip"]
return networks
def _find_default_route(device_name, input_definitions, flags):
""" Returns the ip to which the device should be routed defaultly and
None if the change of default routing is not required.
"""
return None
def _generate_config_vars(input_definitions, flags):
""" Generates vars file for all devices. """
hosts = []
for host in input_definitions["hosts"]:
new_host = dict()
new_host["name"] = host["name"]
new_host["networks"] = _find_ip(host["name"], input_definitions)
new_host["route_to"] = _find_default_route(host["name"], input_definitions, flags)
hosts.append(new_host)
routers = []
for router in input_definitions["routers"]:
new_router = dict()
new_router["name"] = router["name"]
new_router["networks"] = _find_ip(router["name"], input_definitions)
new_router["route_to"] = _find_default_route(router["name"], input_definitions, flags)
routers.append(new_router)
generate_file("config", "base_provisioning/config.yml", hosts=hosts, routers=routers)
def generate_ansible_vars(input_definitions, flags):
""" Generates files with variables for ansible. """
_create_inventory(input_definitions)
_generate_config_vars(input_definitions, flags)
_generate_hosts_vars(input_definitions, flags)
_generate_routers_vars(input_definitions, flags)
_generate_device_vars(input_definitions, flags)
......@@ -20,6 +20,23 @@ def open_yaml(file_name):
input_file.close()
def dump_to_yaml(data, filename):
""" Writes a data structure to a YAML document.
:param data: a dict or list which should be written to file
:param filename: name of the target file
"""
try:
stream = open(OUTPUT_DIRECTORY + "/" + filename, 'w')
yaml.dump(data, stream)
except IOError:
print("Error: cannot write to this location.")
raise
finally:
stream.close()
def generate_file(template, filename, **template_args):
""" Generates a file using a template.
......@@ -61,16 +78,20 @@ def _create_provisioning_directories(directory, device_definitions):
if device_definitions["hosts"]:
os.mkdir(OUTPUT_DIRECTORY + "/" + directory + "/roles/hosts")
os.mkdir(OUTPUT_DIRECTORY + "/" + directory + "/roles/hosts/tasks")
os.mkdir(OUTPUT_DIRECTORY + "/" + directory + "/roles/hosts/vars")
for host in device_definitions["hosts"]:
os.mkdir(OUTPUT_DIRECTORY + "/" + directory + "/roles/" + host["name"])
os.mkdir(OUTPUT_DIRECTORY + "/" + directory + "/roles/" + host["name"] + "/tasks")
os.mkdir(OUTPUT_DIRECTORY + "/" + directory + "/roles/" + host["name"] + "/vars")
if device_definitions["routers"]:
os.mkdir(OUTPUT_DIRECTORY + "/" + directory + "/roles/routers")
os.mkdir(OUTPUT_DIRECTORY + "/" + directory + "/roles/routers/tasks")
os.mkdir(OUTPUT_DIRECTORY + "/" + directory + "/roles/routers/vars")
for router in device_definitions["routers"]:
os.mkdir(OUTPUT_DIRECTORY + "/" + directory + "/roles/" + router["name"])
os.mkdir(OUTPUT_DIRECTORY + "/" + directory + "/roles/" + router["name"] + "/tasks")
os.mkdir(OUTPUT_DIRECTORY + "/" + directory + "/roles/" + router["name"] + "/vars")
except FileExistsError:
pass
......
hosts:
{% for host in hosts %}
- name: {{ host.name }}
{% if "networks" in host %}
- networks:
{% endif %}
{% for network, ip in host.networks.items() %}
{{ network }}: {{ ip }}
{% endfor %}
{% endfor %}
routers:
{% for router in routers %}
- name: {{ router.name }}
{% if "networks" in router %}
- networks:
{% endif %}
{% for network, ip in router.networks.items() %}
{{ network }}: {{ ip }}
{% endfor %}
{% endfor %}
---
# Basic configuration of all defined devices
- name: include common variables
include_vars:
file: config.yml
name: config
- name: Configuring all hosts
hosts: hosts
become: yes
roles:
- hosts
- name: Configuring host {{ item.name }} separately
hosts: {{ item.name }}
- name: Configuring hosts separately
hosts: {{ "{{ item.name }}" }}
become: yes
loop: "{{ hosts }}"
loop: {{ "\"{{ config.hosts }}\"" }}
roles:
- {{ item.name }}
- {{ "{{ item.name }}" }}
{# TODO finish playbook #}
{% for host in hosts %}
- name: Configuring host {{ host.host_name }}
hosts: {{ host.host_name }}
hosts: hosts
become: yes
tasks:
{% for network_ip in network_ips %}
......@@ -29,7 +33,10 @@
command: route add -net {{ network_ip }} gw {{ host.router_ip }} {{ host.interface }}
{% endfor %}
{% endfor %}
- name: Configuring all routers
hosts: {{ routers|map(attribute='router_name')|unique|reject('eq', border_router_name)|join(',') }}
become: yes
......
......@@ -2,22 +2,8 @@
# Basic configuration of all host devices
- name: Install net-tools
command: apt install net-tools
{% for host in hosts %}
- name: Add {{ host.host_name }} alias
lineinfile:
path: /etc/hosts
line: {{ host.host_ip }} {{ host.host_name }}
{% endfor %}
{% for router in routers %}
- name: Add {{ router.router_name }} alias
lineinfile:
path: /etc/hosts
line: {{ router.router_ip }} {{ router.router_name }}
{% endfor %}
apt:
name: net-tools
- name: Delete default gateway
command: route del default
......
......@@ -9,20 +9,6 @@
- name: Restarting procps service
command: /etc/init.d/procps restart
{% for host in hosts %}
- name: Add {{ host.host_name }} alias
lineinfile:
path: /etc/hosts
line: {{ host.host_ip }} {{ host.host_name }}
{% endfor %}
{% for router in routers %}
- name: Add {{ router.router_name }} alias
lineinfile:
path: /etc/hosts
line: {{ router.router_ip }} {{ router.router_name }}
{% endfor %}
- name: Delete default gateway
command: route del default
......
- name: Add aliases
loop: {{ "{{ aliases|dict2items }}" }}
lineinfile:
path: /etc/hosts
line: {{ "{{ item.value }} \"{{ item.key }}\"" }}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment