launch.yml 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. ---
  2. - name: Launch instance(s)
  3. hosts: localhost
  4. connection: local
  5. gather_facts: no
  6. vars:
  7. inst_region: us-east-1
  8. rhel7_ami: ami-a24e30ca
  9. user_data_file: user_data.txt
  10. oo_vpc_subnet_id: # Purposely left blank, these are here to be overridden in env vars_files
  11. oo_assign_public_ip: # Purposely left blank, these are here to be overridden in env vars_files
  12. vars_files:
  13. - vars.yml
  14. - "vars.{{ oo_env }}.yml"
  15. tasks:
  16. - name: Launch instances in VPC
  17. ec2:
  18. state: present
  19. region: "{{ inst_region }}"
  20. keypair: mmcgrath_libra
  21. group_id: "{{ oo_security_group_ids }}"
  22. instance_type: c4.xlarge
  23. image: "{{ rhel7_ami }}"
  24. count: "{{ oo_new_inst_names | oo_len }}"
  25. user_data: "{{ lookup('file', user_data_file) }}"
  26. wait: yes
  27. assign_public_ip: "{{ oo_assign_public_ip }}"
  28. vpc_subnet_id: "{{ oo_vpc_subnet_id }}"
  29. when: oo_vpc_subnet_id
  30. register: ec2_vpc
  31. - set_fact:
  32. ec2: "{{ ec2_vpc }}"
  33. when: oo_vpc_subnet_id
  34. - name: Launch instances in Classic
  35. ec2:
  36. state: present
  37. region: "{{ inst_region }}"
  38. keypair: mmcgrath_libra
  39. group: ['Libra', '{{ oo_env }}', '{{ oo_env }}_proxy', '{{ oo_env }}_proxy_atomic']
  40. instance_type: c4.xlarge
  41. image: "{{ rhel7_ami }}"
  42. count: "{{ oo_new_inst_names | oo_len }}"
  43. user_data: "{{ lookup('file', user_data_file) }}"
  44. wait: yes
  45. when: not oo_vpc_subnet_id
  46. register: ec2_classic
  47. - set_fact:
  48. ec2: "{{ ec2_classic }}"
  49. when: not oo_vpc_subnet_id
  50. - name: Add Name and environment tags to instances
  51. ec2_tag: "resource={{ item.1.id }} region={{ inst_region }} state=present"
  52. with_together:
  53. - oo_new_inst_names
  54. - ec2.instances
  55. args:
  56. tags:
  57. Name: "{{ item.0 }}"
  58. - name: Add other tags to instances
  59. ec2_tag: "resource={{ item.id }} region={{ inst_region }} state=present"
  60. with_items: ec2.instances
  61. args:
  62. tags: "{{ oo_new_inst_tags }}"
  63. - name: Add new instances public IPs to oo_hosts_to_config
  64. add_host: "hostname={{ item.0 }} ansible_ssh_host={{ item.1.public_ip }} groupname=oo_hosts_to_config"
  65. with_together:
  66. - oo_new_inst_names
  67. - ec2.instances
  68. - debug: var=ec2
  69. - name: Wait for ssh
  70. wait_for: "port=22 host={{ item.public_ip }}"
  71. with_items: ec2.instances
  72. - name: Wait for root user setup
  73. command: "ssh -o StrictHostKeyChecking=no -o PasswordAuthentication=no -o ConnectTimeout=10 -o UserKnownHostsFile=/dev/null root@{{ item.public_ip }} echo root user is setup"
  74. register: result
  75. until: result.rc == 0
  76. retries: 20
  77. delay: 10
  78. with_items: ec2.instances
  79. - name: Initial setup
  80. hosts: oo_hosts_to_config
  81. user: root
  82. gather_facts: true
  83. tasks:
  84. - name: Yum update
  85. yum: name=* state=latest
  86. # Apply the configs, seprate so that just the configs can be run by themselves
  87. - include: config.yml