Defining states depending on existance of a file/directory

前端 未结 3 655
广开言路
广开言路 2021-01-06 11:33

How is it possible to get something like the following running:

{% if not exist(\'/tmp/dummy/\') then %}
dummy:
  file.touch:
    - name: /tmp/dummy/tmp.txt
         


        
相关标签:
3条回答
  • 2021-01-06 11:53

    You could use the pure python renderer for salt. Here is how it would look like.

    #!py
    import os
    
    def run():
      # defines the hash config
      config = {}
    
      if (not os.path.isfile("/tmp/dummy")):
        config["dummy"] = { 
          "file.touch": [
            {'name': '/tmp/dummy'},
          ],  
        }    
    
      return config
    
    0 讨论(0)
  • 2021-01-06 11:55

    You can use unless for this.

    dummy:
      file.touch:
        - name: /tmp/dummy/tmp.txt
        - unless: test -d /tmp/dummy/
    
    0 讨论(0)
  • 2021-01-06 11:56
    {% if 1 == salt['cmd.retcode']('test -f /tmp/woo.test') %}
    ack:
      file.touch:
        - name: /tmp/woo.test
    {% endif %}
    
    0 讨论(0)
提交回复
热议问题