Skip to content

Instantly share code, notes, and snippets.

@pattu777
Created September 16, 2013 11:42
Show Gist options
  • Save pattu777/6579617 to your computer and use it in GitHub Desktop.
Save pattu777/6579617 to your computer and use it in GitHub Desktop.
delete ec2 instances, ebs volumes and security groups via ansible playbooks.
Hi,
I have just started learning ansible. I have written a simple playbook which will create , delete a server using ansible's ec2 module. I can also create a volume and attach it to a previously created instance.
---
- name: Sample playbook for Amazon AWS ec2 management via ansible
hosts: local
connection: local
vars:
aws_access_key: xxxx
aws_secret_key: xxxx/xxxx/xxxx
keypair: abc123
security_group: default
instance_type: t1.micro
image: ami-05355a6c
count: 1
region: us-east-1
user: root
sudo: yes
tasks:
- name: Launch Instances
local_action: ec2 keypair={{keypair}} group={{security_group}} instance_type={{instance_type}} image={{image}} wait=true count={{count}} region={{region}} instance_tags='{"Name":"NewansibleTest019"}' aws_access_key={{aws_access_key}} aws_secret_key={{aws_secret_key}}
register: ec2
- name: Add volume
local_action: ec2_vol instance={{item.id}} volume_size=5 aws_access_key={{aws_access_key}} aws_secret_key={{aws_secret_key}}
with_items: ec2.instances
register: ec2_vol
- name: Stop instance
local_action: command aws ec2 stop-instances --region={{region}} --instance-id={{item.id}}
with_items: ec2.instances
My question is is there a way to detach/delete a volume, delete a security group and delete an instance(I used aws cli command as seen above).
Thanks,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment