summaryrefslogtreecommitdiffstats
path: root/playbooks/aws/openshift-cluster/terminate.yml
blob: aafd40c43c2fc9a6186188567bb2944e4e680d6f (plain)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
---
- name: Terminate instance(s)
  hosts: localhost
  connection: local
  become: no
  gather_facts: no
  vars_files:
  - vars.yml
  tasks:
  - set_fact: scratch_group=tag_env_{{ cluster_id }}
  - add_host:
      name: "{{ item }}"
      groups: oo_hosts_to_terminate
      ansible_ssh_user: "{{ deployment_vars[deployment_type].ssh_user }}"
      ansible_sudo: "{{ deployment_vars[deployment_type].sudo }}"
    with_items: groups[scratch_group] | default([]) | difference(['localhost'])

- name: Unsubscribe VMs
  hosts: oo_hosts_to_terminate
  roles:
  - role: rhel_unsubscribe
    when: deployment_type == "enterprise" and
          ansible_distribution == "RedHat" and
          lookup('oo_option', 'rhel_skip_subscription') | default(rhsub_skip, True) |
            default('no', True) | lower in ['no', 'false']

- name: Terminate instances
  hosts: localhost
  connection: local
  become: no
  gather_facts: no
  vars:
    host_vars: "{{ hostvars
                   | oo_select_keys(groups['oo_hosts_to_terminate']) }}"
  tasks:
    - name: Remove tags from instances
      ec2_tag: resource={{ item.ec2_id }} region={{ item.ec2_region }} state=absent
      args:
        tags:
          env: "{{ item['ec2_tag_env'] }}"
          host-type: "{{ item['ec2_tag_host-type'] }}"
          sub_host_type: "{{ item['ec2_tag_sub-host-type'] }}"
      with_items: host_vars
      when: "'oo_hosts_to_terminate' in groups"

    - name: Terminate instances
      ec2:
        state: absent
        instance_ids: ["{{ item.ec2_id }}"]
        region: "{{ item.ec2_region }}"
      ignore_errors: yes
      register: ec2_term
      with_items: host_vars
      when: "'oo_hosts_to_terminate' in groups"

    # Fail if any of the instances failed to terminate with an error other
    # than 403 Forbidden
    - fail: msg=Terminating instance {{ item.ec2_id }} failed with message {{ item.msg }}
      when: "'oo_hosts_to_terminate' in groups and item.failed and not item.msg | search(\"error: EC2ResponseError: 403 Forbidden\")"
      with_items: ec2_term.results

    - name: Stop instance if termination failed
      ec2:
        state: stopped
        instance_ids: ["{{ item.item.ec2_id }}"]
        region: "{{ item.item.ec2_region }}"
      register: ec2_stop
      when: "'oo_hosts_to_terminate' in groups and item.failed"
      with_items: ec2_term.results

    - name: Rename stopped instances
      ec2_tag: resource={{ item.item.item.ec2_id }} region={{ item.item.item.ec2_region }} state=present
      args:
        tags:
          Name: "{{ item.item.item.ec2_tag_Name }}-terminate"
      with_items: ec2_stop.results
      when: "'oo_hosts_to_terminate' in groups"