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

use groups in vagrantfile instead of inventory file

parent 7fbe0bef
No related branches found
No related tags found
1 merge request!7Resolve Refactoring
...@@ -5,8 +5,11 @@ from modules.ansible_vars_generator import generate_ansible_vars ...@@ -5,8 +5,11 @@ from modules.ansible_vars_generator import generate_ansible_vars
def _create_config_playbooks(input_definitions, flags): def _create_config_playbooks(input_definitions, flags):
""" Generates playbooks and roles for basic device configuration. """ """ 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"]: 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 # TODO create other playbooks
return return
......
...@@ -2,20 +2,6 @@ from modules.file_manager import generate_file, dump_to_yaml ...@@ -2,20 +2,6 @@ from modules.file_manager import generate_file, dump_to_yaml
from conf.border_router import BORDER_ROUTER_NETWORK_NAME 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): def _find_networks_of_device(name, input_definitions):
""" Returns a list of network names in which the device have an """ Returns a list of network names in which the device have an
interface. interface.
...@@ -149,7 +135,6 @@ def _generate_config_vars(input_definitions, flags): # TODO check what vars need ...@@ -149,7 +135,6 @@ def _generate_config_vars(input_definitions, flags): # TODO check what vars need
def generate_ansible_vars(input_definitions, flags): def generate_ansible_vars(input_definitions, flags):
""" Generates files with variables for ansible. """ """ Generates files with variables for ansible. """
_create_inventory(input_definitions)
_generate_config_vars(input_definitions, flags) _generate_config_vars(input_definitions, flags)
_generate_hosts_vars(input_definitions, flags) _generate_hosts_vars(input_definitions, flags)
_generate_routers_vars(input_definitions, flags) _generate_routers_vars(input_definitions, flags)
......
...@@ -8,8 +8,6 @@ VAGRANT_MAPPING = open_yaml("conf/vagrant_mapping.yml") ...@@ -8,8 +8,6 @@ VAGRANT_MAPPING = open_yaml("conf/vagrant_mapping.yml")
VIRTUALBOX_MAPPING = open_yaml("conf/virtualbox_mapping.yml") VIRTUALBOX_MAPPING = open_yaml("conf/virtualbox_mapping.yml")
BASE_PLAYBOOK = "base_provisioning/device_configuration.yml" BASE_PLAYBOOK = "base_provisioning/device_configuration.yml"
USER_PLAYBOOK = "provisioning/playbook.yml" USER_PLAYBOOK = "provisioning/playbook.yml"
INVENTORY_FILE = "provisioning/inventory.ini"
def _create_simple_attribute(key, value, attribute_type): def _create_simple_attribute(key, value, attribute_type):
...@@ -49,7 +47,7 @@ def _create_commands(device_attributes, device_type, input_definitions, flags): ...@@ -49,7 +47,7 @@ def _create_commands(device_attributes, device_type, input_definitions, flags):
return commands 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. """ """ Creates commands for running a playbook from the Vagrantfile. """
commands = [] commands = []
...@@ -60,12 +58,6 @@ def _create_ansible_commands(playbook_location, flags): ...@@ -60,12 +58,6 @@ def _create_ansible_commands(playbook_location, flags):
playbook["value"] = playbook_location playbook["value"] = playbook_location
commands.append(playbook) 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"]: if "verbose_ansible" in flags and flags["verbose_ansible"]:
verbosity = dict() verbosity = dict()
verbosity["type"] = "boolean" verbosity["type"] = "boolean"
...@@ -73,6 +65,20 @@ def _create_ansible_commands(playbook_location, flags): ...@@ -73,6 +65,20 @@ def _create_ansible_commands(playbook_location, flags):
verbosity["value"] = True verbosity["value"] = True
commands.append(verbosity) 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 return commands
...@@ -109,7 +115,7 @@ def _add_all_networks(vagrant_definitions, input_definitions, flags): ...@@ -109,7 +115,7 @@ def _add_all_networks(vagrant_definitions, input_definitions, flags):
_add_networks_to_device(definition, input_definitions["router_mappings"], input_definitions) _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. """ """ Creates entry to vagrant definitions for calling the provisioner. """
provisioner_calls = [] provisioner_calls = []
...@@ -122,7 +128,7 @@ def _call_provisioner(flags): ...@@ -122,7 +128,7 @@ def _call_provisioner(flags):
config_playbook["provisioner"] = "ansible" config_playbook["provisioner"] = "ansible"
config_playbook["note"] = "basic configuration of devices and networks" config_playbook["note"] = "basic configuration of devices and networks"
config_playbook["commands"] = _create_ansible_commands( config_playbook["commands"] = _create_ansible_commands(
BASE_PLAYBOOK, flags) BASE_PLAYBOOK, input_definitions, flags)
provisioner_calls.append(config_playbook) provisioner_calls.append(config_playbook)
...@@ -134,7 +140,7 @@ def _call_provisioner(flags): ...@@ -134,7 +140,7 @@ def _call_provisioner(flags):
user_playbook["provisioner"] = "ansible" user_playbook["provisioner"] = "ansible"
user_playbook["note"] = "user configuration of devices" user_playbook["note"] = "user configuration of devices"
user_playbook["commands"] = _create_ansible_commands( user_playbook["commands"] = _create_ansible_commands(
USER_PLAYBOOK, flags) USER_PLAYBOOK, input_definitions, flags)
provisioner_calls.append(user_playbook) provisioner_calls.append(user_playbook)
...@@ -164,7 +170,7 @@ def _build_vagrant_definitions(input_definitions, flags): ...@@ -164,7 +170,7 @@ def _build_vagrant_definitions(input_definitions, flags):
_add_all_networks(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 return vagrant_definitions
......
--- ---
# Basic configuration of all defined devices # Basic configuration of all defined devices
- name: include common variables - name: Including variables
include_vars: hosts: all
file: config.yml tasks:
name: config
- name: Including common variables
include_vars:
file: config.yml
name: config
- name: Configuring all hosts - name: Configuring hosts
hosts: hosts hosts: hosts
become: yes
roles: roles:
- hosts - hosts
- name: Configuring hosts separately #- name: Configuring routers
hosts: {{ "{{ item.name }}" }} # hosts: routers
become: yes # roles:
loop: {{ "\"{{ config.hosts }}\"" }} # - routers
roles:
- {{ "{{ item.name }}" }}
{# TODO finish playbook #}
- name: Configuring devices separately
- name: Configuring host {{ host.host_name }} hosts: all
hosts: hosts
become: yes
tasks: tasks:
{% for network_ip in network_ips %}
- name: Add gateway for {{ network_ip }} - name: include role
command: route add -net {{ network_ip }} gw {{ host.router_ip }} {{ host.interface }} include_role:
{% endfor %} 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 #- name: Configuring border router
hosts: {{ border_router_name }} # hosts: {{ border_router_name }}
become: yes # become: yes
roles: # roles:
- br # - br
... #...
...@@ -5,6 +5,6 @@ ...@@ -5,6 +5,6 @@
apt: apt:
name: net-tools name: net-tools
- name: Delete default gateway # name: Delete default gateway
command: route del default # command: route del default
... ...
[hosts]
{% for host in hosts %}
{{ host }}
{% endfor %}
[routers]
{% for router in routers %}
{{ router }}
{% endfor %}
- name: Add aliases - name: Add aliases
loop: {{ "{{ aliases|dict2items }}" }} loop: "{{ aliases|dict2items }}"
lineinfile: lineinfile:
path: /etc/hosts path: /etc/hosts
line: {{ "{{ item.value }} \"{{ item.key }}\"" }} line: {{ item.value }} "{{ item.key }}"
...@@ -49,6 +49,19 @@ ...@@ -49,6 +49,19 @@
{{ namespace }}.{{ item.command }} = {{ item.value }} {{ namespace }}.{{ item.command }} = {{ item.value }}
{% endmacro -%} {% 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 for network items #}
{% macro network(item, namespace) %} {% macro network(item, namespace) %}
{{ namespace }}.vm.network :{{ item.network_type }}, ip: "{{ item.ip }}" {{ namespace }}.vm.network :{{ item.network_type }}, ip: "{{ item.ip }}"
...@@ -87,6 +100,8 @@ end ...@@ -87,6 +100,8 @@ end
{{ provider(item, namespace) -}} {{ provider(item, namespace) -}}
{% elif item.type == "network" %} {% elif item.type == "network" %}
{{ network(item, namespace) -}} {{ network(item, namespace) -}}
{% elif item.type == "dictionary" %}
{{ dictionary(item, namespace) -}}
{% endif %} {% endif %}
{% endfor %} {% endfor %}
end end
......
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