问题
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