forked from jriguera/ansible-ironic-standalone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdel-baremetal.yml
119 lines (103 loc) · 4.2 KB
/
del-baremetal.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
---
- name: Define and deploy physical servers using Ironic
hosts: ironic-api
become: yes
gather_facts: yes
vars_prompt:
- name: "id"
prompt: "Server Name"
private: no
pre_tasks:
- name: "Load Ironic (encrypted) variables"
include_vars: "{{ item }}"
with_first_found:
- "vars/{{ deployment }}/Ironic.yml"
- "vars/Ironic.yml"
- name: "Load (encrypted) baremetal variables"
include_vars: "{{ item }}"
with_first_found:
- "vars/{{ deployment }}/Baremetal.yml"
- "vars/Baremetal.yml"
# Minimun set of variables required, defaults are taken from the previous load
- name: Load hostvars
set_fact:
baremetal_ipmi_ip: "{{ hostvars[id]['baremetal_ipmi_ip'] | default(Baremetal_ipmi_ip) }}"
baremetal_ipmi_user: "{{ hostvars[id]['baremetal_ipmi_user'] | default(Baremetal_ipmi_user) }}"
baremetal_ipmi_pass: "{{ hostvars[id]['baremetal_ipmi_pass'] | default(Baremetal_ipmi_pass) }}"
baremetal_os: "{{ hostvars[id]['baremetal_os'] | default(Baremetal_os) }}"
baremetal_macs: "{{ hostvars[id]['baremetal_macs'] | default(Baremetal_macs) }}"
baremetal_network: "{{ hostvars[id]['baremetal_network'] | default(Baremetal_network) }}"
baremetal_fqdn: "{{ hostvars[id]['baremetal_fqdn'] | default(Baremetal_fqdn) }}"
baremetal_domain: "{{ hostvars[id]['baremetal_domain'] | default(Baremetal_domain) }}"
baremetal_name: "{{ hostvars[id]['baremetal_name'] | default(Baremetal_name) }}"
baremetal_use_configdrive: "{{ hostvars[id]['baremetal_use_configdrive'] | default(Baremetal_use_configdrive) }}"
- name: Load image variable definitions
include_vars: "{{ item }}"
with_first_found:
- "vars/{{ deployment }}/images/{{ baremetal_os }}.yml"
- "vars/images/{{ baremetal_os }}.yml"
# Sanity checks of the variables and check Ironic server definition
- include: tasks/baremetal_prepare.yml
when: id != 'vbox'
# Sanity checks of the variables and check Ironic server definition
- include: tasks/baremetal_vbox.yml
when: id == 'vbox'
tasks:
- name: Get list of defined servers
shell: >
ironic
--os-auth-token="{{ Ironic_api_token }}"
--ironic-url="{{ Ironic_api_public_url }}"
node-list | awk -F '|' '/ {{ baremetal_name }} / { printf("%s\n%s\n%s",$2,$5,$6) }'
register: __baremetal_new
delegate_to: "{{ groups.client[0] }}"
ignore_errors: yes
become: no
run_once: yes
- name: Checking if server name exists
fail:
msg: "Server name '{{ baremetal_name }}' does not exist!"
when: __baremetal_new.stdout | trim == ''
- name: Define server facts
set_fact:
_baremetal_new: "{{ __baremetal_new.stdout_lines }}"
- name: Checking if server status is correct
fail:
msg: "Server name '{{ baremetal_name }}' is not in <available> or <enroll> status"
when: >
not (_baremetal_new[2] | trim == 'available'
or _baremetal_new[2] | trim == 'enroll')
- name: Power off the server
command: >
ironic
--os-auth-token="{{ Ironic_api_token }}"
--ironic-url="{{ Ironic_api_public_url }}"
node-set-power-state "{{ baremetal_name }}" off
when: _baremetal_new[1].split() | last | trim == 'on'
delegate_to: "{{ groups.client[0] }}"
become: no
run_once: yes
- name: Pause half a minute
pause:
seconds: 30
prompt: "Giving some time to stop the server ..."
when: _baremetal_new[1].split() | last | trim == 'on'
delegate_to: "{{ groups.client[0] }}"
become: no
run_once: yes
- name: Delete the server definition from Ironic
command: >
ironic
--os-auth-token="{{ Ironic_api_token }}"
--ironic-url="{{ Ironic_api_public_url }}"
node-delete "{{ baremetal_name }}"
delegate_to: "{{ groups.client[0] }}"
become: no
run_once: yes
- name: Delete config-drive from the repository
file:
path: "{{ Ironic_metadata_path }}/{{ item }}"
state: absent
with_items:
- "{{ _baremetal_new[0] | trim }}"
- "{{ _baremetal_new[0] | trim }}.cfgd"