launch_instances.yml 9.2 KB

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