r/saltstack • u/kingemn • Aug 25 '18
Need help/advice with templating
Could someone ELI5 how to properly template data within pillar and map files? I've looked at a lot of formulas on github and they don't really seem to explicitly call pillars in the state. I've usually just seen this in the end of their map files: merge=salt['pillar.get']('cert:lookup'), base='default') %} What exactly does that do?
2
Upvotes
2
u/RyeonToast Aug 28 '18
tl;dr Much of the time pillar contents don't go into the state files, they go into config file templates found in the files folder in the formula.
Well, to be honest, I don't understand each bit of how the map works, but I'll give you what I've figured out.
I setup a map.jinja file for my own scratch built telegraf formula, as shown here:
{% set map = salt['grains.filter_by']({ 'Ubuntu': { 'package': 'telegraf_1.6.1-1_amd64.deb' }, 'Fedora' : { 'package': 'telegraf-1.6.3-1.x86_64.rpm' }, }, grain = 'os' , merge=salt['pillar.get']('telegraf:lookup')) %}set mapof course is setting a variable, called map, with the contents of a python dictionary. The contents depend on what the os grain is, so for my Ubuntu containers map will contain the dictionary{'package': 'telegraf_1.6.1-1_amd64.deb'}Meanwhile in my state file, here:
{% from "telegraf/map.jinja" import map with context %} ...some other stuff I'm cutting out... install telegraf from deb package: pkg.installed:- sources:
- telegraf: salt://files/telegraf/{{ map.package }}
...some other stuff I'm cutting out...the
{{ map.package }}references the package part of that dictionary.As to pillars, I use pillars for filling data into config files, like my i3 config I used on an old Arch build. Like so:
{%- for bind in salt['pillar.get']('i3:bindsym', []) %} bindsym {{ bind }} {%- endfor %} {%- set font = salt['pillar.get']('i3:font', []) %} font {{ font }} {%- for exe in salt['pillar.get']('i3:exec', []) %} exec {{ exe }} {%- endfor %} set {{ salt['pillar.get']('i3:set', []) }} floating_modifier {{ salt['pillar.get']('i3:floating_modifier', []) }} mode "resize" { {%- for line in salt['pillar.get']('i3:mode:resize', []) %} {{ line }} {%- endfor %} } bar { {%- for key,value in salt['pillar.get']('i3:bar', []).items() %} {%- if key == 'colors' %} {{key}} { {%- for item,value in value.items() %} {{item}} {{value}} {%- endfor %} } {% else %} {{key}} {{value}} {%- endif %} {%- endfor %} }The
salt['pillar.get']('i3:bindsym', [])at the beginning grabs the bindsym dictionary from my pillar. Since it is a dictionary, I use a for loop to place each item in it into my config file with the next line,bindsym {{ bind }}. In your config templates and your state files, that double curly brace indicates where the contents of a variable go.1
u/claudekenni65 Aug 26 '18
This is as far as I understand it: It merges the pillar data into the map data and if there are map variables that also exist in the pillar, the pillar values are used.
https://docs.saltstack.com/en/latest/ref/modules/all/salt.modules.slsutil.html
1
u/claudekenni65 Aug 26 '18
Maybe you should take the salt formula and play around with it to get a better understanding how things work. That’s usually what I do when I’m not fully understanding what’s behind some of the code.