Closed UtahDave closed 8 years ago
@whiteinge asked me to test if the grains.get
works in the sls file directly and it does.
I changed to this:
httpd_stuff:
pkg.installed:
- pkgs:
- httpd
- mod_ssl
file.managed:
- name: /etc/httpd/conf/httpd.conf
- source: salt://pkgrepo/apache/httpd.conf
- template: jinja
- user: root
- group: root
- mode: 644
- mystuff: {{ salt['grains.get']('ip4_interfaces:eth1').pop() }}
and mystuff was assigned the ip address as the value correctly
OK, for completeness, here's the workaround:
cat /srv/salt/pkgrepo/apache/init.sls
httpd_stuff:
pkg.installed:
- pkgs:
- httpd
- mod_ssl
file.managed:
- name: /etc/httpd/conf/httpd.conf
- source: salt://pkgrepo/apache/httpd.conf
- template: jinja
- user: root
- group: root
- mode: 644
- context:
eth1_ip: {{ salt['grains.get']('ip4_interfaces:eth1').pop() }}
cat /srv/salt/pkgrepo/apache/httpd.conf
<-- snip -->
Listen {{ eth1_ip }}:80
<-- snip -->
When I apply the state with the above changes it works correctly. I'm not sure why the grains.get
works in the sls file and not within the jinja context in the httpd.conf
This is not a bug in Salt. It's an error in your template. It's easy to do, though, since this is a subtle side-effect in Jinja2.
What you're seeing here is a single-element list being returned from this grain. When you call pop()
, it modifies the list in place before evaluating it, so it thinks the list is empty. What you want to do here instead is either access the element directly or iterate over the list.
Whoops, I overlooked that. Good catch, Mike. Modifying the loaded grains object is definitely bad juju. This is better done using the | first()
filter in Jinja, or by looping over each value.
That said, that error message is very perplexing. The result of that Jinja operation should be a string (the return value of pop()
). The pop()
operation should only be executed one time and at that time the list is decidedly not empty. It looks like we're doing two passes over the templated file 😦 :
{{ salt.cmd.run('touch /tmp/gotfoo && echo foo >> /tmp/gotfoo') }}
Since we're looping twice and it does modify the list it is indeed empty on the second pass.
Description of Issue/Question
When I run
salt '<minion id>' grains.get 'ip4_interfaces:eth1'
on the salt master I get a value back. When I make the same call within a jinja managed file I get an empty list.Setup
cat /srv/salt/pkgrepo/apache/init.sls
cat /srv/salt/pkgrepo/apache/httpd.conf
Steps to Reproduce Issue
Versions Report