saltconf14 - forrest alvarez, choice hotels - salt formulas and states
DESCRIPTION
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.TRANSCRIPT
![Page 1: SaltConf14 - Forrest Alvarez, Choice Hotels - Salt Formulas and States](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f8eb1b4c905435d8b4f6f/html5/thumbnails/1.jpg)
Salt Formulas and States Forrest Alvarez
![Page 2: SaltConf14 - Forrest Alvarez, Choice Hotels - Salt Formulas and States](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f8eb1b4c905435d8b4f6f/html5/thumbnails/2.jpg)
![Page 3: SaltConf14 - Forrest Alvarez, Choice Hotels - Salt Formulas and States](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f8eb1b4c905435d8b4f6f/html5/thumbnails/3.jpg)
Formulas are the evolution of Salt states, they are the future of
Salt.
![Page 4: SaltConf14 - Forrest Alvarez, Choice Hotels - Salt Formulas and States](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f8eb1b4c905435d8b4f6f/html5/thumbnails/4.jpg)
Salt States
Core of Salt !Range from basic to very complex !Used for provisioning and deployment
![Page 5: SaltConf14 - Forrest Alvarez, Choice Hotels - Salt Formulas and States](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f8eb1b4c905435d8b4f6f/html5/thumbnails/5.jpg)
States Refresher
Syntax is fairly simple !Modular focus !Ease of use
![Page 6: SaltConf14 - Forrest Alvarez, Choice Hotels - Salt Formulas and States](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f8eb1b4c905435d8b4f6f/html5/thumbnails/6.jpg)
Example Statenginx: pkg: - installed service: - running - enable: True - reload: True
![Page 7: SaltConf14 - Forrest Alvarez, Choice Hotels - Salt Formulas and States](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f8eb1b4c905435d8b4f6f/html5/thumbnails/7.jpg)
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
![Page 8: SaltConf14 - Forrest Alvarez, Choice Hotels - Salt Formulas and States](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f8eb1b4c905435d8b4f6f/html5/thumbnails/8.jpg)
Salt States Can Become Complexapache: pkg: - installed {% if grains[‘os_family’] == ‘RedHat’ %} - name: httpd {% elif grains[‘os_family’] == ‘Debian’ %} - name: apache2 {% endif %}
![Page 9: SaltConf14 - Forrest Alvarez, Choice Hotels - Salt Formulas and States](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f8eb1b4c905435d8b4f6f/html5/thumbnails/9.jpg)
Pillar ExploitationOS variables that shouldn’t be in pillar are !Pillar becomes bloated !Harder to understand for new users
![Page 10: SaltConf14 - Forrest Alvarez, Choice Hotels - Salt Formulas and States](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f8eb1b4c905435d8b4f6f/html5/thumbnails/10.jpg)
Combine pillar and state complexity
![Page 11: SaltConf14 - Forrest Alvarez, Choice Hotels - Salt Formulas and States](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f8eb1b4c905435d8b4f6f/html5/thumbnails/11.jpg)
Complexity leads to problemsDifferent operating systems !Multiple locations in a state !Several states require changes
![Page 12: SaltConf14 - Forrest Alvarez, Choice Hotels - Salt Formulas and States](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f8eb1b4c905435d8b4f6f/html5/thumbnails/12.jpg)
How Do We Address This?Ease of use !Simple to modify multiple locations !Can be applied to existing configurations
![Page 13: SaltConf14 - Forrest Alvarez, Choice Hotels - Salt Formulas and States](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f8eb1b4c905435d8b4f6f/html5/thumbnails/13.jpg)
Salt FormulasEvolution of the Salt states !Easy to write because you already are !Somewhat difficult to understand how they work
![Page 14: SaltConf14 - Forrest Alvarez, Choice Hotels - Salt Formulas and States](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f8eb1b4c905435d8b4f6f/html5/thumbnails/14.jpg)
Formula Directory Structure/srv/salt/apache/init.sls !/srv/salt/apache/conf.sls !/srv/salt/apache/map.jinja !
![Page 15: SaltConf14 - Forrest Alvarez, Choice Hotels - Salt Formulas and States](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f8eb1b4c905435d8b4f6f/html5/thumbnails/15.jpg)
The Core of Formulas, map.jinjaSets data based on OS grains !Merges with Pillar data !Centralizes variables !
![Page 16: SaltConf14 - Forrest Alvarez, Choice Hotels - Salt Formulas and States](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f8eb1b4c905435d8b4f6f/html5/thumbnails/16.jpg)
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’)) %}
![Page 17: SaltConf14 - Forrest Alvarez, Choice Hotels - Salt Formulas and States](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f8eb1b4c905435d8b4f6f/html5/thumbnails/17.jpg)
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
![Page 18: SaltConf14 - Forrest Alvarez, Choice Hotels - Salt Formulas and States](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f8eb1b4c905435d8b4f6f/html5/thumbnails/18.jpg)
Beforeapache:
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
![Page 19: SaltConf14 - Forrest Alvarez, Choice Hotels - Salt Formulas and States](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f8eb1b4c905435d8b4f6f/html5/thumbnails/19.jpg)
After{% from “apache/map.jinja” import apache with context %} !apache: pkg: - installed - name: {{ apache.server }} service: - running - name: {{ apache.service }} - enable: True
![Page 20: SaltConf14 - Forrest Alvarez, Choice Hotels - Salt Formulas and States](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f8eb1b4c905435d8b4f6f/html5/thumbnails/20.jpg)
A Few Seconds After That
![Page 21: SaltConf14 - Forrest Alvarez, Choice Hotels - Salt Formulas and States](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f8eb1b4c905435d8b4f6f/html5/thumbnails/21.jpg)
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
![Page 22: SaltConf14 - Forrest Alvarez, Choice Hotels - Salt Formulas and States](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f8eb1b4c905435d8b4f6f/html5/thumbnails/22.jpg)
PillarsSplitting pillar data !Identifying it with sets of states Will all be combined in the end
![Page 23: SaltConf14 - Forrest Alvarez, Choice Hotels - Salt Formulas and States](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f8eb1b4c905435d8b4f6f/html5/thumbnails/23.jpg)
Pillar Directory Structure/srv/pillar/top.sls !/srv/pillar/apache.sls !/srv/pillar/php.sls
![Page 24: SaltConf14 - Forrest Alvarez, Choice Hotels - Salt Formulas and States](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f8eb1b4c905435d8b4f6f/html5/thumbnails/24.jpg)
The Pillar Top.slsbase: ‘*’: - apache - php
![Page 25: SaltConf14 - Forrest Alvarez, Choice Hotels - Salt Formulas and States](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f8eb1b4c905435d8b4f6f/html5/thumbnails/25.jpg)
Extending Your Pillarapache: lookup: config: tmpl: salt://apache/files/redhat/httpd.conf
![Page 26: SaltConf14 - Forrest Alvarez, Choice Hotels - Salt Formulas and States](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f8eb1b4c905435d8b4f6f/html5/thumbnails/26.jpg)
Merging Maps With PillarsMultiple pillars further reduce complexity !Easy to include several pillars in your top pillar !Pillar variables can overwrite map
![Page 27: SaltConf14 - Forrest Alvarez, Choice Hotels - Salt Formulas and States](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f8eb1b4c905435d8b4f6f/html5/thumbnails/27.jpg)
Overwriting Maps
apache: lookup: config: tmpl: salt://apache/files/redhat/ server: my_custom_apache
![Page 28: SaltConf14 - Forrest Alvarez, Choice Hotels - Salt Formulas and States](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f8eb1b4c905435d8b4f6f/html5/thumbnails/28.jpg)
Clear Location of VariablesWhere did I miss that conf name change? !Did I make sure to update all my requires? !Hey, could you modify this value?
![Page 29: SaltConf14 - Forrest Alvarez, Choice Hotels - Salt Formulas and States](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f8eb1b4c905435d8b4f6f/html5/thumbnails/29.jpg)
The Simpler the BetterWorks great even if you only have a single OS !Easy to hand off !Fewer files to manage
![Page 30: SaltConf14 - Forrest Alvarez, Choice Hotels - Salt Formulas and States](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f8eb1b4c905435d8b4f6f/html5/thumbnails/30.jpg)
![Page 31: SaltConf14 - Forrest Alvarez, Choice Hotels - Salt Formulas and States](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f8eb1b4c905435d8b4f6f/html5/thumbnails/31.jpg)
Managing FormulasDirectory structure is the same as always !One map file per formula !Pillars are just included in your top pillar
![Page 32: SaltConf14 - Forrest Alvarez, Choice Hotels - Salt Formulas and States](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f8eb1b4c905435d8b4f6f/html5/thumbnails/32.jpg)
Creating a Formulamap.jinja !Example Pillar !Modular structure
![Page 33: SaltConf14 - Forrest Alvarez, Choice Hotels - Salt Formulas and States](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f8eb1b4c905435d8b4f6f/html5/thumbnails/33.jpg)
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
![Page 34: SaltConf14 - Forrest Alvarez, Choice Hotels - Salt Formulas and States](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f8eb1b4c905435d8b4f6f/html5/thumbnails/34.jpg)
Premade Salt FormulasOver 75 formulas publicly available !Not all formulas require the map !Fork it with GIT and modify as needed
![Page 35: SaltConf14 - Forrest Alvarez, Choice Hotels - Salt Formulas and States](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f8eb1b4c905435d8b4f6f/html5/thumbnails/35.jpg)
Contributing BackFork an existing repo !Ask for a repo to be made !Contact the members
![Page 36: SaltConf14 - Forrest Alvarez, Choice Hotels - Salt Formulas and States](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f8eb1b4c905435d8b4f6f/html5/thumbnails/36.jpg)
IRC: forrest !Twitter: failvarez !GitHub: gravyboat
Questions?
![Page 37: SaltConf14 - Forrest Alvarez, Choice Hotels - Salt Formulas and States](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f8eb1b4c905435d8b4f6f/html5/thumbnails/37.jpg)
SourcesXKCD: 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