From 6bc615ba15a0cdade874f6554daf2ca88814b06c Mon Sep 17 00:00:00 2001
From: Attila Farkas <x394097@fi.muni.cz>
Date: Mon, 16 Mar 2020 11:11:22 +0100
Subject: [PATCH] add template user playbooks and roles

---
 modules/ansible_generator.py    | 52 +++++++++++++++++++++++++++++++++
 templates/config                |  9 ++++++
 templates/device_configuration  | 16 ++++++----
 templates/playbook              |  2 +-
 templates/user_hosts            |  1 -
 templates/user_routers          |  7 +++++
 templates/user_separate_hosts   |  1 -
 templates/user_separate_routers |  7 +++++
 8 files changed, 86 insertions(+), 9 deletions(-)
 create mode 100644 templates/config
 create mode 100644 templates/user_routers
 create mode 100644 templates/user_separate_routers

diff --git a/modules/ansible_generator.py b/modules/ansible_generator.py
index 94a7b95..fd53bde 100644
--- a/modules/ansible_generator.py
+++ b/modules/ansible_generator.py
@@ -16,6 +16,55 @@ def _create_inventory(input_definitions):
     generate_file("provisioning/inventory.ini", output)
 
 
+def _create_config(input_definitions, flags):
+    """ Creates a file with common variables for all roles. """
+
+    hosts = []
+    for host in input_definitions["hosts"]:
+        new_host = dict()
+        new_host["name"] = host["name"]
+        hosts.append(new_host)
+
+    routers = []
+    for router in input_definitions["routers"]:
+        new_router = dict()
+        new_router["name"] = router["name"]
+        routers.append(new_router)
+    
+    config_template = load_template("config")
+    output = config_template.render(hosts=hosts, routers=routers)
+    generate_file("base_provisioning/config.yml", output)
+    
+def _create_config_playbooks(input_definitions, flags):
+    """ Generates playbooks and roles for basic device configuration. """
+    # TODO create playbooks
+    return
+
+
+def _create_user_playbooks(input_definitions):
+    """ Generates template playbooks and roles for users. """
+
+    playbook_template = load_template("playbook")
+    generate_file("provisioning/playbook.yml", playbook_template.render())
+
+    user_hosts_template = load_template("user_hosts")
+    generate_file("provisioning/roles/hosts/tasks/main.yml", user_hosts_template.render())
+
+    user_routers_template = load_template("user_routers")
+    generate_file("provisioning/roles/routers/tasks/main.yml", user_routers_template.render())
+
+    user_host_template = load_template("user_separate_hosts")
+    for host in input_definitions["hosts"]:
+        output = user_host_template.render(host_name=host["name"])
+        generate_file("provisioning/roles/" + host["name"] + "/tasks/main.yml", output)
+
+    user_router_template = load_template("user_separate_routers")
+    for router in input_definitions["routers"]:
+        output = user_router_template.render(router_name=router["name"])
+        generate_file("provisioning/roles/" + router["name"] + "/tasks/main.yml", output)
+
+
+
 def generate_playbooks(input_definitions, flags):
     """ Generates ansible playbooks.
 
@@ -24,3 +73,6 @@ def generate_playbooks(input_definitions, flags):
     """
 
     _create_inventory(input_definitions)
+    _create_config(input_definitions, flags)
+    _create_config_playbooks(input_definitions, flags)
+    _create_user_playbooks(input_definitions)
diff --git a/templates/config b/templates/config
new file mode 100644
index 0000000..473bea9
--- /dev/null
+++ b/templates/config
@@ -0,0 +1,9 @@
+hosts:
+  {% for host in hosts %}
+  - name: {{ host.name }}
+  {% endfor %}
+
+routers:
+  {% for router in routers %}
+  - name: {{ router.name }}
+  {% endfor %}
diff --git a/templates/device_configuration b/templates/device_configuration
index e8ba832..4acf99f 100644
--- a/templates/device_configuration
+++ b/templates/device_configuration
@@ -2,19 +2,23 @@
 # Basic configuration of all defined devices
 
 - name: Configuring all hosts
-  hosts: {{ hosts|map(attribute='host_name')|unique|join(',') }}
+  hosts: hosts
   become: yes
   roles:
     - hosts
 
-{% for host in hosts %}
-- name: Configuring host {{ host.host_name }} separately
-  hosts: {{ host.host_name }}
+- name: Configuring host {{ item.name }} separately
+  hosts: {{ item.name }}
   become: yes
+  loop: "{{ hosts }}"
   roles:
-    - {{ host.host_name }}
+    - {{ item.name }}
+
+
+{# TODO finish playbook #}
+
+
 
-{% endfor %}
 {% for host in hosts %}
 - name: Configuring host {{ host.host_name }}
   hosts: {{ host.host_name }}
diff --git a/templates/playbook b/templates/playbook
index 7f3e1c7..2fe3cd9 100644
--- a/templates/playbook
+++ b/templates/playbook
@@ -1,5 +1,5 @@
 ---
-# Main user ansible playbook
+# Main user ansible playbook.
 # Write your custom configuration here:
 
 - name: Hello world
diff --git a/templates/user_hosts b/templates/user_hosts
index 0dccf70..c79ff48 100644
--- a/templates/user_hosts
+++ b/templates/user_hosts
@@ -1,7 +1,6 @@
 ---
 # This is a role for all hosts.
 # You can write your tasks here.
-# These changes will affect all hosts.
 
 
 
diff --git a/templates/user_routers b/templates/user_routers
new file mode 100644
index 0000000..00eb86e
--- /dev/null
+++ b/templates/user_routers
@@ -0,0 +1,7 @@
+---
+# This is a role for all routers.
+# You can write your tasks here.
+
+
+
+...
diff --git a/templates/user_separate_hosts b/templates/user_separate_hosts
index f49a8ba..25921c0 100644
--- a/templates/user_separate_hosts
+++ b/templates/user_separate_hosts
@@ -1,7 +1,6 @@
 ---
 # 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/user_separate_routers b/templates/user_separate_routers
new file mode 100644
index 0000000..c92c2bd
--- /dev/null
+++ b/templates/user_separate_routers
@@ -0,0 +1,7 @@
+---
+# This is a role for the router {{ router_name }}.
+# You can write your tasks here.
+
+
+
+...
-- 
GitLab