This repository has been archived by the owner on Dec 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
hard-reset.yml
171 lines (149 loc) · 5.27 KB
/
hard-reset.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
---
- name: RESET SERVER
hosts: localhost, baremetal
tags: hetzner_reset, hetzner_rescue
tasks:
- name: Prompt for confirmation
ansible.builtin.pause:
prompt: "This will boot the server into rescue mode and DELETE ALL DATA. Type 'yes' in all caps to confirm"
register: confirmation
run_once: true
delegate_to: localhost
- name: Fail if confirmation is not 'YES'
ansible.builtin.fail:
msg: "Confirmation not 'YES'"
when:
- confirmation.user_input != "YES"
- name: Prompt for confirmation
ansible.builtin.pause:
prompt: "ARE YOU SURE? Type 'yes' in all caps to confirm"
register: confirmation_2
run_once: true
delegate_to: localhost
- name: Fail if confirmation is not 'YES'
ansible.builtin.fail:
msg: "Confirmation not 'YES'"
when:
- confirmation_2.user_input != "YES"
- name: Query for servers
community.hrobot.server_info:
hetzner_user: "{{ hetzner_server_api_username }}"
hetzner_password: "{{ hetzner_server_api_password }}"
register: hrobot_servers
when: hetzner_server_number is not defined
run_once: true
delegate_to: localhost
- name: Select server automatically
ansible.builtin.set_fact:
hetzner_server_number: "{{ hrobot_servers.servers[0].server_number }}"
when:
- hetzner_server_number is not defined
- hrobot_servers.servers | length == 1
- name: Query SSH keys
community.hrobot.ssh_key_info:
hetzner_user: "{{ hetzner_server_api_username }}"
hetzner_password: "{{ hetzner_server_api_password }}"
register: hrobot_keys
when: hetzner_ssh_key is not defined
run_once: true
delegate_to: localhost
- name: Select SSH key automatically
ansible.builtin.set_fact:
hetzner_server_authorized_key: "{{ hrobot_keys.ssh_keys[0].fingerprint }}"
when:
- hetzner_ssh_key is not defined
- hrobot_keys.ssh_keys | length == 1
- name: "Enable rescue system for next boot"
community.hrobot.boot:
hetzner_user: "{{ hetzner_server_api_username }}"
hetzner_password: "{{ hetzner_server_api_password }}"
server_number: "{{ hetzner_server_number | int }}"
rescue:
arch: 64
os: linux
authorized_keys: "{{ hetzner_server_authorized_key }}"
run_once: true
delegate_to: localhost
- name: Wait for 10 seconds
ansible.builtin.pause:
seconds: 10
- name: Try to reboot the server
block:
- name: Reboot server
ansible.builtin.reboot:
msg: "Reboot initiated by Ansible"
reboot_timeout: 600
run_once: true
delegate_to: omori
rescue:
- name: Reset via HRobot
community.hrobot.reset:
hetzner_user: "{{ hetzner_server_api_username }}"
hetzner_password: "{{ hetzner_server_api_password }}"
server_number: "{{ hetzner_server_number | int }}"
reset_type: "hardware"
run_once: true
delegate_to: localhost
- name: Clear facts
ansible.builtin.meta: clear_facts
- name: Wait for host to come back
ansible.builtin.wait_for_connection:
delay: 10
timeout: 600
- name: If we are running on a Hetzner rescue system, install Debian
hosts: baremetal
gather_facts: true
tags: hetzner_rescue, install_os
tasks:
- name: Check if /root/.oldroot/nfs/install/installimage exists
ansible.builtin.stat:
path: /root/.oldroot/nfs/install/installimage
register: installimage
- name: Install Debian 11
when:
- installimage.stat.exists
- ansible_facts['distribution'] == 'Debian'
- "'rescue' in ansible_facts['cmdline'].BOOT_IMAGE"
- "'rescue' in ansible_facts['cmdline'].initrd"
- ansible_facts['hostname'] == 'rescue'
block:
- name: Install Debian image
ansible.builtin.command: |
/root/.oldroot/nfs/install/installimage \
-a \
-i /root/images/Debian-1107-bullseye-amd64-base.tar.gz \
-t yes \
-s en \
-r yes \
-l 1
changed_when: true # Always true, this reinstalls the system..
- name: Reboot the server
ansible.builtin.reboot:
msg: "Reboot initiated by Ansible"
reboot_timeout: 600
- name: Clear facts
ansible.builtin.meta: clear_facts
- name: Wait for host to come back
ansible.builtin.wait_for_connection:
delay: 10
timeout: 600
- name: Gather facts
ansible.builtin.setup:
- name: Remove ssh host key from localhost
ansible.builtin.command: |
ssh-keygen -f "~/.ssh/known_hosts" -R "{{ hostvars['omori']['ansible_facts']['default_ipv4']['address'] }}"
run_once: true
changed_when: true
delegate_to: localhost
- name: Remove tfstate file
ansible.builtin.file:
path: "{{ playbook_dir }}/../../terraform/{{ item }}"
state: absent
run_once: true
delegate_to: localhost
loop:
- vms/terraform.tfstate
- vms/terraform.tfstate.backup
- rancher/terraform.tfstate
- rancher/terraform.tfstate.backup
...