summaryrefslogtreecommitdiff
path: root/roles/cloud/install/tasks/hroot.yml
blob: 3d7ba36b4764db23b2e4e3856e6c3b77cdf88be0 (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
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
---
- name: retrieve ssh key fingerprints
  uri:
    url: "https://robot-ws.your-server.de/key"
    method: GET
    user: "{{ install_cooked.cloud.credentials.username }}"
    password: "{{ install_cooked.cloud.credentials.password }}"
    force_basic_auth: yes
    status_code: 200
  register: sshkeys
  delegate_to: localhost
  check_mode: no

- name: do not continue in check mode
  fail:
    msg: "can not bootstrap new servers in check mode"
  when: ansible_check_mode | bool
  check_mode: no

- block:
  - name: retrieve server list from robot
    uri:
      url: "https://robot-ws.your-server.de/server"
      method: GET
      user: "{{ install_cooked.cloud.credentials.username }}"
      password: "{{ install_cooked.cloud.credentials.password }}"
      force_basic_auth: yes
      status_code: 200
    register: servers
    delegate_to: localhost
    check_mode: no

  - name: extract server IP address from robot result
    set_fact:
      hetzner_main_ip: "{{ servers.json | hroot_extract_serverip(install_cooked.cloud.server_name | default(inventory_hostname)) }}"

  when: hetzner_main_ip is not defined

- name: display warning message
  pause:
    prompt: |
      *** Danger ****
      will be bootstraping host {{ inventory_hostname }} with main IP {{ hetzner_main_ip }} ...
      ALL DATA WILL BE LOST!!! press CTRL-C then A to abort.
    seconds: 15

- name: check if rescue mode is already active
  uri:
    url: "https://robot-ws.your-server.de/boot/{{ hetzner_main_ip }}/rescue"
    method: GET
    user: "{{ install_cooked.cloud.credentials.username }}"
    password: "{{ install_cooked.cloud.credentials.password }}"
    force_basic_auth: yes
    status_code: 200
  register: rescuestatus
  delegate_to: localhost
  check_mode: no

### TODO: for now we add all ssh keys that are installed in the robot - this might not be a good idea!
- name: activate rescue mode
  when: not rescuestatus.json.rescue.active
  uri:
    url: "https://robot-ws.your-server.de/boot/{{ hetzner_main_ip }}/rescue"
    method: POST
    user: "{{ install_cooked.cloud.credentials.username }}"
    password: "{{ install_cooked.cloud.credentials.password }}"
    force_basic_auth: yes
    body: "os=linux&arch=64&authorized_key[]={{ sshkeys.json | hroot_extract_ssh_key_fingerprints | join('&authorized_key[]=') }}"
    status_code: 200
    headers:
      Content-Type: "application/x-www-form-urlencoded"
  delegate_to: localhost

- name: wait for the rescue mode to become active
  pause:
    seconds: 5

- name: do a hardware reset
  uri:
    url: "https://robot-ws.your-server.de/reset/{{ hetzner_main_ip }}"
    method: POST
    user: "{{ install_cooked.cloud.credentials.username }}"
    password: "{{ install_cooked.cloud.credentials.password }}"
    force_basic_auth: yes
    body: "type=hw" ## type=sw -> CTRL-ALT-DEL, type=hw -> reset button
    status_code: 200
    headers:
      Content-Type: "application/x-www-form-urlencoded"
  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

- include_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