Defining states depending on existance of a file/d

2019-02-14 06:20发布

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.

标签: salt-stack
3条回答
Root(大扎)
2楼-- · 2019-02-14 06:48

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
查看更多
3楼-- · 2019-02-14 07:06

You can use unless for this.

dummy:
  file.touch:
    - name: /tmp/dummy/tmp.txt
    - unless: test -d /tmp/dummy/
查看更多
做个烂人
4楼-- · 2019-02-14 07:09
{% if 1 == salt['cmd.retcode']('test -f /tmp/woo.test') %}
ack:
  file.touch:
    - name: /tmp/woo.test
{% endif %}
查看更多
登录 后发表回答