Tuesday, 9 January 2018

Create users and deploy ssh key from master to minion via salt pillars

Step 1: Define users in /srv/pillar/users.sls

users:

 test:
    fullname: Test User
    email: testuser@example.com
    uid: 5004
    gid: 5004
    shell: /bin/bash
    home: /home/test
    groups:
      - dev
    authkey: ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAQEAiTfNNhHbM3Db3DgO9OF+uJFZNs51FHhzp3W65ABZW2ZocIa8XmzY+ZVgJSNoRpCSIw2MCKZ+VNXyYtMua8rVkOQddR0O0coeQ5j1DUFDDNse//WO334wUUxL0J+VfFDXNvCJNi8Y0rnJclksLKz/uxBuXN+6Y1OlX/nYKv35XGna3I1UM1nWYHmlm1cVOl5ZnZ7+rW+Q9OCsZqx6EjQggyOME9B1nJgnJj4oAaD5ocs5o11KGBowmik8tqa1gTExyQ9ptmLpWHyRRDs+yiFsCD0QkeCY2MIRQPKdId/ijxhKKakZoRqh6WoHnb/z6Wy5mVQ1a4XuQbs55scZq3mFaGQ== testuser@example.com

Step 2: Add the new pillar to /srv/pillar/top.sls

base:
  'saltminion01':
    - users

Step 3: Use jinja to map pillar to states in /srv/salt/user/init.sls

{% for user, args in pillar['users'].iteritems() %}
"{{ user }}":
  group.present:
    - gid: {{ args['gid'] }}
  user.present:
    - home: {{ args['home'] }}
    - shell: {{ args['shell'] }}
    - uid: {{ args['uid'] }}
    - gid: {{ args['gid'] }}
    - fullname: {{ args['fullname'] }}

  file.directory:
    - name: /home/{{user}}
    - user: {{user}}
    - group: {{user}}
    - mode: 0750
    - makedirs: True

user_{{user}}_sshdir:
  file.directory:
    - name: /home/{{user}}/.ssh
    - user: {{user}}
    - group: {{user}}
    - mode: 0700

user_{{user}}_authkeys:
  ssh_auth.present:
    - user: {{user}}
    - name: {{ args['authkey'] }}

{% endfor %}


Step 4: Test and push the changes

salt 'saltminion01' state.show_sls users

Above command will display if there is any errors in the state file, if there is no errors you are good to go and push the changes.


salt 'saltminion01' state.sls users


Now you can login to “saltminion01” with your private key.

No comments:

Post a Comment