diff --git a/requirements.txt b/requirements.txt index 38f36dac..9f178056 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ ansible==2.5.2 +jmespath==0.9.4 \ No newline at end of file diff --git a/roles/cloud-cloudstack/tasks/main.yml b/roles/cloud-cloudstack/tasks/main.yml index 43266d4d..100e6ae8 100644 --- a/roles/cloud-cloudstack/tasks/main.yml +++ b/roles/cloud-cloudstack/tasks/main.yml @@ -6,6 +6,12 @@ - block: - name: Include prompts import_tasks: prompts.yml + + - set_fact: + algo_region: >- + {% if region is defined %}{{ region }} + {%- elif _algo_region.user_input is defined and _algo_region.user_input != "" %}{{ cs_zones[_algo_region.user_input | int -1 ] }} + {%- else %}{{ cs_zones[default_region | int - 1] }}{% endif %} - name: Security group created local_action: @@ -50,7 +56,7 @@ template: "{{ image_id }}" ssh_key: "{{ keypair_name }}" security_groups: "{{ cs_security_group.name }}" - zone: "{{ _algo_region.user_input }}" + zone: "{{ algo_region }}" register: cs_server - set_fact: diff --git a/roles/cloud-cloudstack/tasks/prompts.yml b/roles/cloud-cloudstack/tasks/prompts.yml index 116fb4df..e16c48bb 100644 --- a/roles/cloud-cloudstack/tasks/prompts.yml +++ b/roles/cloud-cloudstack/tasks/prompts.yml @@ -1,7 +1,30 @@ --- - block: + - name: List zones on configured cloud provider + local_action: shell "{{ cloudstack_venv }}/bin/cs" listZones + register: cs_zones_list + + - name: Parse zones from output + set_fact: + cs_zones: "{{cs_zones_list.stdout | from_json | json_query('zone[*].name')}}" + + - name: Set the default zone + set_fact: + default_zone: >- + {% for z in cs_zones %} + {%- if z == "ch-gva-2" %}{{ loop.index }}{% endif %} + {%- endfor %} + - pause: prompt: | - What region should the server be located in? + What zone should the server be located in? + {% for z in cs_zones %} + {{ loop.index }}. {{ z }} + {% endfor %} + + Enter the number of your desired zone + [{{ default_zone }}] register: _algo_region when: region is undefined + environment: + PYTHONPATH: "{{ cloudstack_venv }}/lib/python2.7/site-packages/"