Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Salt Formulas and States
Forrest Alvarez
Formulas are the evolution of
Salt states, they are the future of
Salt.
Salt States
Core of Salt
!
Range from basic to very complex
!
Used for provisioning and deployment
States Refresher
Syntax is fairly simple
!
Modular focus
!
Ease of use
Example State
nginx:
pkg:
- installed
service:
- running
- enable: True
- reload: True
State Directory Structure
/srv/salt/nginx/init.sls - Installs and starts
service
!
/srv/salt/mysql/client.sls - installs c...
Salt States Can Become Complex
apache:
pkg:
- installed
{% if grains[‘os_family’] == ‘RedHat’ %}
- name: httpd
{% elif gra...
Pillar Exploitation
OS variables that shouldn’t be in pillar are
!
Pillar becomes bloated
!
Harder to understand for new u...
Combine pillar and state complexity
Complexity leads to problems
Different operating systems
!
Multiple locations in a state
!
Several states require changes
How Do We Address This?
Ease of use
!
Simple to modify multiple locations
!
Can be applied to existing configurations
Salt Formulas
Evolution of the Salt states
!
Easy to write because you already are
!
Somewhat difficult to understand how ...
Formula Directory Structure
/srv/salt/apache/init.sls
!
/srv/salt/apache/conf.sls
!
/srv/salt/apache/map.jinja
!
The Core of Formulas, map.jinja
Sets data based on OS grains
!
Merges with Pillar data
!
Centralizes variables
!
Creating The Map
{% set apache = salt[‘grains.filter_by’]({
‘Debian’: {
‘server’: ‘apache2’,
‘service’: ‘apache2’,
‘conf’:...
Using Maps in States
{% from “apache/map.jinja” import apache with context %}
!
apache:
pkg:
- installed
- name: {{ apache...
Before
apache:
pkg:
- installed
{% if grains[‘os_family’] == ‘RedHat’ %}
- name: httpd
{% elif grains[‘os_family’] == ‘Deb...
After
{% from “apache/map.jinja” import apache with context %}
!
apache:
pkg:
- installed
- name: {{ apache.server }}
serv...
A Few Seconds After That
More Maps in States
{% from “apache/map.jinja” import apache with context %}
!
include:
- apache
!
apache_conf:
file:
- ma...
Pillars
Splitting pillar data
!
Identifying it with sets of states


Will all be combined in the end
Pillar Directory Structure
/srv/pillar/top.sls
!
/srv/pillar/apache.sls
!
/srv/pillar/php.sls
The Pillar Top.sls
base:
‘*’:
- apache
- php
Extending Your Pillar
apache:
lookup:
config:
tmpl: salt://apache/files/redhat/httpd.conf
Merging Maps With Pillars
Multiple pillars further reduce complexity
!
Easy to include several pillars in your top pillar
...
Overwriting Maps
apache:
lookup:
config:
tmpl: salt://apache/files/redhat/
server: my_custom_apache
Clear Location of Variables
Where did I miss that conf name change?
!
Did I make sure to update all my requires?
!
Hey, co...
The Simpler the Better
Works great even if you only have a single OS
!
Easy to hand off
!
Fewer files to manage
Managing Formulas
Directory structure is the same as always
!
One map file per formula
!
Pillars are just included in your...
Creating a Formula
map.jinja
!
Example Pillar
!
Modular structure
Writing Formulas Effectively
Think about what is OS based
!
Consider what might need to be expanded
!
Look at how someone ...
Premade Salt Formulas
Over 75 formulas publicly available
!
Not all formulas require the map
!
Fork it with GIT and modify...
Contributing Back
Fork an existing repo
!
Ask for a repo to be made
!
Contact the members
IRC: forrest
!
Twitter: failvarez
!
GitHub: gravyboat
Questions?
Sources
XKCD: Kayak comic - http://xkcd.com/209/
Easy button - http://dealer-communications.com/wp-content/uploads/2014/01...
SaltConf14 - Forrest Alvarez, Choice Hotels - Salt Formulas and States
SaltConf14 - Forrest Alvarez, Choice Hotels - Salt Formulas and States
Upcoming SlideShare
Loading in …5
×

SaltConf14 - Forrest Alvarez, Choice Hotels - Salt Formulas and States

1,888 views

Published on

This session will expand your knowledge of cutting-edge techniques for creating Salt states and formulas. Users will obtain a thorough understanding of how states interact with pillars, as well as map.jinja files. We'll discuss how to make formulas OS agnostic and show how the usage of external pillars combined with a map file can result in formulas that are easy to explain, easy to learn, and easy to update.

Published in: Technology
  • Be the first to comment

SaltConf14 - Forrest Alvarez, Choice Hotels - Salt Formulas and States

  1. 1. Salt Formulas and States Forrest Alvarez
  2. 2. Formulas are the evolution of Salt states, they are the future of Salt.
  3. 3. Salt States Core of Salt ! Range from basic to very complex ! Used for provisioning and deployment
  4. 4. States Refresher Syntax is fairly simple ! Modular focus ! Ease of use
  5. 5. Example State nginx: pkg: - installed service: - running - enable: True - reload: True
  6. 6. State Directory Structure /srv/salt/nginx/init.sls - Installs and starts service ! /srv/salt/mysql/client.sls - installs client ! /srv/salt/mysql/server.sls - installs the server and starts the service
  7. 7. Salt States Can Become Complex apache: pkg: - installed {% if grains[‘os_family’] == ‘RedHat’ %} - name: httpd {% elif grains[‘os_family’] == ‘Debian’ %} - name: apache2 {% endif %}
  8. 8. Pillar Exploitation OS variables that shouldn’t be in pillar are ! Pillar becomes bloated ! Harder to understand for new users
  9. 9. Combine pillar and state complexity
  10. 10. Complexity leads to problems Different operating systems ! Multiple locations in a state ! Several states require changes
  11. 11. How Do We Address This? Ease of use ! Simple to modify multiple locations ! Can be applied to existing configurations
  12. 12. Salt Formulas Evolution of the Salt states ! Easy to write because you already are ! Somewhat difficult to understand how they work
  13. 13. Formula Directory Structure /srv/salt/apache/init.sls ! /srv/salt/apache/conf.sls ! /srv/salt/apache/map.jinja !
  14. 14. The Core of Formulas, map.jinja Sets data based on OS grains ! Merges with Pillar data ! Centralizes variables !
  15. 15. Creating The Map {% set apache = salt[‘grains.filter_by’]({ ‘Debian’: { ‘server’: ‘apache2’, ‘service’: ‘apache2’, ‘conf’: ‘/etc/apache2/apache.conf’, }, ‘RedHat’: { ‘server’: ‘httpd’, ‘service’: ‘httpd’, ‘conf’: ‘/etc/httpd/httpd.conf’, }, }, merge=salt[‘pillar.get’](‘apache:lookup’)) %}
  16. 16. Using Maps in States {% from “apache/map.jinja” import apache with context %} ! apache: pkg: - installed - name: {{ apache.server }} service: - running - name: {{ apache.service }} - enable: True
  17. 17. Before apache: pkg: - installed {% if grains[‘os_family’] == ‘RedHat’ %} - name: httpd {% elif grains[‘os_family’] == ‘Debian’ %} - name: apache2 {% endif %} service: - running {% if grains[‘os_family’] == ‘RedHat’ %} - name: httpd {% elif grains[‘os_family’] == ‘Debian’ %} - name: apache2 {% endif %} - enable: True
  18. 18. After {% from “apache/map.jinja” import apache with context %} ! apache: pkg: - installed - name: {{ apache.server }} service: - running - name: {{ apache.service }} - enable: True
  19. 19. A Few Seconds After That
  20. 20. More Maps in States {% from “apache/map.jinja” import apache with context %} ! include: - apache ! apache_conf: file: - managed - name: {{ apache.conf }} - source: {{ salt[‘pillar.get’](‘apache:lookup:config:tmpl’) }} # Notice this variable doesn’t live in our map - template: jinja - user: root - group: root - mode: ‘0644’ - watch_in: - service: apache
  21. 21. Pillars Splitting pillar data ! Identifying it with sets of states 
 Will all be combined in the end
  22. 22. Pillar Directory Structure /srv/pillar/top.sls ! /srv/pillar/apache.sls ! /srv/pillar/php.sls
  23. 23. The Pillar Top.sls base: ‘*’: - apache - php
  24. 24. Extending Your Pillar apache: lookup: config: tmpl: salt://apache/files/redhat/httpd.conf
  25. 25. Merging Maps With Pillars Multiple pillars further reduce complexity ! Easy to include several pillars in your top pillar ! Pillar variables can overwrite map
  26. 26. Overwriting Maps apache: lookup: config: tmpl: salt://apache/files/redhat/ server: my_custom_apache
  27. 27. Clear Location of Variables Where did I miss that conf name change? ! Did I make sure to update all my requires? ! Hey, could you modify this value?
  28. 28. The Simpler the Better Works great even if you only have a single OS ! Easy to hand off ! Fewer files to manage
  29. 29. Managing Formulas Directory structure is the same as always ! One map file per formula ! Pillars are just included in your top pillar
  30. 30. Creating a Formula map.jinja ! Example Pillar ! Modular structure
  31. 31. Writing Formulas Effectively Think about what is OS based ! Consider what might need to be expanded ! Look at how someone else would see your formula
  32. 32. Premade Salt Formulas Over 75 formulas publicly available ! Not all formulas require the map ! Fork it with GIT and modify as needed
  33. 33. Contributing Back Fork an existing repo ! Ask for a repo to be made ! Contact the members
  34. 34. IRC: forrest ! Twitter: failvarez ! GitHub: gravyboat Questions?
  35. 35. Sources XKCD: Kayak comic - http://xkcd.com/209/ Easy button - http://dealer-communications.com/wp-content/uploads/2014/01/easy_button.png Bad cabling - http://farm3.staticflickr.com/2353/2219131561_31feee1745.jpg Earl of Lemongrab - http://fc03.deviantart.net/fs70/i/2013/108/e/3/lemongrab_by_twillis-d6266g9.png Dat Map - http://cdn.memegenerator.net/instances/500x/43904408.jpg Bruce Lee - http://www.elliottcaras.com/wp-content/uploads/2013/11/Bruce-Lee-Simplicity-is-the-key-2-brilliance.jpg Structure of Salt - http://upload.wikimedia.org/wikipedia/commons/2/29/NaCl.png Github OctoCat - http://www.iconsdb.com/icons/download/black/github-10-512.png

×