6.0 - Cleaning up Resources¶
Overview¶
-
It's beyond recommended to cleanup cloud resources to avoid incurring charges for unused resources.
-
Typically in this scenario, resources should be removed in the order of creation, each will require modules used to create them. Most of the time will be the same, with
state: absentadded instead ofpresent -
For any resources that need to be "found" before cleanup, modules of similar name with
_infosuffixed can be used e.g.:ec2_instance_infoec2_vpc_net_infoec2_vpc_route_info
-
Further information can be found via the Ansible docs.
- Example EC2 termination playbook:
- name: AWS EC2 Termination
hosts: localhost
gather_facts: false
vars_files:
- vars/info.yaml
- vars/instance_ids.yaml
tasks:
- name: Terminate Instances
hosts: localhost
connection: local
tasks:
- name: Terminate instances that were previously launched
ec2:
state: 'absent'
instance_ids: '{{ instance_ids }}'