Skip to content

Instantly share code, notes, and snippets.

@eddieparker
Last active December 4, 2021 11:21
Show Gist options
  • Save eddieparker/27ed73e657338f2c0c6ef53464343748 to your computer and use it in GitHub Desktop.
Save eddieparker/27ed73e657338f2c0c6ef53464343748 to your computer and use it in GitHub Desktop.
KeeAgent ansible playbook for ubuntu on WSL
# Instructions forged into playbook from https://gist.github.com/strarsis/e533f4bca5ae158481bbe53185848d49
# To use:
# - Install ansible (apt-get update -y && apt-get install -y ansible)
# - Run "ansible-playbook playbook.yml" wherever you downloaded this gist to.
# - Don't forget to "Enable agent for Windows OpenSSH (experimental)" in keepass to allow this to work.
---
- name: Setup for keepass
hosts: localhost
remote_user: root
tasks:
- name: Install required modules
become: true
apt:
name:
- socat
- p7zip-full
state: present
update-cache: true
- name: Get wsl-ssh-agent
become: true
args:
creates: /usr/local/bin/npiperelay.exe
warn: false # Don't warn about wget; I'm lazy and don't want another command right now.
shell: |
wget https://github.com/rupor-github/wsl-ssh-agent/releases/download/v1.5.2/wsl-ssh-agent.zip -P /tmp
sudo 7z e -y /tmp/wsl-ssh-agent.zip -o/usr/local/bin/
sudo chmod +x /usr/local/bin/npiperelay.exe
rm /tmp/wsl-ssh-agent.zip
- name: Create ~/bin
file:
path: ~/bin
state: directory
mode: '0755'
- name: Create ~/bin/wsl-agent-forwarder
copy:
mode: u+wrx
dest: ~/bin/wsl-agent-forwarder
content: |
#!/bin/bash
# Usage: wsl-ssh-agent-forward [ -k | -r ]
# Options:
# -k Kill the current process (if exists) and do not restart it.
# -r Kill the current process (if exists) and restart it.
# Default operation is to start a process only if it does not exist.
export SSH_AUTH_SOCK=$HOME/.ssh/agent.sock
sshpid=$(ss -ap | grep "$SSH_AUTH_SOCK")
if [ "$1" = "-k" ] || [ "$1" = "-r" ]; then
sshpid=${sshpid//*pid=/}
sshpid=${sshpid%%,*}
if [ -n "${sshpid}" ]; then
kill "${sshpid}"
else
echo "'socat' not found or PID not found"
fi
if [ "$1" = "-k" ]; then
exit
fi
unset sshpid
fi
if [ -z "${sshpid}" ]; then
rm -f $SSH_AUTH_SOCK
( setsid socat UNIX-LISTEN:$SSH_AUTH_SOCK,fork EXEC:"/usr/local/bin/npiperelay.exe -ei -s //./pipe/openssh-ssh-agent",nofork & ) >/dev/null 2>&1
fi
#Add to .bashrc:
#
## KeeAgent
#. ~/bin/wsl-ssh-agent-forwarder
- name: Add forwarder to .bashrc
lineinfile:
path: ~/.bashrc
line: . ~/bin/wsl-agent-forwarder
create: yes
#Make sure socket exists:
#
#mkdir -p $HOME/.ssh
#touch $HOME/.ssh/agent.sock
- name: Create ~/.ssh
file:
path: ~/.ssh
state: directory
mode: '0700'
- name: Ensure socket exists
file:
path: ~/.ssh/agent.sock
state: touch
modification_time: preserve
access_time: preserve
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment