diff --git a/modules/ansible_generator.py b/modules/ansible_generator.py index 94a7b951ff58e7eff669e9e6e6da91fa39b0d43c..fd53bde2aae182ae6090055db1a841ab6ff17840 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 0000000000000000000000000000000000000000..473bea92d67be268b9d915dead91af026b2ddad8 --- /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 e8ba8322319433a12718201c9e7823f43b14a95a..4acf99f13188a14b3b17f94eb7d21a5966ca4e8f 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 7f3e1c7089a960a7da5b952b974f818a23081acf..2fe3cd9f7613d46390332339c044d12c74b3b36a 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 0dccf70c06e2ec8580828c2c8c6693e870030b59..c79ff48449939fcf1fe5406771c6d21cbbc846f1 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 0000000000000000000000000000000000000000..00eb86eec6c3124109e067240692ae0085cf78cb --- /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 f49a8bafc9297f21682d4a170677d597336f6544..25921c0776e23af7285a5f5fb20574ecc9fb4f37 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 0000000000000000000000000000000000000000..c92c2bd41008a10d5710bb1127eb092c1111054b --- /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. + + + +...