- hosts: localhost connection: local gather_facts: false user: root pre_tasks: - include_vars: variables.yml tasks: - name: Get EC2 instance IDs run_once: true ec2_remote_facts: filters: "tag:Type": "{{ ec2_tag_Type }}" "tag:Role": "{{ ec2_tag_Role }}" region: "{{ ec2_region }}" register: instances - name: display instances run_once: true debug: var: instances - name: Remove registered instances run_once: true ec2: state: absent wait: true instance_ids: "{{instances|json_query('instances[*].id')}}" region: "{{ ec2_region }}" when: instances