launch_instances.yml 7.6 KB

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