Skip to content

Commit

Permalink
Exchange keys to allow for rsync
Browse files Browse the repository at this point in the history
  • Loading branch information
cailafinn committed Sep 24, 2024
1 parent f2cfcf7 commit 4e4d4f9
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Default values for creating external data mirror.

# IP of server containing external data main copy.
main_server_ip: "198.74.56.37"

# User on the main server.
main_server_user: root
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
- name: Generate key pair if it does not exist
community.crypto.openssh_keypair:
force: no # Don't regenerate existing keys.
path: ~/.ssh/id_rsa

- name: Read public key into tmp to copy over.
fetch:
src: ~/.ssh/id_rsa.pub
dest: /tmp/{{ ansible_hostname }}-id_rsa.pub
flat: yes

- name: Add public key to main server's authorized keys
ansible.posix.authorized_key:
user: "{{ main_server_user }}"
key: "{{ lookup('file','/tmp/{{ ansible_hostname }}-id_rsa.pub')}}"
remote_user: "{{ main_server_user }}"
delegate_to: "{{ main_server_ip }}"

- name: Touch the known_hosts file if it's missing
file:
path: ~/.ssh/known_hosts
state: touch
mode: 0644

- name: Check if known_hosts contains existing server fingerprint
command: ssh-keygen -F {{ main_server_user }}
register: key_exists
failed_when: key_exists.stderr != ''
changed_when: False

- name: Scan for existing remote ssh fingerprint
command: ssh-keyscan -T5 {{ main_server_ip }}
register: keyscan
failed_when: keyscan.rc != 0 or keyscan.stdout == ''
changed_when: False
when: key_exists.rc == 1

- name: Copy ssh-key to local known_hosts
lineinfile:
name: ~/.ssh/known_hosts
create: yes
line: "{{ item }}"
when: key_exists.rc == 1
with_items: "{{ keyscan.stdout_lines|default([]) }}"
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
state: directory
mode: '0755'

- name: Exchange SSH keys with linode so we can access the data.
import_tasks: exchange-keys.yml

0 comments on commit 4e4d4f9

Please sign in to comment.