Created
August 14, 2017 07:56
-
-
Save nitanka/60e255f8d77f64d52ec502f44d02a77a to your computer and use it in GitHub Desktop.
Ansible: Creating ec2 instance (ubuntu) and adding it to host groups
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- name: creating an instance in AWS using default VPC and Security Group | |
hosts: localhost | |
vars: | |
instance_info: | |
- name: "< instance name >" | |
image: "ami-835b4efa" # Ubuntu 14.04 | |
group_id: '<security-group>' | |
region: '<region-name' #us-west-2, ap-northeast-1 | |
instance_type: '< instance type >' # t2.micro, mx.xlarge etc | |
key_name: '<key-name>' | |
vpc_subnet_id: '< subnet-id >' | |
count: <count> #no of servers to create | |
tasks: | |
- name: Creating the instance | |
ec2: | |
group_id: "{{ item.group_id }}" | |
region: "{{ item.region }}" | |
aws_access_key: "{{ lookup('env', 'AWS_ACCESS_KEY_ID') }}" | |
aws_secret_key: "{{ lookup('env', 'AWS_SECRET_ACCESS_KEY') }}" | |
image: '{{ item.image }}' | |
instance_type: "{{ item.instance_type }}" | |
key_name: "{{ item.key_name }}" | |
state: present | |
assign_public_ip: true | |
count: '{{ item.count }}' | |
instance_tags: | |
Name: '< instance-tage >' | |
vpc_subnet_id: '{{ item.vpc_subnet_id }}' | |
wait: yes #wait for the instance state to become running | |
register: ec2 | |
with_items: instance_info | |
- name: Add hosts group temporary inventory group with pem path | |
add_host: | |
name: "{{ item.public_ip }}" | |
groups: dynamic_hosts | |
ansible_user: ubuntu | |
with_items: "{{ instance_info.instances[0:] }}" | |
- hosts: dynamic_hosts | |
become: true | |
remote_user: ubuntu | |
roles: | |
- < roles > | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment