Commit a097e8dc authored by Attila Farkas's avatar Attila Farkas
Browse files

add ansible_local support and update readme

parent e3a4ffb3
......@@ -16,7 +16,7 @@ create.py is a python program that generates a vagrant source file from a defini
2. Install Python dependencies with `pip3 install -r requirements.txt`.
3. Clone the project.
4. Navigate to the project folder.
5. Type `$ python3 create.py sandbox.yml`.
5. Type `$ python3 create.py sandbox.yml`. On Windows or in case of an error due to ansible/python use `python3 create.py -l sandbox.yml`. (note: This will rewrite the previously generated Vagrantfile and ansible files.)
6. Run `$ vagrant up`
### Input yaml file structure
......
......@@ -8,13 +8,25 @@ from modules.file_generator import generate_vagrantfile, generate_ansible_files
from modules.device_creator import open_file
from modules.routing import create_border_router
if len(sys.argv) != 2:
print("Error: Expecting 1 argument (yml file).")
if len(sys.argv) == 3:
if str(sys.argv[1]) == "-l":
ansible_local = True
input_file_name = str(sys.argv[2])
elif str(sys.argv[2]) == "-l":
ansible_local = True
input_file_name = str(sys.argv[1])
else:
print("Error: Expecting a yml file and optionally a flag -l.")
sys.exit()
elif len(sys.argv) == 2:
ansible_local = False
input_file_name = str(sys.argv[1])
else:
print("Error: Expecting a yml file and optionally a flag -l.")
sys.exit()
device_definitions = open_file(str(sys.argv[1]))
device_definitions = open_file(input_file_name)
create_border_router(device_definitions)
generate_vagrantfile(device_definitions)
generate_vagrantfile(device_definitions, ansible_local)
generate_ansible_files(device_definitions)
......@@ -46,19 +46,19 @@ def _create_hosts(yml, mappings, flavors):
return host_definitions
def _create_routers(yml):
def _create_routers(yml, ansible_local):
""" Creates a dictionary with formatted definition of each router. """
router_definitions = {}
for router in yml['routers']:
router_definitions[router['name']] = []
add_router_ip(router["name"], yml, router_definitions)
add_router_specification(router, router_definitions)
add_router_specification(router, router_definitions, ansible_local)
return router_definitions
def create_devices(definitions):
def create_devices(definitions, ansible_local):
""" Returns a merged dictionary of host and router definitions. """
mappings = open_file(MAPPING_FILE)
......@@ -66,4 +66,4 @@ def create_devices(definitions):
return {
**_create_hosts(definitions, mappings, flavors),
**_create_routers(definitions)}
**_create_routers(definitions, ansible_local)}
......@@ -54,13 +54,13 @@ def _find_user_ansible_files(definitions):
return host_names
def generate_vagrantfile(definitions):
def generate_vagrantfile(definitions, ansible_local):
""" Writes the prepared output to a Vagrantfile. """
device_definitions = create_devices(definitions)
device_definitions = create_devices(definitions, ansible_local)
user_ansible_files = _find_user_ansible_files(definitions)
template = _load_template("vagrantfile")
output = template.render(devices=device_definitions, user_files=user_ansible_files)
output = template.render(devices=device_definitions, user_files=user_ansible_files, ansible_local=ansible_local)
_generate_file("Vagrantfile", output)
print("Info: Vagrantfile successfully created.")
......
......@@ -48,7 +48,7 @@ def add_prov_attributes(host, flavors, provider_attributes, definitions):
definitions[host['name']].append("end")
def add_router_specification(router, definitions):
def add_router_specification(router, definitions, ansible_local):
""" Adds the default specification for a router. """
router_box = "generic/debian10"
......@@ -62,3 +62,6 @@ def add_router_specification(router, definitions):
"device.vm.provider \"virtualbox\" do |vb|")
definitions[router['name']].append(" vb.memory = " + str(router_memory))
definitions[router['name']].append("end")
if ansible_local:
definitions[router['name']].append(
"config.vm.synced_folder \".\", \"/vagrant\", type: \"rsync\", rsync__exclude: \".git/\"")
......@@ -20,7 +20,7 @@ Vagrant.configure("2") do |config|
{% endfor %}
# configuration of devices with ansible
config.vm.provision "ansible" do |ansible|
config.vm.provision :ansible{% if ansible_local %}_local{% endif %} do |ansible|
ansible.playbook = "provisioning/playbook.yml"
ansible.verbose = true
ansible.extra_vars = {
......@@ -29,7 +29,7 @@ Vagrant.configure("2") do |config|
end
{% for name in user_files %}
config.vm.provision "ansible" do |ansible|
config.vm.provision :ansible{% if ansible_local %}_local{% endif %} do |ansible|
ansible.playbook = "provisioning/{{ name }}.yml"
ansible.verbose = true
ansible.extra_vars = {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment