Hi,
This is how i do it:
Generate the key:
We will append the username to the keys for better managability.
ssh-keygen -o -a 100 -t ed25519 -C “tsiamer@allttech.uk” -f $HOME/.ssh/$USER-linux.key -q -N “” 0>&-
This will translate to this:
ssh-keygen -o -a 100 -t ed25519 -C “tsiamer@allttech.uk” -f /home/tsiamer/.ssh/tsiamer-linux.key -q -N “” 0>&-
It will generate the files as so:
tsiamer-linux.key
tsiamer-linux.key.pub
Sign the keys:
vault write -field=signed_key ssh-client-signer/sign/tsiamer public_key=@$HOME/.ssh/$USER-linux.key.pub valid_principals=tsiamer > $HOME/.ssh/$USER-signed-key.pub
tsiamer-linux.key.pub
tsiamer-signed-key.pub
Or like so:
vault write -field=signed_key ssh-client-signer/sign/tsiamer public_key=@$HOME/.ssh/tsiamer-linux.key.pub valid_principals=tsiamer > /home/tsiamer/.ssh/ivo-signed-key.pub
Now to integrate with ansible,
In the inventory file we use the below, in production “real life” we have many sysadmins with different usernames, how to achieve that with ansible we could use this but:
[MDCDomainControllers:vars]
ansible_ssh_user=tsiamer
ansible_ssh_private_key_file=/home/tsiamer/.ssh/tsiamer-signed-key.pub
group_name=k8snodes
Unfortunatly not everyone “initial surname” is tsiamer, and ansible will not work as we have to append vault ssh pvt key.
So the way we would achieve this:
[MDCDomainControllers:vars]
ansible_user= “{{ lookup(‘env’, ‘USER’) }}”
ansible_ssh_private_key_file= “{{ lookup(‘env’, ‘HOME’) }}/.ssh/{{ lookup(‘env’, ‘USER’) }}-linux.key”
ansible_ssh_extra_args= " -i {{ lookup(‘env’, ‘HOME’) }}/.ssh/{{ lookup(‘env’, ‘USER’) }}-signed-key.pub"
So now we could enumerate any logged user "ansible_user= “{{ lookup(‘env’, ‘USER’) }}”, 2nd line to get the private key, 3rd line - important we append this ansible_ssh_extra_args= " -i and lookup the signed public key for the current user.
now running:
ansible-playbook blabla.yml
Above will work for any logged user as long as he/she are authorised and have cert signed by Vault.
Then depending on your environment, if you want implement the same for all inventory groups.
To summarise only add this to the inventory or whatever suits you.
[MDCDomainControllers:vars]
ansible_user= “{{ lookup(‘env’, ‘USER’) }}”
ansible_ssh_private_key_file= “{{ lookup(‘env’, ‘HOME’) }}/.ssh/{{ lookup(‘env’, ‘USER’) }}-linux.key”
ansible_ssh_extra_args= " -i {{ lookup(‘env’, ‘HOME’) }}/.ssh/{{ lookup(‘env’, ‘USER’) }}-signed-key.pub"
Hope this help,
Regards
Siamert