Jinja变量未定义

时间:2015-07-31 14:38:05

标签: jinja2 salt-stack

我尝试使用salt在我的服务器上构建hosts文件。在某些服务器中,eth0网络接口具有inet set,而在其他服务器中则是bond0接口。

在init.sls中我有:

 /etc/hosts:
  file.managed:
    - source: salt://configs/etc/hosts/hostsfile
    - user: root
    - group: root
    - mode: 644
    - template: jinja
    - context:
      {% for host, interface in salt['mine.get']('*', 'network.interfaces').items() %}
        {% if interface['bond0'].has_key('inet') %}
      ip: {{ salt['network.interfaces']()['bond0']['inet'][0]['address'] }}
        {% else %}
      ip: {{ salt['network.interfaces']()['eth0']['inet'][0]['address'] }}
        {% endif %}
      {% endfor %}
      hostname: {{ salt['network.get_hostname']() }}

在我上面的" - source"中设置的主机文件中,我有:

{{ ip }}  {{ hostname }}

然后,当我从salt master运行state.highstate时,我收到错误说:

  

SaltRenderError:Jinja变量' ip'未定义;第97行

似乎检索网络接口的salt函数在jinja for循环内部(或者我做错了)时不起作用。 我说这是因为它返回主机名的最后一行,效果很好。

我在这里做错了什么?我怀疑if条件没有得到满足,因此" ip"变量永远不会被赋值。

谢谢,

0 个答案:

没有答案
相关问题