在我的国家档案的顶部,我有:
{% if grains['os'] == 'Ubuntu' %} {% set ubuntu = True %} {% set arch = False %} {% elif grains['os'] == 'Arch' %} {% set ubuntu = False %} {% set arch = True %} {% endif %}
稍后的,
{% if ubuntu %} cron: {% elif arch %} cronie: {% endif %} pkg.installed service.running: - enable: True
但是这不起作用; 我的条件是没有渲染(空string)。 即使一个小的重构可以完成工作,这对我来说也是一种气味。
有没有一个更习惯的方式来replace像Salt这样的小细节没有太多的模板样板?
它不工作可能是因为pkg.installed
必须是一个列表,即使没有参数:
pkg.installed: []
这应该工作:
{% if ubuntu %} cron: {% elif arch %} cronie: {% endif %} pkg.installed: [] service.running: - enable: True
或者,以更聪明的方式:
{% set cron = salt['grains.filter_by']({ 'Ubuntu': 'cron', 'Arch': 'cronie', }, grain='os') %} {{cron}}: pkg.installed: [] service.running: - enable: True
或者,服务名称可能与软件包名称不同:
{% set cron = salt['grains.filter_by']({ 'Ubuntu': { 'package': 'cron', 'service': 'crond', }, 'Arch': { 'package': 'cronie', 'service': 'cronie', }, }, grain='os') %} {{cron['package']}}: pkg.installed: [] service.running: - name: {{cron['service']}} - enable: True
grains.filter_by
logging在http://docs.saltstack.com/en/latest/ref/modules/all/salt.modules.grains.html#salt.modules.grains.filter_by
有关更详细的内容,请查看https://github.com/saltstack-formulas/apache-formula/blob/master/apache/map.jinja