Skip to content

Instantly share code, notes, and snippets.

@kevduggan
Last active March 31, 2018 08:01
Show Gist options
  • Save kevduggan/74a7c9f2b1b45f1f4170 to your computer and use it in GitHub Desktop.
Save kevduggan/74a7c9f2b1b45f1f4170 to your computer and use it in GitHub Desktop.
Ansible AWS
- hosts: all # run for all hosts in the inventory
connection: local # ec2 module runs locally, no need to connect to a non existant host!
gather_facts: False
vars:
# build up dictionary of tags so we can use them as facts in the dynamic inventory when configuring the instances
TAGS: "{'env': 'Test', 'Name': '{{ inventory_hostname }}', {% for group in group_names %}'{{ group }}':'yes'{% if not loop.last %},{% endif %}{% endfor %}{{ GROUP_TAGS | default() }} }"
tasks:
- name: Provision an instance {{ inventory_hostname }}
ec2:
state: present
region: "{{ AWS_REGION }}"
key_name: "{{ KEY_NAME }}"
group_id: "{{ SECURITY_GROUP.group_id }}"
vpc_subnet_id: "{{ subnet_id }}"
instance_type: "{{ INSTANCE_TYPE }}"
image: "{{ AMI_ID }}"
assign_public_ip: yes
wait: true
instance_tags: "{{ TAGS }}"
volumes:
- device_name: /dev/sda1
volume_type: gp2
volume_size: 8
delete_on_termination: true
register: ec2_instance
when: inventory_hostname != "localhost"
# make sure we can ssh into the instances, otherwise no point proceeding with trying to configure them!
- name: Wait for SSH to start
wait_for:
host: "{{ ec2_instance.instances[0].public_ip }}"
port: 22
timeout: 300
delegate_to: localhost
when: inventory_hostname != "localhost"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment