launch_instances.yml 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. - set_fact:
  9. ec2_region: "{{ lookup('env', 'ec2_region')
  10. | default(deployment_vars[deployment_type].region, true) }}"
  11. when: ec2_region is not defined
  12. - set_fact:
  13. ec2_image_name: "{{ lookup('env', 'ec2_image_name')
  14. | default(deployment_vars[deployment_type].image_name, true) }}"
  15. when: ec2_image_name is not defined and ec2_image is not defined
  16. - set_fact:
  17. ec2_image: "{{ lookup('env', 'ec2_image')
  18. | default(deployment_vars[deployment_type].image, true) }}"
  19. when: ec2_image is not defined and not ec2_image_name
  20. - set_fact:
  21. ec2_instance_type: "{{ lookup('env', 'ec2_instance_type')
  22. | default(deployment_vars[deployment_type].type, true) }}"
  23. when: ec2_instance_type is not defined
  24. - set_fact:
  25. ec2_keypair: "{{ lookup('env', 'ec2_keypair')
  26. | default(deployment_vars[deployment_type].keypair, true) }}"
  27. when: ec2_keypair is not defined
  28. - set_fact:
  29. ec2_vpc_subnet: "{{ lookup('env', 'ec2_vpc_subnet')
  30. | default(deployment_vars[deployment_type].vpc_subnet, true) }}"
  31. when: ec2_vpc_subnet is not defined
  32. - set_fact:
  33. ec2_assign_public_ip: "{{ lookup('env', 'ec2_assign_public_ip')
  34. | default(deployment_vars[deployment_type].assign_public_ip, true) }}"
  35. when: ec2_assign_public_ip is not defined
  36. - set_fact:
  37. ec2_security_groups: "{{ lookup('env', 'ec2_security_groups')
  38. | default(deployment_vars[deployment_type].security_groups, true) }}"
  39. when: ec2_security_groups is not defined
  40. - name: Find amis for deployment_type
  41. ec2_ami_find:
  42. region: "{{ ec2_region }}"
  43. ami_id: "{{ ec2_image | default(omit, true) }}"
  44. name: "{{ ec2_image_name | default(omit, true) }}"
  45. register: ami_result
  46. - fail: msg="Could not find requested ami"
  47. when: not ami_result.results
  48. - set_fact:
  49. latest_ami: "{{ ami_result.results | oo_ami_selector(ec2_image_name) }}"
  50. user_data: "{{ lookup('template', '../templates/user_data.j2') }}"
  51. volume_defs:
  52. etcd:
  53. root:
  54. volume_size: "{{ lookup('env', 'os_etcd_root_vol_size') | default(25, true) }}"
  55. device_type: "{{ lookup('env', 'os_etcd_root_vol_type') | default('gp2', true) }}"
  56. iops: "{{ lookup('env', 'os_etcd_root_vol_iops') | default(500, true) }}"
  57. etcd:
  58. volume_size: "{{ lookup('env', 'os_etcd_vol_size') | default(32, true) }}"
  59. device_type: "{{ lookup('env', 'os_etcd_vol_type') | default('gp2', true) }}"
  60. iops: "{{ lookup('env', 'os_etcd_vol_iops') | default(500, true) }}"
  61. master:
  62. root:
  63. volume_size: "{{ lookup('env', 'os_master_root_vol_size') | default(25, true) }}"
  64. device_type: "{{ lookup('env', 'os_master_root_vol_type') | default('gp2', true) }}"
  65. iops: "{{ lookup('env', 'os_master_root_vol_iops') | default(500, true) }}"
  66. node:
  67. root:
  68. volume_size: "{{ lookup('env', 'os_node_root_vol_size') | default(25, true) }}"
  69. device_type: "{{ lookup('env', 'os_node_root_vol_type') | default('gp2', true) }}"
  70. iops: "{{ lookup('env', 'os_node_root_vol_iops') | default(500, true) }}"
  71. docker:
  72. volume_size: "{{ lookup('env', 'os_docker_vol_size') | default(32, true) }}"
  73. device_type: "{{ lookup('env', 'os_docker_vol_type') | default('gp2', true) }}"
  74. iops: "{{ lookup('env', 'os_docker_vol_iops') | default(500, true) }}"
  75. - set_fact:
  76. volumes: "{{ volume_defs | oo_ec2_volume_definition(host_type, docker_vol_ephemeral | bool) }}"
  77. - name: Launch instance(s)
  78. ec2:
  79. state: present
  80. region: "{{ ec2_region }}"
  81. keypair: "{{ ec2_keypair }}"
  82. group: "{{ ec2_security_groups }}"
  83. instance_type: "{{ ec2_instance_type }}"
  84. image: "{{ latest_ami }}"
  85. count: "{{ instances | length }}"
  86. vpc_subnet_id: "{{ ec2_vpc_subnet | default(omit, true) }}"
  87. assign_public_ip: "{{ ec2_assign_public_ip | default(omit, true) }}"
  88. user_data: "{{ user_data }}"
  89. wait: yes
  90. instance_tags:
  91. created-by: "{{ created_by }}"
  92. environment: "{{ env }}"
  93. env: "{{ env }}"
  94. host-type: "{{ host_type }}"
  95. env-host-type: "{{ env_host_type }}"
  96. volumes: "{{ volumes }}"
  97. register: ec2
  98. - name: Add Name tag to instances
  99. ec2_tag: resource={{ item.1.id }} region={{ ec2_region }} state=present
  100. with_together:
  101. - instances
  102. - ec2.instances
  103. args:
  104. tags:
  105. Name: "{{ item.0 }}"
  106. - set_fact:
  107. instance_groups: tag_created-by_{{ created_by }}, tag_env_{{ env }}, tag_host-type_{{ host_type }}, tag_env-host-type_{{ env_host_type }}
  108. - name: Add new instances groups and variables
  109. add_host:
  110. hostname: "{{ item.0 }}"
  111. ansible_ssh_host: "{{ item.1.dns_name }}"
  112. ansible_ssh_user: "{{ deployment_vars[deployment_type].ssh_user }}"
  113. ansible_sudo: "{{ deployment_vars[deployment_type].sudo }}"
  114. groups: "{{ instance_groups }}"
  115. ec2_private_ip_address: "{{ item.1.private_ip }}"
  116. ec2_ip_address: "{{ item.1.public_ip }}"
  117. with_together:
  118. - instances
  119. - ec2.instances
  120. - name: Wait for ssh
  121. wait_for: "port=22 host={{ item.dns_name }}"
  122. with_items: ec2.instances
  123. - name: Wait for user setup
  124. 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"
  125. register: result
  126. until: result.rc == 0
  127. retries: 20
  128. delay: 10
  129. with_together:
  130. - instances
  131. - ec2.instances