deploy ssh key from master to minion via salt pillars
Solution 1:
The Salt Pillar system does not have an init.sls file. Both states and pillars has a top.sls file. States that are subdirectories may have an init.sls file.
Step 1: Define your users in /srv/pillar/users.sls
users:
- name: fred
fullname: Fred Flintstone
email: [email protected]
uid: 4001
gid: 4001
shell: /bin/bash
groups:
- bowling
shadow: $6$Sasdf/Ss$asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfsadfasdfsadfsadfsdf
authkey: ssh-dss AAAAasdfasdfsadfsadfsadfsadfasdfasdfsdafsdafaasdfasdfasdfasdfasdfsadfsadfsadfsadfasdfasdfsdafsdafaasdfasdfasdfasdfasdfsadfsadfsadfsadfasdfasdfsdafsdafaasdfasdfasdfasdfasdfsadfsadfsadfsadfasdfasdfsdafsdafa = [email protected]
sshpub: ssh-dss AAAAasdfasdfsadfsadfsadfsadfasdfasdfsdafsdafaasdfasdfasdfasdfasdfsadfsadfsadfsadfasdfasdfsdafsdafaasdfasdfasdfasdfasdfsadfsadfsadfsadfasdfasdfsdafsdafaasdfasdfasdfasdfasdfsadfsadfsadfsadfasdfasdfsdafsdafa = [email protected]
- name: barney
fullname: Barney Rubble
email: [email protected]
uid: 4002
gid: 4002
shell: /bin/bash
groups:
- bowling
shadow: $6$Suiop/Ss$uiopuiopuiopuiopuiopuiopuiopuiopuiopuiopuiopsadfuiopsadfsadfsdf
authkey: ssh-dss AAAAuiopuiopsadfsadfsadfsadfuiopuiopsdafsdafauiopuiopuiopuiopuiopsadfsadfsadfsadfuiopuiopsdafsdafauiopuiopuiopuiopuiopsadfsadfsadfsadfuiopuiopsdafsdafauiopuiopuiopuiopuiopsadfsadfsadfsadfuiopuiopsdafsdafa = [email protected]
sshpub: ssh-dss AAAAuiopuiopsadfsadfsadfsadfuiopuiopsdafsdafauiopuiopuiopuiopuiopsadfsadfsadfsadfuiopuiopsdafsdafauiopuiopuiopuiopuiopsadfsadfsadfsadfuiopuiopsdafsdafauiopuiopuiopuiopuiopsadfsadfsadfsadfuiopuiopsdafsdafa = [email protected]
Step 2: Add the new pillar to /srv/pillar/top.sls
base:
'testminion':
- users
Step 3: Use jinja to map pillar to states in /srv/salt/user/init.sls
{% for user in pillar['users'] %}
user_{{user.name}}:
group.present:
- name: {{user.name}}
- gid: {{user.gid}}
user.present:
- name: {{user.name}}
- fullname: {{user.fullname}}
- password: {{user.shadow}}
- shell: {{user.shell}}
- uid: {{user.uid}}
- gid: {{user.gid}}
{% if user.groups %}
- optional_groups:
{% for group in user.groups %}
- {{group}}
{% endfor %}
{% endif %}
- require:
- group: user_{{user.name}}
file.directory:
- name: /home/{{user.name}}
- user: {{user.name}}
- group: {{user.name}}
- mode: 0751
- makedirs: True
user_{{user.name}}_forward:
file.append:
- name: /home/{{user.name}}/.forward
- text: {{user.email}}
user_{{user.name}}_sshdir:
file.directory:
- name: /home/{{user.name}}/.ssh
- user: {{user.name}}
- group: {{user.name}}
- mode: 0700
{% if 'authkey' in user %}
user_{{user.name}}_authkeys:
ssh_auth.present:
- user: {{user.name}}
- name: {{user.authkey}}
{% endif %}
{% if 'sshpriv' in user %}
user_{{user.name}}_sshpriv:
file.managed:
- name: /home/{{user.name}}/.ssh/id_rsa
- user: {{user.name}}
- group: {{user.name}}
- mode: 0600
- contents_pillar: {{user.sshpriv}}
{% endif %}
{% if 'sshpub' in user %}
user_{{user.name}}_sshpub:
file.managed:
- name: /home/{{user.name}}/.ssh/id_rsa.pub
- user: {{user.name}}
- group: {{user.name}}
- mode: 0600
- contents_pillar: {{user.sshpub}}
{% endif %}
{% endfor %} # user in users
# vim: ft=yaml tabstop=2 sts=2 sw=2 et ai si
Don't forget to sync the minions with the new pillars!
salt targetminions saltutil.refresh_pillar
Solution 2:
It should probably be noted that in relation to the original question, there's another simple solution if source: salt://...
format doesn't work with file.managed
- as it still happened with salt-ssh
because of bug https://github.com/saltstack/salt/issues/38458 that was since fixed - and that is to switch to contents:
with the file tree external pillar, which is also backed by files on the master.
The file_tree
ext_pillar
is documented at https://docs.saltstack.com/en/latest/ref/pillar/all/salt.pillar.file_tree.html#module-salt.pillar.file_tree nowadays. It has existed since version 2015.5.0 so it's newer than the original question and answer, yet it is a solution that is reasonably well available today.
Indeed, it is also in the FAQ at https://docs.saltstack.com/en/latest/faq.html#is-it-possible-to-deploy-a-file-to-a-specific-minion-without-other-minions-having-access-to-it