The Magic of Pillar Targeting In SaltStack

Overview

In the course of using salt for all sorts of use cases I happened upon a particular pattern of using the states top file that, in my opinionated view, is the most elegant way.

Basic States and Top File Setup

Let’s say we have three minions called minion1, minion2, and minion3, and we have the breakfast.sls and dinner.sls states to deploy on them. Where our states are:

{{ sls }} - write out the breakfast menu: 
file.managed:
- name: /tmp/breakfast.txt
- contents: |
eggs: scrambled
meats:
- sausage
- bacon
sides:
spam: gross
beans: baked
{{ sls }} - write out the dinner menu: 
file.managed:
- name: /tmp/dinner.txt
- contents: |
eggs: poached
seafood: lobster
other:
- truffle pate
- mornay sauce
sides:
spam: delicious
eggs: over easy
'minion1': 
- breakfast
'minion2':
- dinner
'minion3':
- breakfast
- dinner

Moving Data to Pillar

The above setup works just fine. But, it’s very rigid. Using the model above, If we were to add another minion that required a different value for dinner eggs, we would have to write a new state and add an entry to the top file. There’s no code reuse here.

breakfast_items: 
menu:
egg: scrambled
meats:
- sausage
- bacon
sides:
spam: gross
beans: baked
dinner_items:
menu:
egg: poached
seafood: lobster
other:
- truffle pate
- mornay sauce
sides:
spam: delicious
egg: over easy
'minion1': 
- breakfast-menu-items
'minion2':
- dinner-menu-items
'minion3':
- breakfast-menu-items
- dinner-menu-items
{{ sls }} - write out the breakfast menu: 
file.managed:
- name: /tmp/breakfast.txt
- contents_pillar: breakfast:menu
{{ sls }} - write out the dinner menu: 
file.managed:
- name: /tmp/dinner.txt
- contents_pillar: dinner:menu

Magic

As can be seen above, the states top file and the pillar top file are very similar. Lets simplify the states top file by using pillar targeting.

breakfast: 
install: true
menu:
egg: scrambled
meats:
- sausage
- bacon
sides:
spam: gross
beans: baked
dinner:
install: true
menu:
egg: poached
seafood: lobster
other:
- truffle pate
- mornay sauce
sides:
spam: delicious
egg: over easy
'breakfast:install:true' 
- match: pillar
- breakfast
'dinner:install:true'
- match: pillar
- dinner

Conclusion

While states and pillar files will proliferate in any environment. In my opinionated view, this is the easiest way of releaving overhead thoughts about what minions need to get which state in the top file.

Climber, surfer, yogi, dad who does some IT on the side to get by.

Climber, surfer, yogi, dad who does some IT on the side to get by.