title: Salt grains in Jinja template
body: <p>I am trying to do something simple in SaltStack: manage the <code>/etc/apt/sources.list</code> file using a formula. Here's the relevant section in my <code>formula/init.sls</code> file:</p>
  
  <pre><code>/etc/apt/sources.list:
    - file.managed:
      - template: jinja
      - source: salt://pkg/files/etc/apt/sources.list
      - user: root
      - group: root
      - mode: 0644
  
  refresh-apt:
    cmd.wait:
      - name: '/usr/bin/apt-get -y update'
      - watch:
        - file: /etc/apt/sources.list
  </code></pre>
  
  <p>And here's the file <code>pkg/files/etc/apt/sources.list</code>:</p>
  
  <pre><code>{{ pillar['headers']['salt']['file'] }}
  
  deb http://{{ grains['aws_package_zone'] }}.ec2.archive.ubuntu.com/ubuntu/ {{ grains['oscodename'] }} main universe multiverse
  deb-src http://{{ grains['aws_package_zone'] }}.ec2.archive.ubuntu.com/ubuntu/ {{ grains['oscodename'] }} main universe multiverse
  
  deb http://{{ grains['aws_package_zone'] }}.ec2.archive.ubuntu.com/ubuntu/ {{ grains['oscodename'] }}-updates main universe multiverse
  deb-src http://{{ grains['aws_package_zone'] }}.ec2.archive.ubuntu.com/ubuntu/ {{ grains['oscodename'] }}-updates main universe multiverse
  
  deb http://{{ grains['aws_package_zone'] }}.ec2.archive.ubuntu.com/ubuntu/ {{ grains['oscodename'] }}-backports main universe multiverse
  deb-src http://{{ grains['aws_package_zone'] }}.ec2.archive.ubuntu.com/ubuntu/ {{ grains['oscodename'] }}-backports main universe multiverse
  
  deb http://security.ubuntu.com/ubuntu {{ grains['oscodename'] }}-security main universe multiverse
  deb-src http://security.ubuntu.com/ubuntu {{ grains['oscodename'] }}-security main universe multiverse
  </code></pre>
  
  <p>The <code>grains['aws_package_zone']</code> returns the string <code>us-west-2</code> in this case (or whatever code for the zone you're in) and <code>grains['oscodename']</code> will return the name of the Ubuntu codename for that release: trusty, xenial,  etc.</p>
  
  <p>Clearly Salt doesn't like this because when I try to run <code>state.highstate</code> I get the following error:</p>
  
  <pre><code>machine.fqdn:
      Data failed to compile:
  ----------
      ID /etc/apt/sources.list in SLS pkg is not a dictionary
  </code></pre>
  
  <p>Not sure what I'm doing wrong...any suggestions? Sorry if this is basic.</p>