blob: a4c61c0f8c7bb9266b39c1a8c137a9b56521e105 (
plain) (
blame)
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
|
---
- name: retrieve ssh key ids
uri:
url: "https://api.hetzner.cloud/v1/ssh_keys"
method: GET
headers:
Authorization: "Bearer {{ hcloud_api_token }}"
status_code: 200
register: sshkeys
delegate_to: localhost
- name: retrieve server id and check if rescue mode is already active
uri:
url: "https://api.hetzner.cloud/v1/servers?name={{ inventory_hostname }}"
method: GET
headers:
Authorization: "Bearer {{ hcloud_api_token }}"
status_code: 200
register: serverstatus
delegate_to: localhost
- name: do not continue in check mode
fail:
msg: "can not bootstrap new servers in check mode"
when: ansible_check_mode
check_mode: no
### TODO: for now we add all ssh keys that are installed for this project - this might not be a good idea!
- name: activate rescue mode
when: not serverstatus.json.servers[0].rescue_enabled
uri:
url: "https://api.hetzner.cloud/v1/servers/{{ serverstatus.json.servers[0].id }}/actions/enable_rescue"
method: POST
body: "{{ {'type': 'linux64', 'ssh_keys': (sshkeys.json.ssh_keys | map(attribute='id') | list) } | to_nice_json }}"
headers:
Authorization: "Bearer {{ hcloud_api_token }}"
Content-Type: "application/json"
status_code: 201
delegate_to: localhost
- name: do a hardware reset
uri:
url: "https://api.hetzner.cloud/v1/servers/{{ serverstatus.json.servers[0].id }}/actions/reset"
method: POST
headers:
Authorization: "Bearer {{ hcloud_api_token }}"
status_code: 201
delegate_to: localhost
### TODO: would be nice to get the SSH host key from robot
- name: completely ignore ssh host keys for now
set_fact:
old_ansible_ssh_extra_args: "{{ ansible_ssh_extra_args | default('') }}"
ansible_ssh_extra_args: "{{ ansible_ssh_extra_args | default('') }} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
- name: wait for rescue system to start up
wait_for_connection:
delay: 30
timeout: 120
- import_tasks: hetzner_installimage.yml
- name: reboot
shell: sleep 2 && shutdown -r now "triggered by ansible after running installimage"
async: 1
poll: 0
ignore_errors: True
changed_when: True
### TODO: SSH host key handling needs to be improved
- name: automatically accept new ssh host key
set_fact:
ansible_ssh_extra_args: "{{ old_ansible_ssh_extra_args }} -o StrictHostKeyChecking=no"
- name: wait for host to start up
wait_for_connection:
delay: 15
timeout: 120
### TODO: SSH host key handling needs to be improved
- name: re-enable ssh host key checking
set_fact:
ansible_ssh_extra_args: "{{ old_ansible_ssh_extra_args }}"
|