diff --git a/modules/file_generator.py b/modules/file_generator.py index a138068590652601bd4d3f23190a468b1a24730c..d73861e69095902cdff418c15719fe3962cbd528 100644 --- a/modules/file_generator.py +++ b/modules/file_generator.py @@ -23,23 +23,23 @@ def _generate_file(filename, output_string): print("Error: cannot write to this location.") -def _create_role_directory(role_name): +def _create_role_directory(role_name, provisioning_dir): """ Creates directory structure for a role. """ try: - os.mkdir("provisioning") + os.mkdir(provisioning_dir) except FileExistsError: pass try: - os.mkdir("provisioning/roles") + os.mkdir(provisioning_dir + "/roles") except FileExistsError: pass try: - os.mkdir("provisioning/roles/" + role_name) + os.mkdir(provisioning_dir + "/roles/" + role_name) except FileExistsError: pass try: - os.mkdir("provisioning/roles/" + role_name +"/tasks") + os.mkdir(provisioning_dir + "/roles/" + role_name +"/tasks") except FileExistsError: pass @@ -71,10 +71,9 @@ def _generate_playbook(definitions): host_map = create_host_map(definitions["net_mappings"], definitions["router_mappings"], definitions["hosts"]) network = create_network_map(definitions) - network_ips = create_network_ips(definitions["networks"]) template = _load_template("playbook") - output = template.render(hosts=host_map, routers=network, network_ips=network_ips, border_router_name = BORDER_ROUTER_NAME) + output = template.render(hosts=host_map, routers=network) try: os.mkdir("provisioning") @@ -92,14 +91,14 @@ def _generate_device_configuration(definitions): network_ips = create_network_ips(definitions["networks"]) template = _load_template("device_configuration") - output = template.render(hosts=host_map, routers=network, network_ips=network_ips) + output = template.render(hosts=host_map, routers=network, network_ips=network_ips, border_router_name = BORDER_ROUTER_NAME) try: - os.mkdir("provisioning") + os.mkdir("base_provisioning") except FileExistsError: pass - _generate_file("./provisioning/device_configuration.yml", output) + _generate_file("./base_provisioning/device_configuration.yml", output) def _generate_hosts_role(definitions): @@ -112,7 +111,13 @@ def _generate_hosts_role(definitions): template = _load_template("hosts") output = template.render(hosts=host_map, routers=network) - _create_role_directory("hosts") + _create_role_directory("hosts", "base_provisioning") + _generate_file("./base_provisioning/roles/hosts/tasks/main.yml", output) + + user_template = _load_template("user_hosts") + user_output = template.render() + + _create_role_directory("hosts", "provisioning") _generate_file("./provisioning/roles/hosts/tasks/main.yml", output) @@ -125,13 +130,21 @@ def _generate_separate_hosts_role(definitions): for host_attributes in host_map: if host_attributes["host_name"] == host["name"]: + host_name = host_attributes["host_name"] router_ip = host_attributes["router_ip"] interface = host_attributes["interface"] template = _load_template("separate_hosts") - output = template.render(router_ip=router_ip, interface=interface) + output = template.render(host_name=host_name, router_ip=router_ip, interface=interface) + + _create_role_directory(host["name"], "base_provisioning") + _generate_file("./base_provisioning/roles/" + host["name"] + "/tasks/main.yml", output) + + + template = _load_template("user_separate_hosts") + output = template.render(host_name=host_name) - _create_role_directory(host["name"]) + _create_role_directory(host["name"], "provisioning") _generate_file("./provisioning/roles/" + host["name"] + "/tasks/main.yml", output) def _generate_routers_role(definitions): @@ -148,8 +161,8 @@ def _generate_routers_role(definitions): template = _load_template("routers") output = template.render(hosts=host_map, routers=network, border_router_ip=BORDER_ROUTER_IP) - _create_role_directory("routers") - _generate_file("./provisioning/roles/routers/tasks/main.yml", output) + _create_role_directory("routers", "base_provisioning") + _generate_file("./base_provisioning/roles/routers/tasks/main.yml", output) def _find_cidr(network_name, definitions): @@ -187,16 +200,15 @@ def _generate_br_role(definitions): template = _load_template("br") output = template.render(hosts = host_map, routers=network, br_routes=routers_in_br_network, border_router_name=BORDER_ROUTER_NAME, border_router_public_ip=BORDER_ROUTER_PUBLIC_IP) - _create_role_directory("br") - _generate_file("./provisioning/roles/br/tasks/main.yml", output) + _create_role_directory("br", "base_provisioning") + _generate_file("./base_provisioning/roles/br/tasks/main.yml", output) def generate_ansible_files(device_definitions): """ Generates files for ansible. """ _generate_playbook(device_definitions) - # uncomment after the new version of ansible can be used - # _generate_device_configuration(device_definitions) + _generate_device_configuration(device_definitions) _generate_hosts_role(device_definitions) _generate_separate_hosts_role(device_definitions) _generate_routers_role(device_definitions) diff --git a/templates/device_configuration b/templates/device_configuration index 73a852fe23a6ad5db658fbe1de3a385b43a7718b..e8ba8322319433a12718201c9e7823f43b14a95a 100644 --- a/templates/device_configuration +++ b/templates/device_configuration @@ -7,18 +7,34 @@ roles: - hosts +{% for host in hosts %} +- name: Configuring host {{ host.host_name }} separately + hosts: {{ host.host_name }} + become: yes + roles: + - {{ host.host_name }} + +{% endfor %} {% for host in hosts %} - name: Configuring host {{ host.host_name }} hosts: {{ host.host_name }} become: yes tasks: - - name: Change default gateway - command: route add default gw {{ host.router_ip }} {{ host.interface }} +{% 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 %} {% endfor %} - name: Configuring all routers - hosts: {{ routers|map(attribute='router_name')|unique|join(',') }} + 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 ... diff --git a/templates/hosts b/templates/hosts index 4317ae01aaf3be2b6121e8c16eef39e31b6944e1..e9254f11c31650636be65efaf1f4cdc53326191e 100644 --- a/templates/hosts +++ b/templates/hosts @@ -1,5 +1,5 @@ --- -# Configuration of all host devices +# Basic configuration of all host devices - name: Install net-tools command: apt install net-tools diff --git a/templates/playbook b/templates/playbook index 93ee8e79c1ddf943a494adf3f1c6a671bf325fae..7f3e1c7089a960a7da5b952b974f818a23081acf 100644 --- a/templates/playbook +++ b/templates/playbook @@ -1,47 +1,12 @@ --- -# Main ansible playbook - -#- import_playbook: device_configuration.yml - for new version - -- name: Configuring all hosts - hosts: {{ hosts|map(attribute='host_name')|unique|join(',') }} - become: yes - roles: - - hosts - -{% for host in hosts %} -- name: Configuring host {{ host.host_name }} separately - hosts: {{ host.host_name }} - become: yes - roles: - - {{ host.host_name }} - -{% endfor %} -{% for host in hosts %} -- name: Configuring host {{ host.host_name }} - hosts: {{ host.host_name }} - 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 %} - - -{% 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 - +# Main user ansible playbook # Write your custom configuration here: +- name: Hello world + hosts: all + tasks: + - name: print hello world + debug: + msg: "Hello World" ... diff --git a/templates/separate_hosts b/templates/separate_hosts index 3bf700488dcaea670f49ba192417276d9057731b..42e3f8076cb7cd468c1bf1cfb87f295d4ddbac46 100644 --- a/templates/separate_hosts +++ b/templates/separate_hosts @@ -1,4 +1,6 @@ --- +# Role for the host {{ host_name }} + - name: Add default path to router command: route add default gw {{ router_ip }} {{ interface }} ... diff --git a/templates/user_hosts b/templates/user_hosts new file mode 100644 index 0000000000000000000000000000000000000000..0dccf70c06e2ec8580828c2c8c6693e870030b59 --- /dev/null +++ b/templates/user_hosts @@ -0,0 +1,8 @@ +--- +# This is a role for all hosts. +# You can write your tasks here. +# These changes will affect all hosts. + + + +... diff --git a/templates/user_separate_hosts b/templates/user_separate_hosts new file mode 100644 index 0000000000000000000000000000000000000000..f49a8bafc9297f21682d4a170677d597336f6544 --- /dev/null +++ b/templates/user_separate_hosts @@ -0,0 +1,8 @@ +--- +# This is a role for the host {{ host_name }}. +# You can write your tasks here. +# These changes will affect only the host {{ host_name }}. + + + +... diff --git a/templates/vagrantfile b/templates/vagrantfile index e51a3ae6f2961d4e9ddbad1b411462a7b09ba189..946446c71fc08127a3026740579c095125186d20 100644 --- a/templates/vagrantfile +++ b/templates/vagrantfile @@ -19,7 +19,17 @@ Vagrant.configure("2") do |config| {{ printAttributes(name) }} end {% endfor %} - # configuration of devices with ansible + # basic ansible configuration of devices and networks + config.vm.provision :ansible{% if ansible_local %}_local{% endif %} do |ansible| + ansible.playbook = "base_provisioning/device_configuration.yml" + ansible.verbose = true + ansible.extra_vars = { + ansible_python_interpreter: "/usr/bin/python3", + } + end + + + # user configuration of devices with ansible config.vm.provision :ansible{% if ansible_local %}_local{% endif %} do |ansible| ansible.playbook = "provisioning/playbook.yml" ansible.verbose = true