From 5486750b209896d2b15f12cec72abdbc55b2279b Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Sat, 18 Apr 2020 20:15:39 +0200 Subject: cleanup hetzner cloud-install --- roles/cloud/install/tasks/hcloud.yml | 49 +++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 18 deletions(-) (limited to 'roles/cloud/install/tasks/hcloud.yml') diff --git a/roles/cloud/install/tasks/hcloud.yml b/roles/cloud/install/tasks/hcloud.yml index 7e87c114..983b3dec 100644 --- a/roles/cloud/install/tasks/hcloud.yml +++ b/roles/cloud/install/tasks/hcloud.yml @@ -1,5 +1,7 @@ --- - name: retrieve ssh key ids + delegate_to: localhost + check_mode: no uri: url: "https://api.hetzner.cloud/v1/ssh_keys" method: GET @@ -7,9 +9,10 @@ Authorization: "Bearer {{ install_cooked.cloud.credentials.token }}" status_code: 200 register: sshkeys - delegate_to: localhost - name: retrieve server id and check if rescue mode is already active + delegate_to: localhost + check_mode: no uri: url: "https://api.hetzner.cloud/v1/servers?name={{ install_cooked.cloud.server_name | default(inventory_hostname) }}" method: GET @@ -17,17 +20,29 @@ Authorization: "Bearer {{ install_cooked.cloud.credentials.token }}" status_code: 200 register: serverstatus - delegate_to: localhost + +- name: do not continue if we found no or multiple servers + when: (serverstatus.json.servers | length) != 1 + fail: + msg: "hcloud API returned {{ serverstatus.json.servers | length }} servers" - name: do not continue in check mode + when: ansible_check_mode | bool fail: msg: "can not bootstrap new servers in check mode" - when: ansible_check_mode | bool - check_mode: no + +- name: display warning message + pause: + prompt: | + *** Danger **** + will be bootstraping host {{ inventory_hostname }} with main IP {{ serverstatus.json.servers[0].public_net.ipv4.ip }} ... + ALL DATA WILL BE LOST!!! press CTRL-C then A to abort. + seconds: 15 ### 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 + delegate_to: localhost uri: url: "https://api.hetzner.cloud/v1/servers/{{ serverstatus.json.servers[0].id }}/actions/enable_rescue" method: POST @@ -36,21 +51,19 @@ Authorization: "Bearer {{ install_cooked.cloud.credentials.token }}" Content-Type: "application/json" status_code: 201 - delegate_to: localhost -## TODO: remove this once the task below is fixed -- pause: - prompt: Please reset the server {{ install_cooked.cloud.server_name | default(inventory_hostname) }} and press enter... +- name: wait for rescue mode activation + pause: + seconds: 5 -### TODO this does not work???? -# - 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 {{ install_cooked.cloud.credentials.token }}" -# status_code: 201 -# delegate_to: localhost +- name: do a hardware reset + delegate_to: localhost + uri: + url: "https://api.hetzner.cloud/v1/servers/{{ serverstatus.json.servers[0].id }}/actions/reset" + method: POST + headers: + Authorization: "Bearer {{ install_cooked.cloud.credentials.token }}" + status_code: 201 ### TODO: would be nice to get the SSH host key from robot - name: completely ignore ssh host keys for now @@ -63,7 +76,7 @@ delay: 30 timeout: 120 -- include_tasks: hetzner_installimage.yml +- import_tasks: hetzner_installimage.yml - name: reboot shell: sleep 2 && shutdown -r now "triggered by ansible after running installimage" -- cgit v1.2.3