forked from redhat-cop/agnosticd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcp_infrastructure_deployment.yml
201 lines (181 loc) · 6.19 KB
/
gcp_infrastructure_deployment.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
---
- name: Step 001 Deploy Infrastructure
hosts: localhost
connection: local
gather_facts: false
become: false
tags:
- step001
- deploy_infrastructure
tasks:
- name: Ensure gcloud is installed
environment:
PATH: /usr/bin
command: which gcloud
register: gcloud_result
- name: Fail if gcloud not available
fail:
msg: you need Google Cloud SDK installed
when: gcloud_result is failed
- name: Check for auto-generated SSH Key
stat:
path: "{{output_dir}}/{{env_authorized_key}}"
register: env_authorized_key_status
tags:
- check_for_env_keys
- name: Get SSH public key
set_fact:
ssh_key: "~/.ssh/{{key_name}}.pem"
ssh_key_data: "{{lookup('file', '~/.ssh/{{key_name}}.pub')}}"
tags:
- set_existing_ssh_key
- must
- create_inventory
when: not env_authorized_key_status.stat.exists
- name: Get SSH public key
set_fact:
ssh_key: "{{output_dir}}/{{env_authorized_key}}"
ssh_key_data: "{{lookup('file', '{{output_dir}}/{{env_authorized_key}}.pub')}}"
tags:
- set_generated_ssh_key
- must
- create_inventory
when: env_authorized_key_status.stat.exists
- name: Set the destination for the template
set_fact:
t_dest: "{{output_dir}}/{{ env_type }}.{{ guid }}.{{cloud_provider}}_cloud_template"
tags:
- gcp_infrastructure_deployment
- validate_gcp_template
- gen_gcp_template
- name: Generate GCP deployment manager template
template:
src: "../configs/{{ env_type }}/files/cloud_providers/{{cloud_provider}}_cloud_template.j2"
dest: "{{t_dest}}"
tags:
- gcp_infrastructure_deployment
- validate_gcp_template
- gen_gcp_template
- name: Starting Google deployment-manager with template
environment:
PATH: /usr/bin
CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE: "{{ gcp_credentials_file }}"
CLOUDSDK_COMPUTE_REGION: "{{ gcp_region }}"
CLOUDSDK_CONFIG: "{{ output_dir }}/.gcloud-{{ guid }}"
CLOUDSDK_CORE_PROJECT: "{{ gcp_project_id }}"
command: >-
gcloud deployment-manager deployments
create "{{ project_tag }}-base"
--config "{{ t_dest }}"
register: gcp_deploy
tags:
- gcp_infrastructure_deployment
- gcp_deployment_manager
until: gcp_deploy is succeeded
retries: 0
- debug:
var: gcp_deploy
verbosity: 2
tags:
- gcp_infrastructure_deployment
- name: Run infra-gcp-create-inventory Role
import_role:
name: infra-gcp-create-inventory
- name: Fetch DNS zone Info
gcp_dns_managed_zone_facts:
dns_name: '{{ cluster_dns_zone + "."}}'
project: "{{ gcp_project_id }}"
auth_kind: "{{ gcp_auth_type }}"
service_account_file: "{{ gcp_credentials_file }}"
register: gcp_managed_zone
tags:
- gcp_infrastructure_deployment
when:
- cluster_dns_zone is defined
- name: Add delegation for NS to the main DNSZone
gcp_dns_resource_record_set:
project: "{{ gcp_project_id }}"
auth_kind: "{{ gcp_auth_type }}"
service_account_file: "{{ gcp_credentials_file }}"
managed_zone: "{{ gcp_managed_zone.resources[0] }}"
name: '{{ guid + "." + cluster_dns_zone + "."}}'
type: NS
ttl: 600
target:
- "{{ gcp_managed_zone.resources[0].nameServers[0] }}"
- "{{ gcp_managed_zone.resources[0].nameServers[1] }}"
- "{{ gcp_managed_zone.resources[0].nameServers[2] }}"
- "{{ gcp_managed_zone.resources[0].nameServers[3] }}"
state: present
tags:
- gcp_infrastructure_deployment
when:
- env_type != "ocp4-cluster"
- HostedZoneId != "none"
- name: Add bastion entry to the main DNSZone
gcp_dns_resource_record_set:
project: "{{ gcp_project_id }}"
auth_kind: "{{ gcp_auth_type }}"
service_account_file: "{{ gcp_credentials_file }}"
managed_zone: "{{ gcp_managed_zone.resources[0] }}"
name: '{{ "bastion." + guid + "." + cluster_dns_zone + "."}}'
type: A
ttl: 300
target:
- '{{ hostvars[item].public_ip_address }}'
state: present
with_items: "{{ groups['bastions'] }}"
tags:
- gcp_infrastructure_deployment
- gcp_set_bastion_dns
when:
- env_type == "ocp4-cluster"
- groups["bastions"] is defined
# Copy env_vars variables from the config to all hosts
- import_playbook: ../include_vars.yml
# TODO: use common infra role instead of this playbook
- name: Configure local ssh config for bastion proxy use
import_playbook: "{{cloud_provider}}_ssh_config_setup.yml"
when: groups["bastions"] is defined and (groups["bastions"]|length>0)
tags:
- must
- create_inventory
- name: wait_for_connection for all non-windows machines and set hostname
hosts:
- all:!windows:!network
gather_facts: false
become: true
tags:
- step001
- wait_ssh
- set_hostname
tasks:
- name: wait for linux host to be available
wait_for_connection:
timeout: 300
register: rwait
ignore_errors: true
- name: restart instance if wait_for_connection failed
become: false
command: "gcloud compute instances reset '{{inventory_hostname}}' --zone '{{ gcp_zone }}'"
delegate_to: localhost
environment:
CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE: "{{ gcp_credentials_file }}"
CLOUDSDK_COMPUTE_REGION: "{{ gcp_region }}"
CLOUDSDK_CONFIG: "{{ output_dir }}/.gcloud-{{ guid }}"
CLOUDSDK_CORE_PROJECT: "{{ gcp_project_id }}"
when: rwait is failed
- name: wait for linux host to be available (retry)
wait_for_connection:
when: rwait is failed
- ping:
register: rping
retries: 3
delay: 10
until: rping is succeeded
- name: Populate /etc/hosts
lineinfile:
dest: /etc/hosts
regexp: ' {{hostvars[item].internaldns}}$'
line: '{{hostvars[item].private_ip_address}} {{hostvars[item].internaldns}}'
with_items: "{{ groups['all'] }}"