--- - name: retrieve ssh key ids delegate_to: localhost check_mode: no uri: url: "https://api.hetzner.cloud/v1/ssh_keys" method: GET headers: Authorization: "Bearer {{ install.cloud.credentials.token }}" status_code: 200 register: sshkeys - 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.cloud.server_name | default(inventory_hostname) }}" method: GET headers: Authorization: "Bearer {{ install.cloud.credentials.token }}" status_code: 200 register: serverstatus - 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" - 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 body: "{{ {'type': 'linux64', 'ssh_keys': (sshkeys.json.ssh_keys | map(attribute='id') | list) } | to_nice_json }}" headers: Authorization: "Bearer {{ install.cloud.credentials.token }}" Content-Type: "application/json" status_code: 201 - name: wait for rescue mode activation pause: seconds: 5 - 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.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 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: 10 timeout: 300 - 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