I would like to use Ansible to
- Generate and encrypt an SSH key pair on the target
- Add the SSH public key to GitHub
- Clone a private GitHub repository
I explicitly do not want to forward any SSH keys from the control node to the target (never understood the point of doing this) or even worse, copy SSH keys from the control node to the target. I also don't want to keep the private key on the target in plain text. All answers to other questions I've seen always suggest one of these three ways, all of which are bad.
Instead, I want to generate an own pair of SSH keys on each target and of course encrypt the private key so that it doesn't lie around on the target in plain text.
I've so far not managed to do this. Can anyone help?
Here is what I've tried so far (assume all variables used exist):
- name: Generate target SSH key for accessing GitHub
command: "ssh-keygen -t ed25519 -f {{ github_ssh_key_path }} -N '{{ github_ssh_key_encryption_password }}'"
- name: Fetch local target SSH public key
command: "cat {{ github_ssh_key_path }}.pub"
register: ssh_pub_key
- name: Authorize target SSH public key with GitHub
github_key:
name: Access key for target "{{ target_serial }}"
pubkey: "{{ ssh_pub_key.stdout }}"
token: "{{ github_access_token }}"
- name: Clone private git repository
git:
repo: git@github.com:my_org/private_repo.git
clone: yes
dest: /path/to/private_repo
The problem with the encrypted key is that I then get a "permission denied" error from GitHub. I probably need to add the key to ssh-agent
, but I haven't been able to figure out how. Or is there an alternative?
The above works fine if I do not encrypt the SSH private key, i.e. if I do
- name: Generate target SSH key for accessing GitHub
command: "ssh-keygen -t ed25519 -f {{ github_ssh_key_path }} -N ''"
instead of the command above, but of course I don't want that.