Linux – generating ssh-keys for a non root user with salt

linuxsaltstackssh-keys

I need to generate ssh-keys(public/private pair) for a user say helen. This user is already in all my salt minions. How do I generate ssh keys for this user from my salt master?

For root user I generated the keys using this command and it worked fine:

salt '*application-server-*' cmd.run "ssh-keygen -q -N '' -f /root/.ssh/id_rsa"

I can issue the following command which can generate ssh-keys for helen; but the permissions and ownerships will be different; so need to issue commands again to correct permissions and ownerships:

salt '*application-server-*' cmd.run "ssh-keygen -q -N '' -f /home/helen/.ssh/id_rsa"

Is there any way in salt to run this command as user helen instead of root?

In general, is there a way in salt to run a command as a non root user?

Best Answer

Using the salt command

salt '*application-server-*' cmd.run \
    "ssh-keygen -q -N '' -f /home/helen/.ssh/id_rsa" \
    runas=helen

http://docs.saltstack.com/en/latest/ref/modules/all/salt.modules.cmdmod.html#salt.modules.cmdmod.run

Using a state

You can specify the user when using the cmd state by setting the runas to the user name to run the command as:

generate_ssh_key_helen:
  cmd.run:
    - name: ssh-keygen -q -N '' -f /home/helen/.ssh/id_rsa
    - runas: helen
    - unless: test -f /home/helen/.ssh/id_rsa

http://docs.saltstack.com/en/latest/ref/states/all/salt.states.cmd.html#salt.states.cmd.run