Skip to content

Instantly share code, notes, and snippets.

@AdheipSingh
Last active February 22, 2020 22:26
Show Gist options
  • Save AdheipSingh/31fc5544c4bf706c93a427218bc1bece to your computer and use it in GitHub Desktop.
Save AdheipSingh/31fc5544c4bf706c93a427218bc1bece to your computer and use it in GitHub Desktop.
ansible.yaml
---
- name: LAUNCH AN EC2 INSTANCE
hosts: localhost
vars: ##################### THESE VARS WILL LAUNCH THE EC2 INSTANCE ################# ON LINE 56 57 58 MENTION THE AMI VARS #######################
region: us-east-1 ## The region you want to provision the EC2
ec2_key_name: Ansible-Keypair ## The key which shall launch the instance
instance_type: t2.micro ## Instance Type
image: ## Image ID for which Base OS
subnet_id: ## The subnet id in which the ec2 shall be launched
security_group_name: launch-wizard-2 ## Name of security group which shall be allocated to the instance launched
assign_public_ip: yes ## This shall assign a public ip which shall reboot on termination
name_tag: 2b-terminated ## This an instance tag
volume_size_root: 20 ## This root volume which shall have base OS installed
delete_on_termination: true ## Whether you want to delete the instance on termination
volume_type_root: gp2 ## gp2 ssd or iops
tasks:
- ec2:
key_name: "{{ ec2_key_name }}"
region: "{{ region }}"
instance_type: "{{ instance_type }}"
image: "{{ image }}"
vpc_subnet_id: "{{ subnet_id }}"
wait: yes
group: "{{ security_group_name }}"
wait_timeout: 500
volumes:
- device_name: /dev/sda1
volume_type: "{{ volume_type_root }}"
volume_size: "{{ volume_size_root }}"
assign_public_ip: "{{ assign_public_ip }}"
instance_tags:
Name: "{{ name_tag }}"
register: ec2
- debug:
var: ec2
- name: Wait for SSH to come up
wait_for:
host: "{{ item.public_ip }}"
port: 22
state: started
loop: "{{ ec2.instances }}"
- name: Add new instance to host group
add_host:
hostname: "{{ item.public_ip }}"
groupname: launched
loop: "{{ ec2.instances }}"
- hosts: launched
become: true
gather_facts: True
vars:
name_tag: 2b-terminated ### EC2 NAME
ami_name: poc-ami-redhat ### AMI NAME
ami_name_tag: poc-ami-redhat ### AMI TAG NAME
region: us-east-1 ### REGION
tasks:
- name: INSTALL WGET AND UNZIP
yum:
name: "{{ item }}"
state: present
with_items:
- wget
- unzip
- name: CONFIGURE BOTO3 AND PIP
shell: "{{ item }}"
with_items:
- curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py"
- sudo python get-pip.py
- pip install boto3
- name: INSTALL AWS CLOUDWATCH AGENT
shell: "{{ item }}"
with_items:
- wget https://s3.amazonaws.com/amazoncloudwatch-agent/linux/amd64/latest/AmazonCloudWatchAgent.zip
- unzip AmazonCloudWatchAgent.zip
- sudo ./install.sh
- name: ENSURE PERMISSIONS ON CRONTAB
file:
path: "{{ item }}"
mode: og-rwx
owner: root
group: root
with_items:
- /etc/crontab
- /etc/cron.hourly
- /etc/cron.weekly
- /etc/cron.monthly
- /etc/cron.d
- name: ALLOW ROOT IN crontab
lineinfile:
path: "{{ item }}"
line: 'root'
create: yes
with_items:
- /etc/cron.allow
- /etc/at.allow
- name: GATHER FACTS ON INSTANCE
ec2_instance_facts:
filters:
"tag:Name": "{{ name_tag }}"
delegate_to: 127.0.0.1
register: ec2
- name: CREATE AMI ID
ec2_ami:
instance_id: "{{ ec2.instances[0].id }}"
name: "{{ ami_name }}"
region: "{{ region }}"
wait: "{{ ec2_ami_wait | default(true) }}"
wait_timeout: "{{ wait_timeout | default(1000)}}"
state: "{{ state | default('present') }}"
tags:
Name: "{{ ami_name_tag }}"
delegate_to: 127.0.0.1
register: ec2_ami
- debug:
var: ec2_ami
- debug:
msg: "YOUR AMI HAS BEEN CREATED NOW USE THIS AMI TO LAUNCH AN EC2 INSTANCE USING THE ec2-launch playbook"
- debug:
msg: "NOW YOUR AMI HAS BEEN CREATED THE INSTANCE USED FOR CREATION FOR AMI IS SET TO TERMINATE TO PREVENT FUTURE COSTS"
- name: TERMINATING ANSIBLE INSTANCE
ec2:
instance_id: "{{ ec2.instances[0].id }}"
state: absent
region: "{{ region }}"
delegate_to: 127.0.0.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment