From 94d2a2a2a12fa0527afaa1c45440ed896c4135bf Mon Sep 17 00:00:00 2001
From: Attila Farkas <394097@mail.muni.cz>
Date: Mon, 19 Oct 2020 11:31:12 +0200
Subject: [PATCH] Configure controller to be able to provision Windows hosts

---
 modules/vagrant_generator.py   | 13 +++++++++++--
 templates/controller           | 19 +++++--------------
 templates/device_configuration | 16 ++++++++++++++--
 templates/separate_devices     |  2 +-
 templates/windows_devices      | 12 ++++--------
 5 files changed, 35 insertions(+), 27 deletions(-)

diff --git a/modules/vagrant_generator.py b/modules/vagrant_generator.py
index 1bb4dfd..2570b08 100644
--- a/modules/vagrant_generator.py
+++ b/modules/vagrant_generator.py
@@ -99,6 +99,7 @@ def _create_commands(device_attributes):
 def _create_ansible_commands(device_name, playbook_location, input_definitions, flags):
     """Create commands for running a playbook from the Vagrantfile."""
     commands = []
+    windows_hosts = _find_windows_boxes(input_definitions);
 
     playbook = dict()
     playbook["type"] = "string"
@@ -130,6 +131,8 @@ def _create_ansible_commands(device_name, playbook_location, input_definitions,
     for router in input_definitions["routers"]:
         router_names.append(router["name"])
     groups["groups"]["routers"] = router_names
+    windows_hosts = _find_windows_boxes(input_definitions)
+    groups["groups"]["windows"] = windows_hosts
     if "groups" in input_definitions:
         for group in input_definitions["groups"]:
             groups["groups"][group["name"]] = group["nodes"]
@@ -157,8 +160,14 @@ def _create_ansible_commands(device_name, playbook_location, input_definitions,
     extravars["type"] = "dictionary"
     extravars["command"] = "extra_vars"
     extravars["dictionary"] = dict()
-    extravars["dictionary"]["ansible_python_interpreter"] = \
-        "\"/usr/bin/python3\""
+    if device_name in windows_hosts:
+        extravars["dictionary"]["ansible_connection"] = "\"winrm\""
+        extravars["dictionary"]["ansible_user"] = "\"vagrant\""
+        extravars["dictionary"]["ansible_password"] = "\"vagrant\""
+        extravars["dictionary"]["ansible_winrm_transport"] = "\"basic\""
+        extravars["dictionary"]["ansible_winrm_server_cert_validation"] = "\"ignore\""
+    else:
+        extravars["dictionary"]["ansible_python_interpreter"] = "\"/usr/bin/python3\""
     commands.append(extravars)
 
     return commands
diff --git a/templates/controller b/templates/controller
index 272ee14..8351a03 100644
--- a/templates/controller
+++ b/templates/controller
@@ -19,18 +19,9 @@
   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
+- name: Install pywinrm
+  pip:
+    name: pywinrm
+    executable: "/usr/bin/pip3"
+...
diff --git a/templates/device_configuration b/templates/device_configuration
index 515b12b..1e103ef 100644
--- a/templates/device_configuration
+++ b/templates/device_configuration
@@ -19,9 +19,21 @@
       name: routers
     when: config.routers
 
-- name: Configuring devices separately
-  hosts: all
+- name: Configuring Linux devices separately
+  hosts: all:!windows
+  become: yes
+  tasks:
+  - name: include role
+    include_role:
+      name: "{{ inventory_hostname }}"
+
+- name: Configuring Windows devices separately
+  hosts: windows
   become: yes
+  become_method: runas
+  become_user: vagrant
+  vars:
+    ansible_become_pass: vagrant
   tasks:
   - name: include role
     include_role:
diff --git a/templates/separate_devices b/templates/separate_devices
index a29c071..88b2ca7 100644
--- a/templates/separate_devices
+++ b/templates/separate_devices
@@ -22,4 +22,4 @@
   loop: "{{ routes }}"
   loop_control:
     loop_var: interface
-...
\ No newline at end of file
+...
diff --git a/templates/windows_devices b/templates/windows_devices
index 1c57881..bf9673b 100644
--- a/templates/windows_devices
+++ b/templates/windows_devices
@@ -1,10 +1,6 @@
 ---
-- name: Hello world
-  hosts: all
-  tasks:
 
-    - name: print hello world
-      debug:
-        msg: "Hello World"
-
-...
\ No newline at end of file
+- name: print hello world
+  debug:
+    msg: "Hello World"
+...
-- 
GitLab