launch_instances.yml 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. ---
  2. - set_fact:
  3. created_by: "{{ lookup('env', 'LOGNAME')|default(cluster, true) }}"
  4. docker_vol_ephemeral: "{{ lookup('env', 'os_docker_vol_ephemeral') | default(false, true) }}"
  5. cluster: "{{ cluster_id }}"
  6. env: "{{ cluster_env }}"
  7. host_type: "{{ type }}"
  8. sub_host_type: "{{ g_sub_host_type }}"
  9. - set_fact:
  10. ec2_region: "{{ lookup('env', 'ec2_region')
  11. | default(deployment_vars[deployment_type].region, true) }}"
  12. when: ec2_region is not defined
  13. - set_fact:
  14. ec2_image_name: "{{ lookup('env', 'ec2_image_name')
  15. | default(deployment_vars[deployment_type].image_name, true) }}"
  16. when: ec2_image_name is not defined and ec2_image is not defined
  17. - set_fact:
  18. ec2_image: "{{ lookup('env', 'ec2_image')
  19. | default(deployment_vars[deployment_type].image, true) }}"
  20. when: ec2_image is not defined and not ec2_image_name
  21. - set_fact:
  22. ec2_keypair: "{{ lookup('env', 'ec2_keypair')
  23. | default(deployment_vars[deployment_type].keypair, true) }}"
  24. when: ec2_keypair is not defined
  25. - set_fact:
  26. ec2_vpc_subnet: "{{ lookup('env', 'ec2_vpc_subnet')
  27. | default(deployment_vars[deployment_type].vpc_subnet, true) }}"
  28. when: ec2_vpc_subnet is not defined
  29. - set_fact:
  30. ec2_assign_public_ip: "{{ lookup('env', 'ec2_assign_public_ip')
  31. | default(deployment_vars[deployment_type].assign_public_ip, true) }}"
  32. when: ec2_assign_public_ip is not defined
  33. - set_fact:
  34. ec2_instance_type: "{{ ec2_master_instance_type | default(lookup('env', 'ec2_master_instance_type') | default(lookup('env', 'ec2_instance_type') | default(deployment_vars[deployment_type].type, true), true), true) }}"
  35. ec2_security_groups: "{{ ec2_master_security_groups | default(lookup('env', 'ec2_master_security_groups') | default(lookup('env', 'ec2_security_groups') | default(deployment_vars[deployment_type].security_groups, true), true), true) }}"
  36. when: host_type == "master" and sub_host_type == "default"
  37. - set_fact:
  38. ec2_instance_type: "{{ ec2_etcd_instance_type | default(lookup('env', 'ec2_etcd_instance_type') | default(lookup('env', 'ec2_instance_type') | default(deployment_vars[deployment_type].type, true), true), true) }}"
  39. ec2_security_groups: "{{ ec2_etcd_security_groups | default(lookup('env', 'ec2_etcd_security_groups') | default(lookup('env', 'ec2_security_groups') | default(deployment_vars[deployment_type].security_groups, true), true), true) }}"
  40. when: host_type == "etcd" and sub_host_type == "default"
  41. - set_fact:
  42. ec2_instance_type: "{{ ec2_infra_instance_type | default(lookup('env', 'ec2_infra_instance_type') | default(lookup('env', 'ec2_instance_type') | default(deployment_vars[deployment_type].type, true), true), true) }}"
  43. ec2_security_groups: "{{ ec2_infra_security_groups | default(lookup('env', 'ec2_infra_security_groups') | default(lookup('env', 'ec2_security_groups') | default(deployment_vars[deployment_type].security_groups, true), true), true) }}"
  44. when: host_type == "node" and sub_host_type == "infra"
  45. - set_fact:
  46. ec2_instance_type: "{{ ec2_node_instance_type | default(lookup('env', 'ec2_node_instance_type') | default(lookup('env', 'ec2_instance_type') | default(deployment_vars[deployment_type].type, true), true), true) }}"
  47. ec2_security_groups: "{{ ec2_node_security_groups | default(lookup('env', 'ec2_node_security_groups') | default(lookup('env', 'ec2_security_groups') | default(deployment_vars[deployment_type].security_groups, true), true), true) }}"
  48. when: host_type == "node" and sub_host_type == "compute"
  49. - set_fact:
  50. ec2_instance_type: "{{ lookup('env', 'ec2_instance_type')
  51. | default(deployment_vars[deployment_type].type, true) }}"
  52. when: ec2_instance_type is not defined
  53. - set_fact:
  54. ec2_security_groups: "{{ lookup('env', 'ec2_security_groups') | default(deployment_vars[deployment_type].security_groups, true) }}"
  55. when: ec2_security_groups is not defined
  56. - name: Find amis for deployment_type
  57. ec2_ami_find:
  58. region: "{{ ec2_region }}"
  59. ami_id: "{{ ec2_image | default(omit, true) }}"
  60. name: "{{ ec2_image_name | default(omit, true) }}"
  61. register: ami_result
  62. - fail: msg="Could not find requested ami"
  63. when: not ami_result.results
  64. - set_fact:
  65. latest_ami: "{{ ami_result.results | oo_ami_selector(ec2_image_name) }}"
  66. volume_defs:
  67. etcd:
  68. root:
  69. volume_size: "{{ lookup('env', 'os_etcd_root_vol_size') | default(25, true) }}"
  70. device_type: "{{ lookup('env', 'os_etcd_root_vol_type') | default('gp2', true) }}"
  71. iops: "{{ lookup('env', 'os_etcd_root_vol_iops') | default(500, true) }}"
  72. etcd:
  73. volume_size: "{{ lookup('env', 'os_etcd_vol_size') | default(32, true) }}"
  74. device_type: "{{ lookup('env', 'os_etcd_vol_type') | default('gp2', true) }}"
  75. iops: "{{ lookup('env', 'os_etcd_vol_iops') | default(500, true) }}"
  76. master:
  77. root:
  78. volume_size: "{{ lookup('env', 'os_master_root_vol_size') | default(25, true) }}"
  79. device_type: "{{ lookup('env', 'os_master_root_vol_type') | default('gp2', true) }}"
  80. iops: "{{ lookup('env', 'os_master_root_vol_iops') | default(500, true) }}"
  81. docker:
  82. volume_size: "{{ lookup('env', 'os_docker_vol_size') | default(10, true) }}"
  83. device_type: "{{ lookup('env', 'os_docker_vol_type') | default('gp2', true) }}"
  84. iops: "{{ lookup('env', 'os_docker_vol_iops') | default(500, true) }}"
  85. node:
  86. root:
  87. volume_size: "{{ lookup('env', 'os_node_root_vol_size') | default(85, true) }}"
  88. device_type: "{{ lookup('env', 'os_node_root_vol_type') | default('gp2', true) }}"
  89. iops: "{{ lookup('env', 'os_node_root_vol_iops') | default(500, true) }}"
  90. docker:
  91. volume_size: "{{ lookup('env', 'os_docker_vol_size') | default(32, true) }}"
  92. device_type: "{{ lookup('env', 'os_docker_vol_type') | default('gp2', true) }}"
  93. iops: "{{ lookup('env', 'os_docker_vol_iops') | default(500, true) }}"
  94. - set_fact:
  95. volumes: "{{ volume_defs | oo_ec2_volume_definition(host_type, docker_vol_ephemeral | bool) }}"
  96. - name: Launch instance(s)
  97. ec2:
  98. state: present
  99. region: "{{ ec2_region }}"
  100. keypair: "{{ ec2_keypair }}"
  101. group: "{{ ec2_security_groups }}"
  102. instance_type: "{{ ec2_instance_type }}"
  103. image: "{{ latest_ami }}"
  104. count: "{{ instances | length }}"
  105. vpc_subnet_id: "{{ ec2_vpc_subnet | default(omit, true) }}"
  106. assign_public_ip: "{{ ec2_assign_public_ip | default(omit, true) }}"
  107. user_data: "{{ lookup('template', '../templates/user_data.j2') }}"
  108. wait: yes
  109. instance_tags:
  110. created-by: "{{ created_by }}"
  111. clusterid: "{{ cluster }}"
  112. environment: "{{ cluster_env }}"
  113. host-type: "{{ host_type }}"
  114. sub-host-type: "{{ sub_host_type }}"
  115. volumes: "{{ volumes }}"
  116. register: ec2
  117. - name: Add Name tag to instances
  118. ec2_tag: resource={{ item.1.id }} region={{ ec2_region }} state=present
  119. with_together:
  120. - instances
  121. - ec2.instances
  122. args:
  123. tags:
  124. Name: "{{ item.0 }}"
  125. - set_fact:
  126. instance_groups: "tag_created-by_{{ created_by }}, tag_clusterid_{{ cluster }}, tag_environment_{{ cluster_env }},
  127. tag_host-type_{{ host_type }}, tag_sub-host-type_{{ sub_host_type }}"
  128. - set_fact:
  129. node_label:
  130. region: "{{ec2_region}}"
  131. type: "{{sub_host_type}}"
  132. when: host_type == "node"
  133. - set_fact:
  134. node_label:
  135. region: "{{ec2_region}}"
  136. type: "{{host_type}}"
  137. when: host_type != "node"
  138. - set_fact:
  139. logrotate:
  140. - name: syslog
  141. path: "/var/log/cron
  142. \n/var/log/maillog
  143. \n/var/log/messages
  144. \n/var/log/secure
  145. \n/var/log/spooler \n"
  146. options:
  147. - daily
  148. - rotate 7
  149. - compress
  150. - sharedscripts
  151. - missingok
  152. scripts:
  153. postrotate: "/bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true"
  154. - name: Add new instances groups and variables
  155. add_host:
  156. hostname: "{{ item.0 }}"
  157. ansible_ssh_host: "{{ item.1.dns_name }}"
  158. ansible_ssh_user: "{{ deployment_vars[deployment_type].ssh_user }}"
  159. ansible_become: "{{ deployment_vars[deployment_type].sudo }}"
  160. groups: "{{ instance_groups }}"
  161. ec2_private_ip_address: "{{ item.1.private_ip }}"
  162. ec2_ip_address: "{{ item.1.public_ip }}"
  163. openshift_node_labels: "{{ node_label }}"
  164. logrotate_scripts: "{{ logrotate }}"
  165. with_together:
  166. - instances
  167. - ec2.instances
  168. - name: Add new instances to nodes_to_add group if needed
  169. add_host:
  170. hostname: "{{ item.0 }}"
  171. ansible_ssh_host: "{{ item.1.dns_name }}"
  172. ansible_ssh_user: "{{ deployment_vars[deployment_type].ssh_user }}"
  173. ansible_become: "{{ deployment_vars[deployment_type].sudo }}"
  174. groups: nodes_to_add
  175. ec2_private_ip_address: "{{ item.1.private_ip }}"
  176. ec2_ip_address: "{{ item.1.public_ip }}"
  177. openshift_node_labels: "{{ node_label }}"
  178. logrotate_scripts: "{{ logrotate }}"
  179. with_together:
  180. - instances
  181. - ec2.instances
  182. when: oo_extend_env is defined and oo_extend_env | bool
  183. - name: Wait for ssh
  184. wait_for: "port=22 host={{ item.dns_name }}"
  185. with_items: ec2.instances
  186. - name: Wait for user setup
  187. command: "ssh -o StrictHostKeyChecking=no -o PasswordAuthentication=no -o ConnectTimeout=10 -o UserKnownHostsFile=/dev/null {{ hostvars[item.0].ansible_ssh_user }}@{{ item.1.dns_name }} echo {{ hostvars[item.0].ansible_ssh_user }} user is setup"
  188. register: result
  189. until: result.rc == 0
  190. retries: 20
  191. delay: 10
  192. with_together:
  193. - instances
  194. - ec2.instances