1、这种情况是可以的,只有一个 deploy.sls 时,appname 可以传递给 /data/apps/{{ appname }}
// deploy.sls {% set appname = 'foo-service' %} app-config-dir: file.directory: - name: /data/apps/{{ appname }} # --> 得到值为 foo-service - mode: 755 - user: root - group: root
2、下面这种情况就不行,appname 不能传递给 include 后的 config.sls 文件,报错提示 Jinja variable 'appname' is undefined
// deploy.sls {% set appname = 'foo-service' %} include: - config // config.sls app-config-dir: file.directory: - name: /data/apps/{{ appname }} - mode: 755 - user: root - group: root
如果要实现 appname 能传递给 include 后的 config.sls 文件,要怎么做?
3、用 macro 实现
// config.sls {% macro get_appname(appname) %} app-config-dir: file.directory: - name: /data/apps/{{ appname }} - mode: 755 - user: root - group: root {% endmacro %}
// deploy.sls {% from 'config.sls' import get_appname with context %} {{ get_appname('foo-service') }}
4、使用方式
# salt '*' state.apply deploy
本文出自https://www.jianshu.com/p/52f3bcc82f9f