-
Notifications
You must be signed in to change notification settings - Fork 0
/
stop-ec2-tag-image.yml
37 lines (33 loc) · 1.13 KB
/
stop-ec2-tag-image.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
---
# ansible-playbook stop-ec2-tag-image.yml -vv
# Play 1: Stop instance on AWS and delete if required
- name: Stop EC2 instance by Name TAG
hosts: localhost
become: false
gather_facts: false
vars:
- ec2_name_tag: nleiva-centos-7.8
- ec2_region: us-east-1
- delete: false
- ec2_environment: "{{ aws_prefix | default('ansible') }}"
tasks:
- name: Stop instances
amazon.aws.ec2_instance:
region: "{{ ec2_region }}"
tags:
Name: "{{ ec2_name_tag }}"
Environment: "{{ ec2_environment }}"
state: stopped
register: stopped_instances
- name: Print out instances to be deleted (it won't do it by default)
ansible.builtin.debug:
msg: "Instances to be deleted if defined: {{ stopped_instances.instance_ids | join(', ') }}"
# Need to wait a bit before destroying them. TODO: add a wait task.
- name: Destroy instances
amazon.aws.ec2_instance:
region: "{{ ec2_region }}"
state: absent
instance_ids: "{{ stopped_instances.instance_ids | list }}"
wait: true
register: result_ec2_destroy
when: delete