-
Notifications
You must be signed in to change notification settings - Fork 38
/
tower-deprovision.yml
62 lines (53 loc) · 2.12 KB
/
tower-deprovision.yml
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
---
- hosts: localhost
become: false
gather_facts: no
vars:
tower_host: "{{ groups['tower'][0] }}"
tower_api_password: "{{ hostvars[tower_host]['admin_password'] }}"
tower_api_user: admin
tower_protocol: "https"
tasks:
- block:
- set_fact:
tower_api_base: "{{ tower_protocol }}://{{ tower_host | infer_address(hostvars) }}"
- name: get instance groups (excluding isolated)
uri:
url: "{{ tower_api_base }}/api/v2/instance_groups?controller=null"
register: instance_groups
become: false
changed_when: False
- name: get instance group instances
uri:
url: "{{ tower_api_base }}{{ item['related']['instances'] }}"
register: response
changed_when: False
loop: "{{ instance_groups.json.results }}"
- name: set facts for configured instances
set_fact:
tower_instances: "{{ response.results | map(attribute='json') | map(attribute='results') | flatten | map(attribute='hostname') | list }}"
- name: set fact for all instance groups
set_fact:
inventory_instance_groups: "{{ (groups | select('match', 'instance_group') | list ) + (groups | select('match', 'tower') | list) }}"
- name: set fact for each instance in instance instance_groups
set_fact:
inventory_instances: "{{ inventory_instance_groups | map('extract', groups) | flatten | map('infer_address', hostvars) | list }}"
- name: determine differences
set_fact:
inventory_only_instances: "{{ inventory_instances | difference(tower_instances) }}"
tower_only_instances: "{{ tower_instances | difference(inventory_instances) }}"
- name: deprovision instances not in inventory
shell: "awx-manage deprovision_instance --hostname={{ item }}"
loop: "{{tower_only_instances}}"
delegate_to: "{{tower_host}}"
become: true
when: not ansible_check_mode
check_mode: no
module_defaults:
uri:
method: GET
user: "{{ tower_api_user }}"
password: "{{ tower_api_password }}"
validate_certs: False
status_code: [ 200, 201 ]
force_basic_auth: yes