How can I check for a file on a salt file server

I want to copy ssh keys for users automatically, some users do not have keys.

Now I have:

ssh_auth:                                                                                                                                                                      
  - present                                                                                                                                                                    
  - user: {{ usr }}                                                                                                                                                            
  - source: salt://users/keys/{{usr}}.id_rsa.pub 

If the key for the user does not exist in the salt: // fileserver file, I get an error message. Is there any function to check for the presence of a file in the salt: // fileserver file?

+4
source share
2 answers

If you think you SHOULD learn how to do this using only states, you can use the backup mechanism by specifying a list of sources:

From the docs:

ssh_auth:                                                                        
  - present
  - user:{{usr}}
  - source:
    - salt://users/keys/{{usr}}.id_rsa.pub
    - salt://users/keys/null.id_rsa.pub

Where cat /dev/null > /srv/salt/users/keys/null.id_dsa.pub

, . , , , . - openvpn:

http://garthwaite.org/virtually-secure-with-openvpn-pillars-and-salt.html

+2

jinja salt, . , , jinja , . :

:

# Name of file : user_pillar.sls
users:
  root:
    ssh_key: some_key_value
    home : /root
    createhome: True

:

# Name of file : users_salt_state_file.sls
{% for user,args in salt['pillar.get']('users',{}).iteritems() %}
# Ensure user is present
{{ user }}_user:
  user.present:
    - name: {{ user }}

# Home Creation
{% if args and 'home' in args %} 
    - home: {{ args['home'] }}
{% endif %}
{% if args and 'createhome' in args %} 
    - createhome: {{ args['createhome'] }}
{% endif %}

# SSH_auth
{% if args and 'ssh_key' in args %}
{{ args['ssh_key'] }}
  ssh_auth:                                                                                                                                                                      
    - present                                                                                                                                                                    
    - user: {{ user }}  
{% endfor %}
+2

Source: https://habr.com/ru/post/1532996/


All Articles