From fe68b34c0078331fbdc2de7331614f03e654a763 Mon Sep 17 00:00:00 2001 From: Attila Farkas <394097@mail.muni.cz> Date: Thu, 15 Oct 2020 14:06:10 +0200 Subject: [PATCH] Create ansible roles for Controller and Windows hosts --- modules/ansible_generator.py | 22 +++++++++++++++++++-- templates/controller | 36 ++++++++++++++++++++++++++++++++++ templates/device_configuration | 6 ------ templates/separate_devices | 4 ++++ templates/windows_devices | 10 ++++++++++ 5 files changed, 70 insertions(+), 8 deletions(-) create mode 100644 templates/controller create mode 100644 templates/windows_devices diff --git a/modules/ansible_generator.py b/modules/ansible_generator.py index 74fd818..928a943 100644 --- a/modules/ansible_generator.py +++ b/modules/ansible_generator.py @@ -4,6 +4,15 @@ from modules.file_manager import generate_file, copy_template_file,\ copy_user_provisioning_dir from modules.ansible_vars_generator import generate_ansible_vars from conf.border_router import BORDER_ROUTER_NAME +from modules.controller import CONTROLLER_NAME + +def _find_windows_boxes(definitions): + """Find and return list of host names with windows boxes.""" + windows_hosts = [] + for host in definitions["hosts"]: + if "windows" in host["base_box"]["image"].lower(): + windows_hosts.append(host["name"]) + return windows_hosts def _create_config_playbooks(input_definitions, flags): @@ -18,14 +27,23 @@ def _create_config_playbooks(input_definitions, flags): copy_template_file("routers", "base_provisioning/roles/routers/tasks/main.yml") + windows_hosts = _find_windows_boxes(input_definitions) + for device in input_definitions["hosts"] + input_definitions["routers"]: if "border_router" in flags and flags["border_router"] and\ device["name"] == BORDER_ROUTER_NAME: copy_template_file("br", "base_provisioning/roles/" + device["name"] + "/tasks/main.yml") else: - copy_template_file("separate_devices", "base_provisioning/roles/" + - device["name"] + "/tasks/main.yml") + if device["name"] == CONTROLLER_NAME: + copy_template_file("controller", "base_provisioning/roles/" + + device["name"] + "/tasks/main.yml") + elif device["name"] in windows_hosts: + copy_template_file("windows_devices", "base_provisioning/roles/" + + device["name"] + "/tasks/main.yml") + else: + copy_template_file("separate_devices", "base_provisioning/roles/" + + device["name"] + "/tasks/main.yml") def _generate_user_playbooks(input_definitions): diff --git a/templates/controller b/templates/controller new file mode 100644 index 0000000..272ee14 --- /dev/null +++ b/templates/controller @@ -0,0 +1,36 @@ +--- +- name: Adding aliases + loop: "{{ aliases | dict2items }}" + lineinfile: + path: /etc/hosts + line: "{{ item.value }} {{ item.key }}" + +- name: Configuring routes + include_role: + name: interface + vars: + interface_configuration_type: static + interface_identification: "{{ interface.interface_ip }}" + interface_static_ip: "{{ interface.interface_ip }}" + interface_static_netmask: "{{ interface.interface_netmask }}" + interface_default_gateway: "{{ interface.interface_default_gateway | default('') }}" + interface_routes: "{{ interface.interface_routes | default([]) }}" + loop: "{{ routes }}" + loop_control: + loop_var: interface + +- name: Prepare controller + hosts: controller + become: yes + tasks: + + - name: Install pywinrm + pip: + name: pywinrm + executable: "/usr/bin/pip3" + + - name: Add aliases + lineinfile: + path: /etc/hosts + line: '192.168.20.6 win' +... \ No newline at end of file diff --git a/templates/device_configuration b/templates/device_configuration index 056e198..515b12b 100644 --- a/templates/device_configuration +++ b/templates/device_configuration @@ -10,12 +10,6 @@ file: config.yml name: config -- name: Configuring hosts - hosts: hosts - become: yes - roles: - - hosts - - name: Configuring routers hosts: routers become: yes diff --git a/templates/separate_devices b/templates/separate_devices index 0c8df57..a29c071 100644 --- a/templates/separate_devices +++ b/templates/separate_devices @@ -1,4 +1,8 @@ --- +- name: Install net-tools + apt: + name: "net-tools" + - name: Adding aliases loop: "{{ aliases | dict2items }}" lineinfile: diff --git a/templates/windows_devices b/templates/windows_devices new file mode 100644 index 0000000..1c57881 --- /dev/null +++ b/templates/windows_devices @@ -0,0 +1,10 @@ +--- +- name: Hello world + hosts: all + tasks: + + - name: print hello world + debug: + msg: "Hello World" + +... \ No newline at end of file -- GitLab