Ansible Playbook for provisioning a new EC2 instance in AWS - Create a new EC2 Using Ansible Playbook
Please find the Ansible Playbook for provisioning a new EC2 instance. Please follow the below steps in the machine where you installed Ansible.
Steps to create EC2 instance using Ansible:
1. Login to AWS console, click on username and go to My security credentials.
2. Continue on security credentials, click on access keys
3. Create a new access key if you dont have one. Make sure you download the keys.
4. Login to EC2 instance using Git bash or ITerm where you installed Ansible.
5. Edit Ansible hosts or inventory file
sudo vi /etc/ansible/hosts
Add the below two lines in the end of the file:
[localhost]
local
Steps to create EC2 instance using Ansible:
1. Login to AWS console, click on username and go to My security credentials.
2. Continue on security credentials, click on access keys
3. Create a new access key if you dont have one. Make sure you download the keys.
4. Login to EC2 instance using Git bash or ITerm where you installed Ansible.
execute the below command
sudo vi ~/.boto
add below three lines in the above file, replace the ?? with access key and secret key values.
[Credentials]
aws_access_key_id = ??
aws_secret_access_key = ??
5. Edit Ansible hosts or inventory file
sudo vi /etc/ansible/hosts
Add the below two lines in the end of the file:
[localhost]
local
8. cd playbooks
Create Ansible playbook
9. sudo vi create_jenkins_ec2.yml
(copy the below content in green color)
(copy the below content in green color)
edit the create_jenkins_ec2.yml to make sure you update the key which is red marked below:
---
- name: provisioning EC2 Lab Exercises using Ansible
hosts: localhost
connection: local
gather_facts: False
tags: provisioning
vars:
keypair: MyEC2Key
instance_type: t2.small
image: ami-07c1207a9d40bc3bd
wait: yes
group: webserver
count: 1
region: us-east-2
security_group: my-jenkins-security-grp
tasks:
- name: Create my security group
local_action:
module: ec2_group
name: "{{ security_group }}"
description: Security Group for webserver Servers
region: "{{ region }}"
rules:
- proto: tcp
from_port: 22
to_port: 22
cidr_ip: 0.0.0.0/0
- proto: tcp
from_port: 8080
to_port: 8080
cidr_ip: 0.0.0.0/0
- proto: tcp
from_port: 80
to_port: 80
cidr_ip: 0.0.0.0/0
rules_egress:
- proto: all
cidr_ip: 0.0.0.0/0
register: basic_firewall
- name: Launch the new EC2 Instance
local_action: ec2
group={{ security_group }}
instance_type={{ instance_type}}
image={{ image }}
wait=true
region={{ region }}
keypair={{ keypair }}
count={{count}}
register: ec2
- name: Add Tagging to EC2 instance
local_action: ec2_tag resource={{ item.id }} region={{ region }} state=present
with_items: "{{ ec2.instances }}"
args:
tags:
Name: MyTargetEc2Instance
No comments:
Post a Comment