Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save jorandradefig/a9a6ce2bcac7d0b358781545fa385084 to your computer and use it in GitHub Desktop.

Select an option

Save jorandradefig/a9a6ce2bcac7d0b358781545fa385084 to your computer and use it in GitHub Desktop.
Create new EC2 instance with Ansible
---
- name: Create new EC2 instance
hosts: localhost
connection: local
gather_facts: False
vars:
aws_access_key: "{{ lookup('env','AWS_ACCESS_KEY_ID') }}"
aws_secret_key: "{{ lookup('env','AWS_SECRET_ACCESS_KEY') }}"
ec2_keypair: "{{ lookup('env','EC2_KEY_PAIR') }}"
ec2_security_group: "{{ lookup('env','EC2_SECURITY_GROUP') }}"
ec2_instance_type: "{{ lookup('env','EC2_INSTANCE_TYPE') }}"
ec2_image: "{{ lookup('env','EC2_IMAGE') }}"
ec2_subnet_id: "{{ lookup('env','EC2_SUBNET_ID') }}"
ec2_region: "{{ lookup('env','EC2_REGION') }}"
ec2_tag_name: "{{ lookup('env','EC2_TAG_NAME') }}"
ec2_volume_size: "{{ lookup('env','EC2_VOLUME_SIZE') }}"
tasks:
- name: Create an EC2 instance
ec2:
key_name: "{{ ec2_keypair }}"
group: "{{ ec2_security_group }}"
instance_type: "{{ ec2_instance_type }}"
image: "{{ ec2_image }}"
wait: true
region: "{{ ec2_region }}"
count: 1
count_tag:
Name: "{{ ec2_tag_name }}"
instance_tags:
Name: "{{ ec2_tag_name }}"
vpc_subnet_id: "{{ ec2_subnet_id }}"
assign_public_ip: yes
volumes:
- device_name: /dev/sda1
device_type: gp2
volume_size: "{{ ec2_volume_size }}"
delete_on_termination: true
register: ec2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment