summaryrefslogtreecommitdiffstats
path: root/playbooks/aws/openshift-master/terminate.yml
blob: fd15cf00f3429d82579a63fc134b5f005ec72d89 (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
---
- name: Populate oo_masters_to_terminate host group if needed
  hosts: localhost
  gather_facts: no
  tasks:
    - name: Evaluate oo_host_group_exp if it's set
      add_host: "name={{ item }} groups=oo_masters_to_terminate"
      with_items: "{{ oo_host_group_exp | default('') }}"
      when: oo_host_group_exp is defined

- name: Gather facts for instances to terminate
  hosts: oo_masters_to_terminate

- name: Terminate instances
  hosts: localhost
  connection: local
  gather_facts: no
  vars:
    host_vars: "{{ hostvars
        | oo_select_keys(groups['oo_masters_to_terminate']) }}"
  tasks:
    - 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

    # Fail if any of the instances failed to terminate with an error other
    # than 403 Forbidden
    - fail: msg=Terminating instance {{ item.item.ec2_id }} failed with message {{ item.msg }}
      when: "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: 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