2018-08-27 14:05:45 +00:00
|
|
|
---
|
|
|
|
- pause:
|
|
|
|
prompt: |
|
|
|
|
Enter the local path to your credentials JSON file
|
|
|
|
(https://support.google.com/cloud/answer/6158849?hl=en&ref_topic=6262490#serviceaccounts)
|
|
|
|
register: _gce_credentials_file
|
|
|
|
when:
|
|
|
|
- gce_credentials_file is undefined
|
|
|
|
- lookup('env','GCE_CREDENTIALS_FILE_PATH')|length <= 0
|
|
|
|
|
|
|
|
- set_fact:
|
|
|
|
credentials_file_path: "{{ gce_credentials_file | default(_gce_credentials_file.user_input|default(None)) | default(lookup('env','GCE_CREDENTIALS_FILE_PATH'), true) }}"
|
|
|
|
ssh_public_key_lookup: "{{ lookup('file', '{{ SSH_keys.public }}') }}"
|
|
|
|
|
|
|
|
- set_fact:
|
|
|
|
credentials_file_lookup: "{{ lookup('file', '{{ credentials_file_path }}') }}"
|
|
|
|
|
|
|
|
- set_fact:
|
|
|
|
service_account_email: "{{ credentials_file_lookup.client_email | default(lookup('env','GCE_EMAIL')) }}"
|
|
|
|
project_id: "{{ credentials_file_lookup.project_id | default(lookup('env','GCE_PROJECT')) }}"
|
|
|
|
|
|
|
|
- block:
|
|
|
|
- name: Get regions
|
2019-09-28 00:10:20 +00:00
|
|
|
gcp_compute_location_info:
|
|
|
|
auth_kind: serviceaccount
|
|
|
|
service_account_file: "{{ credentials_file_path }}"
|
|
|
|
project: "{{ project_id }}"
|
|
|
|
scope: regions
|
|
|
|
filters: status=UP
|
|
|
|
register: gcp_compute_regions_info
|
2018-08-27 14:05:45 +00:00
|
|
|
|
|
|
|
- name: Set facts about the regions
|
|
|
|
set_fact:
|
|
|
|
gce_regions: >-
|
2019-09-28 00:10:20 +00:00
|
|
|
[{%- for region in gcp_compute_regions_info.resources | sort(attribute='name') -%}
|
|
|
|
'{{ region.name }}'{% if not loop.last %},{% endif %}
|
2018-08-27 14:05:45 +00:00
|
|
|
{%- endfor -%}]
|
|
|
|
|
|
|
|
- name: Set facts about the default region
|
|
|
|
set_fact:
|
|
|
|
default_region: >-
|
|
|
|
{% for region in gce_regions %}
|
2019-09-28 00:10:20 +00:00
|
|
|
{%- if region == "us-east1" %}{{ loop.index }}{% endif %}
|
2018-08-27 14:05:45 +00:00
|
|
|
{%- endfor %}
|
|
|
|
|
|
|
|
- pause:
|
|
|
|
prompt: |
|
|
|
|
What region should the server be located in?
|
2019-09-28 00:10:20 +00:00
|
|
|
(https://cloud.google.com/compute/docs/regions-zones/#locations)
|
2018-08-27 14:05:45 +00:00
|
|
|
{% for r in gce_regions %}
|
|
|
|
{{ loop.index }}. {{ r }}
|
|
|
|
{% endfor %}
|
|
|
|
|
|
|
|
Enter the number of your desired region
|
|
|
|
[{{ default_region }}]
|
|
|
|
register: _gce_region
|
|
|
|
when: region is undefined
|
|
|
|
|
2019-09-28 00:10:20 +00:00
|
|
|
- name: Set region as a fact
|
|
|
|
set_fact:
|
2018-08-27 14:05:45 +00:00
|
|
|
algo_region: >-
|
|
|
|
{% if region is defined %}{{ region }}
|
2019-04-26 15:48:28 +00:00
|
|
|
{%- elif _gce_region.user_input %}{{ gce_regions[_gce_region.user_input | int -1 ] }}
|
2018-08-27 14:05:45 +00:00
|
|
|
{%- else %}{{ gce_regions[default_region | int - 1] }}{% endif %}
|
2019-09-28 00:10:20 +00:00
|
|
|
|
|
|
|
- name: Get zones
|
|
|
|
gcp_compute_location_info:
|
|
|
|
auth_kind: serviceaccount
|
|
|
|
service_account_file: "{{ credentials_file_path }}"
|
|
|
|
project: "{{ project_id }}"
|
|
|
|
scope: zones
|
|
|
|
filters:
|
|
|
|
- "name={{ algo_region }}-*"
|
|
|
|
- "status=UP"
|
|
|
|
register: gcp_compute_zone_info
|
|
|
|
|
|
|
|
- name: Set random available zone as a fact
|
|
|
|
set_fact:
|
|
|
|
algo_zone: "{{ (gcp_compute_zone_info.resources | random(seed=algo_server_name + algo_region + project_id) ).name }}"
|