main.yml 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. ---
  2. - name: Create GCP network
  3. gcp_compute_network:
  4. auth_kind: serviceaccount
  5. scopes:
  6. - https://www.googleapis.com/auth/compute
  7. service_account_file: "{{ openshift_gcp_iam_service_account_keyfile }}"
  8. project: "{{ openshift_gcp_project }}"
  9. name: "{{ openshift_gcp_network_name }}"
  10. state: present
  11. register: network
  12. - name: Create GCP firewall
  13. gcp_compute_firewall:
  14. auth_kind: serviceaccount
  15. scopes:
  16. - https://www.googleapis.com/auth/compute
  17. service_account_file: "{{ openshift_gcp_iam_service_account_keyfile }}"
  18. project: "{{ openshift_gcp_project }}"
  19. name: "{{ openshift_gcp_prefix }}{{ item.rule }}"
  20. allowed: "{{ item.allowed }}"
  21. network: "{{ network.selfLink }}"
  22. target_tags: "{{ item.target_tags | default(omit) }}"
  23. source_tags: "{{ item.source_tags | default(omit) }}"
  24. state: present
  25. with_items: "{{ openshift_gcp_firewall_rules }}"
  26. - import_tasks: provision_ssh_keys.yml
  27. - name: Find GCP image
  28. gcp_compute_image_facts:
  29. auth_kind: serviceaccount
  30. scopes:
  31. - https://www.googleapis.com/auth/compute
  32. service_account_file: "{{ openshift_gcp_iam_service_account_keyfile }}"
  33. project: "{{ openshift_gcp_project }}"
  34. filters:
  35. - "family = {{ openshift_gcp_image }}"
  36. register: gcp_node_image
  37. - fail:
  38. msg: "No images for family '{{ openshift_gcp_image }}' found"
  39. when: gcp_node_image['items'] | length == 0
  40. - name: Provision GCP instance templates
  41. gcp_compute_instance_template:
  42. auth_kind: serviceaccount
  43. scopes:
  44. - https://www.googleapis.com/auth/compute
  45. service_account_file: "{{ openshift_gcp_iam_service_account_keyfile }}"
  46. project: "{{ openshift_gcp_project }}"
  47. name: "{{ openshift_gcp_prefix }}instance-template-{{ item.name }}"
  48. properties:
  49. machine_type: "{{ item.machine_type }}"
  50. network_interfaces:
  51. - network: "{{ network }}"
  52. access_configs:
  53. - name: "{{ openshift_gcp_prefix }}instance-template-{{ item.name }}-config"
  54. type: 'ONE_TO_ONE_NAT'
  55. disks:
  56. - auto_delete: true
  57. boot: true
  58. initialize_params:
  59. disk_size_gb: "{{ item.boot_disk_size }}"
  60. source_image: "{{ gcp_node_image['items'][0].selfLink }}"
  61. metadata:
  62. "cluster-id": "{{ openshift_gcp_prefix + openshift_gcp_clusterid }}"
  63. "node-group": "{{ item.name }}"
  64. tags:
  65. items:
  66. - "ocp"
  67. - "{{ openshift_gcp_prefix }}ocp"
  68. - "{{ item.tags }}"
  69. state: present
  70. with_items: "{{ openshift_gcp_node_group_config }}"
  71. register: instance_template
  72. - name: Create GCP Instance Groups
  73. gcp_compute_instance_group_manager:
  74. auth_kind: serviceaccount
  75. scopes:
  76. - https://www.googleapis.com/auth/compute
  77. service_account_file: "{{ openshift_gcp_iam_service_account_keyfile }}"
  78. project: "{{ openshift_gcp_project }}"
  79. zone: "{{ openshift_gcp_zone }}"
  80. name: "{{ openshift_gcp_prefix }}ig-{{ item.item.suffix }}"
  81. base_instance_name: "{{ openshift_gcp_prefix }}ig-{{ item.item.suffix }}"
  82. instance_template: "{{ item }}"
  83. target_size: "{{ item.item.scale | int}}"
  84. named_ports:
  85. - name: "{{ openshift_gcp_prefix }}port-kube-api"
  86. port: "{{ openshift_gcp_kubernetes_api_port }}"
  87. - name: "{{ openshift_gcp_prefix }}port-openshift-api"
  88. port: "{{ openshift_master_api_port }}"
  89. state: present
  90. with_items: "{{ instance_template.results }}"
  91. register: instance_groups
  92. - name: Get bootstrap instance group
  93. gcp_compute_instance_group_facts:
  94. auth_kind: serviceaccount
  95. scopes:
  96. - https://www.googleapis.com/auth/compute
  97. service_account_file: "{{ openshift_gcp_iam_service_account_keyfile }}"
  98. project: "{{ openshift_gcp_project }}"
  99. zone: "{{ openshift_gcp_zone }}"
  100. filters:
  101. - name = "{{ openshift_gcp_prefix }}ig-b"
  102. register: bootstrap_instance_group
  103. - name: Get master instance group
  104. gcp_compute_instance_group_facts:
  105. auth_kind: serviceaccount
  106. scopes:
  107. - https://www.googleapis.com/auth/compute
  108. service_account_file: "{{ openshift_gcp_iam_service_account_keyfile }}"
  109. project: "{{ openshift_gcp_project }}"
  110. zone: "{{ openshift_gcp_zone }}"
  111. filters:
  112. - name = "{{ openshift_gcp_prefix }}ig-m"
  113. register: master_instance_group
  114. - set_fact:
  115. bootstrap_instance_group: "{{ bootstrap_instance_group['items'][0] }}"
  116. master_instance_group: "{{ master_instance_group['items'][0] }}"
  117. - name: Wait for bootstrap instance group to start all instances
  118. gcp_compute_instance_group_manager_facts:
  119. auth_kind: serviceaccount
  120. scopes:
  121. - https://www.googleapis.com/auth/compute
  122. service_account_file: "{{ openshift_gcp_iam_service_account_keyfile }}"
  123. project: "{{ openshift_gcp_project }}"
  124. zone: "{{ openshift_gcp_zone }}"
  125. filters: "name = {{ bootstrap_instance_group['name'] }}"
  126. register: bootstrap_group_result
  127. # Wait for 3 minutes
  128. retries: 36
  129. delay: 5
  130. until:
  131. - "bootstrap_group_result['items'][0]['currentActions']['none'] == bootstrap_group_result['items'][0]['targetSize']"
  132. - name: Wait for master instance group to start all instances
  133. gcp_compute_instance_group_manager_facts:
  134. auth_kind: serviceaccount
  135. scopes:
  136. - https://www.googleapis.com/auth/compute
  137. service_account_file: "{{ openshift_gcp_iam_service_account_keyfile }}"
  138. project: "{{ openshift_gcp_project }}"
  139. zone: "{{ openshift_gcp_zone }}"
  140. filters: "name = {{ master_instance_group['name'] }}"
  141. register: master_group_result
  142. # Wait for 3 minutes
  143. retries: 36
  144. delay: 5
  145. until:
  146. - "master_group_result['items'][0]['currentActions']['none'] == master_group_result['items'][0]['targetSize']"
  147. - name: Collect a list of instances
  148. gcp_compute_instance_facts:
  149. auth_kind: serviceaccount
  150. scopes:
  151. - https://www.googleapis.com/auth/compute
  152. service_account_file: "{{ openshift_gcp_iam_service_account_keyfile }}"
  153. project: "{{ openshift_gcp_project }}"
  154. zone: "{{ openshift_gcp_zone }}"
  155. register: all_instances
  156. - name: Filter instances to fetch bootstrap
  157. set_fact:
  158. bootstrap_instances: "{{ item }}"
  159. with_items:
  160. - "{{ all_instances['items'] }}"
  161. when:
  162. - "'tags' in item"
  163. - "'items' in item['tags']"
  164. - "cluster_tag in item['tags']['items']"
  165. - "'ocp-bootstrap' in item['tags']['items']"
  166. vars:
  167. cluster_tag: "{{ openshift_gcp_prefix }}ocp"
  168. - name: Filter instances to fetch masters
  169. set_fact:
  170. master_instances: "{{ master_instances | default([]) }} + [ {{ item }} ]"
  171. with_items:
  172. - "{{ all_instances['items'] }}"
  173. when:
  174. - "'tags' in item"
  175. - "'items' in item['tags']"
  176. - "cluster_tag in item['tags']['items']"
  177. - "'ocp-master' in item['tags']['items']"
  178. vars:
  179. cluster_tag: "{{ openshift_gcp_prefix }}ocp"
  180. - set_fact:
  181. etcd_discovery_targets: "{{ etcd_discovery_targets | default([]) }} + ['0 0 2380 {{ entry_name }}']"
  182. master_external_ips: "{{ master_external_ips | default([]) }} + ['{{ master_ip }}']"
  183. with_indexed_items: "{{ master_instances }}"
  184. vars:
  185. entry_name: "{{ openshift_gcp_prefix }}etcd-{{ item.0 }}.{{ public_hosted_zone }}."
  186. master_ip: "{{ item.1.networkInterfaces[0].accessConfigs[0].natIP }}"
  187. - set_fact:
  188. bootstrap_and_masters: "{{ master_external_ips | list }} + ['{{ bootstrap_instances.networkInterfaces[0].accessConfigs[0].natIP }}']"
  189. - name: Get managed zone
  190. gcp_dns_managed_zone:
  191. auth_kind: serviceaccount
  192. scopes:
  193. - https://www.googleapis.com/auth/ndev.clouddns.readwrite
  194. service_account_file: "{{ openshift_gcp_iam_service_account_keyfile }}"
  195. project: "{{ openshift_gcp_project }}"
  196. name: "{{ dns_managed_zone | default(openshift_gcp_prefix + 'managed-zone') }}"
  197. state: present
  198. register: managed_zone
  199. - name: Create public API hostname
  200. gcp_dns_resource_record_set:
  201. auth_kind: serviceaccount
  202. scopes:
  203. - https://www.googleapis.com/auth/ndev.clouddns.readwrite
  204. service_account_file: "{{ openshift_gcp_iam_service_account_keyfile }}"
  205. project: "{{ openshift_gcp_project }}"
  206. name: "{{ openshift_master_cluster_public_hostname }}."
  207. managed_zone: "{{ managed_zone }}"
  208. type: A
  209. ttl: 600
  210. target: "{{ bootstrap_and_masters }}"
  211. state: present
  212. - name: Create etcd records for masters
  213. gcp_dns_resource_record_set:
  214. auth_kind: serviceaccount
  215. scopes:
  216. - https://www.googleapis.com/auth/ndev.clouddns.readwrite
  217. service_account_file: "{{ openshift_gcp_iam_service_account_keyfile }}"
  218. project: "{{ openshift_gcp_project }}"
  219. name: "{{ entry_name }}"
  220. managed_zone: "{{ managed_zone }}"
  221. type: A
  222. ttl: 600
  223. target: "{{ master_ip }}"
  224. state: present
  225. with_indexed_items: "{{ master_instances }}"
  226. vars:
  227. entry_name: "{{ openshift_gcp_prefix }}etcd-{{ item.0 }}.{{ public_hosted_zone }}."
  228. master_ip: "{{ item.1.networkInterfaces[0].networkIP }}"
  229. - name: Templatize DNS script
  230. template: src=additional_settings.j2.sh dest=/tmp/additional_settings.sh mode=u+rx
  231. - name: Run addition provision GCP script
  232. command: /tmp/additional_settings.sh
  233. args:
  234. chdir: "{{ files_dir }}"