diff --git a/tasks/borgbase.yml b/tasks/borgbase.yml index 1d665dc..ea7fdea 100644 --- a/tasks/borgbase.yml +++ b/tasks/borgbase.yml @@ -3,7 +3,6 @@ ansible.builtin.slurp: src: "{{ borg_ssh_key_file_path }}.pub" register: ssh_key - check_mode: true - name: Add key to borgbase adhawkins.borgbase.borgbase_ssh: diff --git a/tasks/main.yml b/tasks/main.yml index 5cb571e..69bad4a 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -9,11 +9,12 @@ - name: Setup borgbase backup ansible.builtin.import_tasks: borgbase.yml - when: borgbase is defined and borgbase + when: "'borgbase' in borg_providers" tags: borgbase - name: Setup Hetzner storagebox backup ansible.builtin.import_tasks: storagebox.yml + when: "'hetzner' in borg_providers" tags: storagebox - name: Setup borg diff --git a/tasks/ssh.yml b/tasks/ssh.yml index 202f042..ca79cda 100644 --- a/tasks/ssh.yml +++ b/tasks/ssh.yml @@ -22,10 +22,14 @@ owner: "{{ borg_user }}" group: "{{ borg_group }}" -- name: "SSH | Insert borgbase configuration" +- name: "SSH | Ensure configuration exists" + ansible.builtin.file: + path: .ssh/config + state: touch + mode: "0644" + +- name: "SSH | Create SSH configuration" ansible.builtin.blockinfile: - path: "{{ borg_user_ssh_config_path }}" + path: .ssh/config marker: "### {mark} ANSIBLE MANAGED BLOCK" - block: | - Host *.repo.borgbase.com - IdentityFile {{ borg_ssh_key_file_path }} + block: "{{ lookup('template', 'ssh_config.j2') }}" diff --git a/tasks/storagebox.yml b/tasks/storagebox.yml index 266fd93..cc06be1 100644 --- a/tasks/storagebox.yml +++ b/tasks/storagebox.yml @@ -1,12 +1,10 @@ --- - name: "Storagebox | Read key from file" - ansible.builtin.slurp: - src: "{{ borg_ssh_key_file_path }}.pub" + ansible.builtin.command: + cmd: "cat {{ borg_ssh_key_file_path }}.pub" register: ssh_key - check_mode: true - name: "Storagebox | Copy key to Hetzner" - ansible.posix.authorized_key: - user: "{{ hetzner_storagebox_user }}" - key: "{{ (ssh_key.content | b64decode) }}" - delegate_to: "{{ hetzner_storagebox }}" + ansible.builtin.command: + cmd: "echo {{ ssh_key.stdout_lines }} | ssh {{ hetzner_storagebox.user }}@{{ hetzner_storagebox.server }} -p {{ hetzner_storagebox.port }} install-ssh-key" + delegate_to: localhost diff --git a/templates/ssh_config.j2 b/templates/ssh_config.j2 new file mode 100644 index 0000000..420a303 --- /dev/null +++ b/templates/ssh_config.j2 @@ -0,0 +1,7 @@ +{% if 'borgbase' in borg_providers %} +Host *.repo.borgbase.com + IdentityFile {{ borg_ssh_key_file_path }} +{% elif 'hetzner' in borg_providers %} +Host *.your-storagebox.de + IdentityFile /root/.ssh/borgbackup +{% endif %}