deprovision.yml 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. ---
  2. - hosts: localhost
  3. connection: local
  4. gather_facts: false
  5. tasks:
  6. - include_vars: "{{ item }}"
  7. with_first_found:
  8. - vars.yml
  9. - vars.yaml
  10. - name: Gather ec2 facts
  11. ec2_instance_facts:
  12. region: "{{ aws_region }}"
  13. filters:
  14. tag-key: "kubernetes.io/cluster/{{ aws_cluster_id }}"
  15. register: ec2
  16. - name: Terminate instances
  17. ec2:
  18. instance_ids: "{{ item.instance_id }}"
  19. region: "{{ aws_region }}"
  20. state: absent
  21. wait: no
  22. with_items: "{{ ec2.instances }}"
  23. when: not aws_use_auto_terminator | default(true)
  24. - when: aws_use_auto_terminator | default(true)
  25. block:
  26. - name: Stop VMs
  27. ec2:
  28. instance_ids: "{{ item.instance_id }}"
  29. region: "{{ aws_region }}"
  30. state: stopped
  31. wait: no
  32. with_items: "{{ ec2.instances }}"
  33. ignore_errors: true
  34. - name: Rename VMs
  35. ec2_tag:
  36. resource: "{{ item.instance_id }}"
  37. region: "{{ aws_region }}"
  38. tags:
  39. Name: "{{ item.tags.Name }}-terminate"
  40. when: "'-terminate' not in item.tags.Name"
  41. with_items: "{{ ec2.instances }}"