Jinja variable is undefined

岁酱吖の 提交于 2019-12-24 12:05:53

问题


I'm trying to build the hosts file on my servers using salt. In some of the servers, the eth0 network interface has the inet set and in some others is the bond0 interface.

In the init.sls i have:

 /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']() }}

And in my hosts file that is set above in the "- source", i have:

{{ ip }}  {{ hostname }}

Then, when i run a state.highstate from the salt master, i get an error saying:

SaltRenderError: Jinja variable 'ip' is undefined; line 97

It seems like that the salt function that retrieves the network interface, does not work when it is inside the jinja for loop(or i'm doing something wrong). I'm saying that because last line where it returns the hostname, works just fine.

What am i doing wrong here ? I'm suspecting that the if condition is not met and thus the "ip" variable never gets assigned a value.

Thank you,

来源:https://stackoverflow.com/questions/31748852/jinja-variable-is-undefined

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