Search code examples
salt-project

How can I check for file existence in salt file server


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

What I have now is:

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

When a key for a user does not exist on salt:// fileserver, I get an error. Is there some function to check for existence of a file in salt:// fileserver?


Solution

  • If you feel you MUST learn how to do this with just states, you can use the fallback 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

    Professionally, user keys should be stored in pillars. This presents the additional functionality that pillars are stored and retrieved from the master at execution time - which means you can test for the existence of the file per your original request. I do something just like that for openvpn certificates:

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