From c65ee01b15bdabf1c905fbf7d014722b554b0743 Mon Sep 17 00:00:00 2001
From: Attila Farkas <ati@mail.muni.cz>
Date: Wed, 16 Dec 2020 12:27:18 +0100
Subject: [PATCH] Move ansible windows attributes to groups

---
 modules/vagrant_generator.py | 16 +++++++++-------
 templates/vagrantfile        | 19 +++++++++++++++++--
 2 files changed, 26 insertions(+), 9 deletions(-)

diff --git a/modules/vagrant_generator.py b/modules/vagrant_generator.py
index 82f8ee1..83f5757 100644
--- a/modules/vagrant_generator.py
+++ b/modules/vagrant_generator.py
@@ -133,6 +133,14 @@ def _create_ansible_commands(device_name, playbook_location, input_definitions,
     groups["groups"]["routers"] = router_names
     windows_hosts = _find_windows_boxes(input_definitions)
     groups["groups"]["windows"] = windows_hosts
+    if device_name in windows_hosts:
+        windows_vars = dict()
+        windows_vars["ansible_connection"] = "\"winrm\""
+        windows_vars["ansible_user"] = "\"windows\""
+        windows_vars["ansible_password"] = "\"vagrant\""
+        windows_vars["ansible_winrm_transport"] = "\"basic\""
+        windows_vars["ansible_winrm_server_cert_validation"] = "\"ignore\""
+        groups["groups"]["windows:vars"] = windows_vars
     if "groups" in input_definitions:
         for group in input_definitions["groups"]:
             groups["groups"][group["name"]] = group["nodes"]
@@ -160,13 +168,7 @@ def _create_ansible_commands(device_name, playbook_location, input_definitions,
     extravars["type"] = "dictionary"
     extravars["command"] = "extra_vars"
     extravars["dictionary"] = dict()
-    if device_name in windows_hosts:
-        extravars["dictionary"]["ansible_connection"] = "\"winrm\""
-        extravars["dictionary"]["ansible_user"] = "\"windows\""
-        extravars["dictionary"]["ansible_password"] = "\"vagrant\""
-        extravars["dictionary"]["ansible_winrm_transport"] = "\"basic\""
-        extravars["dictionary"]["ansible_winrm_server_cert_validation"] = "\"ignore\""
-    else:
+    if device_name not in windows_hosts:
         extravars["dictionary"]["ansible_python_interpreter"] = "\"/usr/bin/python3\""
     commands.append(extravars)
 
diff --git a/templates/vagrantfile b/templates/vagrantfile
index ea7a09f..d7e09d9 100644
--- a/templates/vagrantfile
+++ b/templates/vagrantfile
@@ -71,14 +71,29 @@
       }
 {% endmacro -%}
 
+{# Macro for group dictionaries #}
+{% macro group_dict(item, namespace) %}{
+  {% for key, value in item.items() %}
+      {% if loop.last %}
+          "{{ key }}" => {{ value }}
+      {% else %}
+          "{{ key }}" => {{ value }},
+    {% endif %}
+  {% endfor %}
+        }{% endmacro -%}
+
 {# Macro for dictionaries #}
 {% macro groups(item, namespace) %}
   {{ namespace }}.groups = {
   {% for key, value in item.groups.items() %}
-    {% if loop.last %}
-        "{{ key }}" => {{ value }}
+    {% if value is mapping %}
+        "{{ key }}" => {{ group_dict(value, namespace) }}
     {% else %}
+      {% if loop.last %}
+        "{{ key }}" => {{ value }}
+      {% else %}
         "{{ key }}" => {{ value }},
+      {% endif %}
     {% endif %}
   {% endfor %}
       }
-- 
GitLab