launch_instance.yml 3.44 KB
Newer Older
1 2 3 4
# This playbook will launch an ec2 instance in a VPC.
# This instance will have an autogenerated key.
#
# required variables for this playbook:
5 6 7 8
#   - base_ami_id                  - The base base AMI-ID
#   - ec2_vpc_subnet_id            - The Subnet ID to bring up the instance
#   - ec2_security_group_id        - The security group ID to use
#   - ec2_instance_profile_name    - The instance profile that should be used to launch this AMI
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#
# Other Variables:
#   - ec2_region              - The region the server should be brought up in
#   - ec2_instance_type       - The instance type to use
#   - ebs_volume_size         - Size in GB for the root volume
#   - ec2_timeout             - Time in seconds to wait for an ec2 instance become available
#   - ec2_assign_public_ip    - (yes/no) should the instance have a public IP address?
#
# This playbook generates a directory with 2 artifact files:
#   - launch_template.yml     - A yaml file with information such as the instance ID and internal IP address of the instance launched
#   - key.pem                  - The private key file for the newly generated keypair
#
# Example command line to run this playbook:
#    ansible-playbook -i "localhost," -c local -e @overrides.yml launch_instance.yml
#

- hosts: all
  vars:
    artifact_path: /tmp/ansible-runtime
    ec2_region: us-east-1
    ec2_instance_type: t2.medium
    ebs_volume_size: 8
    ec2_timeout: 500
    ec2_assign_public_ip: no
33
    automation_prefix: "gocd automation run -- {{ ansible_date_time.iso8601 }} -- "
34
  gather_facts: True
35 36 37 38 39 40 41 42 43
  connection: local
  tasks:

  - name: Generate UUID for keypair
    command: cat /proc/sys/kernel/random/uuid
    register: unique_key_name

  - name: Generate ec2 keypair to use for this instance
    ec2_key:
44
      name: "{{ automation_prefix }} {{ unique_key_name.stdout }}"
45 46 47 48 49
      region: "{{ ec2_region }}"
    register: ssh_key_register

  - name: Ensure artifact directory exists
    file:
50
      path: "{{ artifact_path }}"
51 52 53 54 55
      state: directory
      force: yes

  - name: Launch EC2 instance
    ec2:
56
      instance_tags: {"Name" : "{{ automation_prefix }} {{ unique_key_name.stdout }}"}
57
      region: "{{ ec2_region }}"
58
      key_name: "{{ automation_prefix }} {{ unique_key_name.stdout }}"
59 60 61 62 63 64 65 66
      instance_type: "{{ ec2_instance_type }}"
      image: "{{ base_ami_id }}"
      wait: yes
      group_id: "{{ ec2_security_group_id }}"
      count: 1
      vpc_subnet_id: "{{ ec2_vpc_subnet_id }}"
      assign_public_ip: "{{ ec2_assign_public_ip }}"
      volumes:
67
        - device_name: /dev/sdf
68
          volume_type: 'gp2'
69 70 71
          volume_size: "{{ ebs_volume_size }}"
      wait: yes
      wait_timeout: "{{ ec2_timeout }}"
72
      instance_profile_name: "{{ ec2_instance_profile_name }}"
73 74 75 76 77 78 79 80 81 82 83 84 85
    register: ec2_instance_register

  - name: Wait for SSH to come up
    wait_for:
      host: "{{ ec2_instance_register.instances[0].private_ip }}"
      port: 22
      delay: 60
      timeout: "{{ ec2_timeout }}"
      state: started

  - name: Generate artifact for jobs down stream
    template:
      src: templates/local/launch_template.yml.j2
86
      dest: "{{ artifact_path }}/launch_info.yml"
87 88 89 90 91
      mode: 0600

  - name: Generate key material artifact for jobs down stream
    template:
      src: templates/local/key.pem.j2
92
      dest: "{{ artifact_path }}/key.pem"
93 94
      mode: 0600

95 96 97 98 99 100
  - name: Generate ansible inventory file
    template:
      src: templates/local/inventory.j2
      dest: "{{ artifact_path }}/ansible_inventory"
      mode: 0600

101