Defining states depending on existance of a file/directory

拜拜、爱过 提交于 2019-11-30 17:48:10

问题


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

...
{% endif %}

I need it for installing software from a ZIP-file. I want to unzip on the minions, but there I don't want to have any remnants of licence files, which I only need for installation, left.


回答1:


You can use unless for this.

dummy:
  file.touch:
    - name: /tmp/dummy/tmp.txt
    - unless: test -d /tmp/dummy/



回答2:


{% if 1 == salt['cmd.retcode']('test -f /tmp/woo.test') %}
ack:
  file.touch:
    - name: /tmp/woo.test
{% endif %}



回答3:


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


来源:https://stackoverflow.com/questions/21857533/defining-states-depending-on-existance-of-a-file-directory

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!