launch.yml 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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-9101c8fa
  9. user_data_file: user_data.txt
  10. vars_files:
  11. - vars.yml
  12. - "vars.{{ oo_env }}.yml"
  13. tasks:
  14. - name: Launch instances in VPC
  15. ec2:
  16. state: present
  17. region: "{{ inst_region }}"
  18. keypair: mmcgrath_libra
  19. group_id: "{{ oo_security_group_ids }}"
  20. instance_type: c4.xlarge
  21. image: "{{ rhel7_ami }}"
  22. count: "{{ oo_new_inst_names | length }}"
  23. user_data: "{{ lookup('file', user_data_file) }}"
  24. wait: yes
  25. assign_public_ip: "{{ oo_assign_public_ip }}"
  26. vpc_subnet_id: "{{ oo_vpc_subnet_id }}"
  27. register: ec2
  28. - name: Add Name and environment tags to instances
  29. ec2_tag: "resource={{ item.1.id }} region={{ inst_region }} state=present"
  30. with_together:
  31. - oo_new_inst_names
  32. - ec2.instances
  33. args:
  34. tags:
  35. Name: "{{ item.0 }}"
  36. - name: Add other tags to instances
  37. ec2_tag: "resource={{ item.id }} region={{ inst_region }} state=present"
  38. with_items: ec2.instances
  39. args:
  40. tags: "{{ oo_new_inst_tags }}"
  41. - name: Add new instances public IPs to oo_hosts_to_config
  42. add_host: "hostname={{ item.0 }} ansible_ssh_host={{ item.1.public_ip }} groupname=oo_hosts_to_config"
  43. with_together:
  44. - oo_new_inst_names
  45. - ec2.instances
  46. - debug: var=ec2
  47. - name: Wait for ssh
  48. wait_for: "port=22 host={{ item.public_ip }}"
  49. with_items: ec2.instances
  50. - name: Wait for root user setup
  51. command: "ssh -o StrictHostKeyChecking=no -o PasswordAuthentication=no -o ConnectTimeout=10 -o UserKnownHostsFile=/dev/null root@{{ item.public_ip }} echo root user is setup"
  52. register: result
  53. until: result.rc == 0
  54. retries: 20
  55. delay: 10
  56. with_items: ec2.instances
  57. - name: Initial setup
  58. hosts: oo_hosts_to_config
  59. user: root
  60. gather_facts: true
  61. tasks:
  62. - name: Update All Things
  63. action: "{{ ansible_pkg_mgr }} name=* state=latest"
  64. # Apply the configs, seprate so that just the configs can be run by themselves
  65. - include: config.yml