diff --git a/modules/ansible_generator.py b/modules/ansible_generator.py index a0c70728f01f562f971018a76724f1fb325eb292..4409f8314bb5c4fa46f9ef2498fbbbc9c0220a25 100644 --- a/modules/ansible_generator.py +++ b/modules/ansible_generator.py @@ -5,8 +5,11 @@ from modules.ansible_vars_generator import generate_ansible_vars def _create_config_playbooks(input_definitions, flags): """ Generates playbooks and roles for basic device configuration. """ + copy_template_file("device_configuration", "base_provisioning/device_configuration.yml") + copy_template_file("hosts", "base_provisioning/roles/hosts/tasks/main.yml") + for device in input_definitions["hosts"] + input_definitions["routers"]: - generate_file("separate_devices", "base_provisioning/roles/" + device["name"] + "/tasks/main.yml") + copy_template_file("separate_devices", "base_provisioning/roles/" + device["name"] + "/tasks/main.yml") # TODO create other playbooks return diff --git a/modules/ansible_vars_generator.py b/modules/ansible_vars_generator.py index c58aff302acd392743eb374b30501fc2b3849bfc..403f6aca9f2a6345b316566823d6e5a979063012 100644 --- a/modules/ansible_vars_generator.py +++ b/modules/ansible_vars_generator.py @@ -2,20 +2,6 @@ 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. @@ -149,7 +135,6 @@ def _generate_config_vars(input_definitions, flags): # TODO check what vars need 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) diff --git a/modules/vagrant_generator.py b/modules/vagrant_generator.py index 58abc605d79f3f6703e42d83c8c57e4d9a51c8a5..d02f94987bf65209ad2be821d8fc4d179b05111f 100644 --- a/modules/vagrant_generator.py +++ b/modules/vagrant_generator.py @@ -8,8 +8,6 @@ VAGRANT_MAPPING = open_yaml("conf/vagrant_mapping.yml") VIRTUALBOX_MAPPING = open_yaml("conf/virtualbox_mapping.yml") BASE_PLAYBOOK = "base_provisioning/device_configuration.yml" USER_PLAYBOOK = "provisioning/playbook.yml" -INVENTORY_FILE = "provisioning/inventory.ini" - def _create_simple_attribute(key, value, attribute_type): @@ -49,7 +47,7 @@ def _create_commands(device_attributes, device_type, input_definitions, flags): return commands -def _create_ansible_commands(playbook_location, flags): +def _create_ansible_commands(playbook_location, input_definitions, flags): """ Creates commands for running a playbook from the Vagrantfile. """ commands = [] @@ -60,12 +58,6 @@ def _create_ansible_commands(playbook_location, flags): playbook["value"] = playbook_location commands.append(playbook) - inventory = dict() - inventory["type"] = "string" - inventory["command"] = "inventory_path" - inventory["value"] = INVENTORY_FILE - commands.append(inventory) - if "verbose_ansible" in flags and flags["verbose_ansible"]: verbosity = dict() verbosity["type"] = "boolean" @@ -73,6 +65,20 @@ def _create_ansible_commands(playbook_location, flags): verbosity["value"] = True commands.append(verbosity) + groups = dict() + groups["type"] = "dictionary" + groups["command"] = "groups" + groups["dictionary"] = dict() + host_names = [] + for host in input_definitions["hosts"]: + host_names.append(host["name"]) + groups["dictionary"]["hosts"] = host_names + router_names = [] + for router in input_definitions["routers"]: + router_names.append(router["name"]) + groups["dictionary"]["routers"] = router_names + commands.append(groups) + return commands @@ -109,7 +115,7 @@ def _add_all_networks(vagrant_definitions, input_definitions, flags): _add_networks_to_device(definition, input_definitions["router_mappings"], input_definitions) -def _call_provisioner(flags): +def _call_provisioner(input_definitions, flags): """ Creates entry to vagrant definitions for calling the provisioner. """ provisioner_calls = [] @@ -122,7 +128,7 @@ def _call_provisioner(flags): config_playbook["provisioner"] = "ansible" config_playbook["note"] = "basic configuration of devices and networks" config_playbook["commands"] = _create_ansible_commands( - BASE_PLAYBOOK, flags) + BASE_PLAYBOOK, input_definitions, flags) provisioner_calls.append(config_playbook) @@ -134,7 +140,7 @@ def _call_provisioner(flags): user_playbook["provisioner"] = "ansible" user_playbook["note"] = "user configuration of devices" user_playbook["commands"] = _create_ansible_commands( - USER_PLAYBOOK, flags) + USER_PLAYBOOK, input_definitions, flags) provisioner_calls.append(user_playbook) @@ -164,7 +170,7 @@ def _build_vagrant_definitions(input_definitions, flags): _add_all_networks(vagrant_definitions, input_definitions, flags) - vagrant_definitions.extend(_call_provisioner(flags)) + vagrant_definitions.extend(_call_provisioner(input_definitions, flags)) return vagrant_definitions diff --git a/templates/device_configuration b/templates/device_configuration index d123d0dd02c9eb11818fe988721ae634f9109b78..d6951767bd0b148d236ca2e467411a70a4e9ef3f 100644 --- a/templates/device_configuration +++ b/templates/device_configuration @@ -1,51 +1,50 @@ --- # Basic configuration of all defined devices -- name: include common variables - include_vars: - file: config.yml - name: config +- name: Including variables + hosts: all + tasks: + + - name: Including common variables + include_vars: + file: config.yml + name: config -- name: Configuring all hosts +- name: Configuring hosts hosts: hosts - become: yes roles: - hosts -- name: Configuring hosts separately - hosts: {{ "{{ item.name }}" }} - become: yes - loop: {{ "\"{{ config.hosts }}\"" }} - roles: - - {{ "{{ item.name }}" }} - - -{# TODO finish playbook #} - +#- name: Configuring routers +# hosts: routers +# roles: +# - routers - -- name: Configuring host {{ host.host_name }} - hosts: hosts - become: yes +- name: Configuring devices separately + hosts: all tasks: -{% for network_ip in network_ips %} - - name: Add gateway for {{ network_ip }} - command: route add -net {{ network_ip }} gw {{ host.router_ip }} {{ host.interface }} -{% endfor %} + + - name: include role + include_role: + name: "{{ inventory_hostname }}" +... +#- name: Configuring host {{ host.host_name }} +# hosts: hosts +# become: yes +# tasks: +#{% for network_ip in network_ips %} +# - name: Add gateway for {{ network_ip }} +# command: route add -net {{ network_ip }} gw {{ host.router_ip }} {{ host.interface }} +#{% endfor %} -- name: Configuring all routers - hosts: {{ routers|map(attribute='router_name')|unique|reject('eq', border_router_name)|join(',') }} - become: yes - roles: - - routers -- name: Configuring border router - hosts: {{ border_router_name }} - become: yes - roles: - - br -... +#- name: Configuring border router +# hosts: {{ border_router_name }} +# become: yes +# roles: +# - br +#... diff --git a/templates/hosts b/templates/hosts index 33cdbc5ed8dde28d8f9d1dd331f792026c917325..05ebab4386403488d89ef30804683cc47a0625e3 100644 --- a/templates/hosts +++ b/templates/hosts @@ -5,6 +5,6 @@ apt: name: net-tools -- name: Delete default gateway - command: route del default +# name: Delete default gateway +# command: route del default ... diff --git a/templates/inventory b/templates/inventory deleted file mode 100644 index cb04ca3c000a9a53e68f060306a83e3050bc4a97..0000000000000000000000000000000000000000 --- a/templates/inventory +++ /dev/null @@ -1,10 +0,0 @@ -[hosts] -{% for host in hosts %} - {{ host }} -{% endfor %} - -[routers] -{% for router in routers %} - {{ router }} -{% endfor %} - diff --git a/templates/separate_devices b/templates/separate_devices index 9309bbb2b09415dc259ed56c6e46ace10869f5b2..212d1244e737062d61067f425b29d8001773aa30 100644 --- a/templates/separate_devices +++ b/templates/separate_devices @@ -1,5 +1,5 @@ - name: Add aliases - loop: {{ "{{ aliases|dict2items }}" }} + loop: "{{ aliases|dict2items }}" lineinfile: path: /etc/hosts - line: {{ "{{ item.value }} \"{{ item.key }}\"" }} + line: {{ item.value }} "{{ item.key }}" diff --git a/templates/vagrantfile b/templates/vagrantfile index 50d92e76ff1f13c70218528c3f05d8dcb2ad43aa..ae000d70a14bf288c015ff8eb92b763d593503af 100644 --- a/templates/vagrantfile +++ b/templates/vagrantfile @@ -49,6 +49,19 @@ {{ namespace }}.{{ item.command }} = {{ item.value }} {% endmacro -%} +{# Macro for dictionaries #} +{% macro dictionary(item, namespace) %} + {{ namespace }}.{{ item.command }} = { + {% for key, value in item.dictionary.items() %} + {% if loop.last %} + "{{ key }}" => {{ value }} + {% else %} + "{{ key }}" => {{ value }}, + {% endif %} + {% endfor %} + } +{% endmacro -%} + {# Macro for network items #} {% macro network(item, namespace) %} {{ namespace }}.vm.network :{{ item.network_type }}, ip: "{{ item.ip }}" @@ -87,6 +100,8 @@ end {{ provider(item, namespace) -}} {% elif item.type == "network" %} {{ network(item, namespace) -}} +{% elif item.type == "dictionary" %} + {{ dictionary(item, namespace) -}} {% endif %} {% endfor %} end