launch_instances.yml 8.1 KB

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