SaltStack: create ssh keypair and add it to authorized_keys

Goal

For testing, we want this to work: ssh $USER@localhost.

  1. create rsa keypair in ~/.ssh/, if not already there
  2. add .ssh/id_rsa.pub to .ssh/authorized_keys, if not already in this file.

Question

How to do this with salt-stack?

Answer

State:

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

State for authorized_key:

  ssh_auth.present:
   - user: my_user
   - require:
     - user: my_user
   - names:
     - ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDGt6eIzilRygebgpzpRwVgja7NbXrGHgG7QbIxOhSUpwmuzJ7nHELrbbA9z+CyTFZwRtpr27OQDv7L8ox5Wp6iCFxyb5Y8sVC8vyYNoFPTfMz4qtgf0xXZRDAzzkeczuMqQubnJtanxhR7t9H2RBSxLvZkqD18O/GekCXBmR43yrBi03rVHcvumTW6m5Kg5qihq/adhVQDutiCp3ICq/blahbasd my_user@

that ssh-rsa is your generated .pub key

Attribution
Source : Link , Question Author : guettli , Answer Author : Danila Ladner

Leave a Comment