Wednesday 13 January 2016

Ansible Playbook To Launch A New Instance In AWS

Ansible-Playbook To Launch A new Instance | AWS

Install Ansible

In this post, will talk about the ansible-playbook to launch a new instance in AWS. If you have not installed ansible yet than go ahead and install it from here.

Setup AWS credentials

After installing the ansible, you need to setup the secret key and access key after generating them from AWS. Put the secret key and access key in .basrc file as shown below.


# For AWS API Access
export AWS_ACCESS_KEY_ID='XXXXXXXXXXX'
export AWS_SECRET_ACCESS_KEY='XXXXXXXXXXXXXX' 
 

The Playbook

---
  - name: provision
    hosts: localhost
    connection: localhost


  tasks:

  - name: Launch the new EC2 Instance
    local_action: ec2
                  group={{ security_group }}
                  instance_type={{ instance_type}}
                  image={{ image }}
                  wait=true
                  region={{ region }}
                  keypair={{ keypair }}
                  volumes={{volumes}}
                  count={{count}}
                  vpc_subnet_id={{vpc_subnet_id}}
    register: ec2

  - name: Add the newly created EC2 instance(s) to the local host group (located inside the directory)
    local_action: lineinfile
                  dest="./hosts"
                  regexp={{ item.public_ip }}
                  insertafter="[launched]" line="{{ item.public_ip }} ansible_ssh_private_key_file=~/.ssh/{{ keypair }}.pem"
    with_items: ec2.instances

  - name: Wait for SSH to come up
    wait_for: host={{ item.public_ip }} port=22 delay=60 timeout=320 state=started
    with_items: ec2.instances

  - name: Add tag to Instance(s)
    local_action: ec2_tag resource={{ item.id }} region={{ region }} state=present
    with_items: ec2.instances
    args:
      tags:
        Name: mynewinstance 

The Variable files

The variable file will have following variables defined which will be used by the playbook. Following is the sample.

---
instance_type: m3.large
security_group: my-sg # Change the security group name here
image: ami-96fjdkc4 # Change the AMI, from which you want to launch the server
region: ap-southeast-1 # Change the Region
keypair: mykey # Change the keypair name
vpc_subnet_id: subnet-20dhf657

Understanding the Playbook

  - name: Launch the new EC2 Instance

The first task of the playbook is to create a new instance and get the details in ec2 variable.
The other variables like {{security group}}, {{instance_type}},{{image}} etc should be available in the vars file.

- name: Add the newly created EC2 instance(s) to the local host group (located inside the directory)

This task updates the hosts file with the IP of newly created instance using lineinfile module of ansible. It will insert the IP with the line "ansible_ssh_private_key_file=~/.ssh/{{ keypair }}.pem" under the group of hosts named "launched" 

  - name: Wait for SSH to come up 

Now since we have created the instance and made an entry in the hosts file we need to wait a bit for the instance to be ready for SSH. So this task take care whether the instance is ready for SSH or not.

- name: Add tag to Instance(s)

Finally when the instance is ready, we can add a tag(name) to it. This task gives the tag name "mynewinstance" to our newly created instance.

1 comments:

  1. I really appreciate information shared above. This snapshot shared in this blog make me easy to understand all steps. Thanks for sharing

    ReplyDelete

 

Copyright @ 2013 Appychip.

Designed by Appychip & YouTube Channel