Creating SaltStack State data with Pyobjects
-
Upload
evan-borgstrom -
Category
Technology
-
view
653 -
download
3
Transcript of Creating SaltStack State data with Pyobjects
![Page 1: Creating SaltStack State data with Pyobjects](https://reader033.fdocuments.in/reader033/viewer/2022050818/55a524d01a28abe50e8b45e4/html5/thumbnails/1.jpg)
Creating State data with Pyobjects
A high level Pythonic API
Evan Borgstrom
#SaltConf15
Site Reliability, LinkedIn
![Page 2: Creating SaltStack State data with Pyobjects](https://reader033.fdocuments.in/reader033/viewer/2022050818/55a524d01a28abe50e8b45e4/html5/thumbnails/2.jpg)
Pyobjects?
• It’s an alternative renderer for your state data
• High level “Pythonic” API
• Part of SaltStack since 2014.7.0
![Page 3: Creating SaltStack State data with Pyobjects](https://reader033.fdocuments.in/reader033/viewer/2022050818/55a524d01a28abe50e8b45e4/html5/thumbnails/3.jpg)
Context
• Long time user & contributor
• Ended up with a very large state tree and
became frustrated with YAML + Jinja
• Began initial brainstorming late 2013
• Finally inspired to start on Pyobjects at SaltConf
2014
![Page 4: Creating SaltStack State data with Pyobjects](https://reader033.fdocuments.in/reader033/viewer/2022050818/55a524d01a28abe50e8b45e4/html5/thumbnails/4.jpg)
Motivation
• 6500+ lines over
hundreds of states
(plus pillars)
• YAML no longer felt
like markup due to all
of the Jinja syntax
![Page 5: Creating SaltStack State data with Pyobjects](https://reader033.fdocuments.in/reader033/viewer/2022050818/55a524d01a28abe50e8b45e4/html5/thumbnails/5.jpg)
Not markup anymore
![Page 6: Creating SaltStack State data with Pyobjects](https://reader033.fdocuments.in/reader033/viewer/2022050818/55a524d01a28abe50e8b45e4/html5/thumbnails/6.jpg)
![Page 7: Creating SaltStack State data with Pyobjects](https://reader033.fdocuments.in/reader033/viewer/2022050818/55a524d01a28abe50e8b45e4/html5/thumbnails/7.jpg)
+
#!py
![Page 8: Creating SaltStack State data with Pyobjects](https://reader033.fdocuments.in/reader033/viewer/2022050818/55a524d01a28abe50e8b45e4/html5/thumbnails/8.jpg)
![Page 9: Creating SaltStack State data with Pyobjects](https://reader033.fdocuments.in/reader033/viewer/2022050818/55a524d01a28abe50e8b45e4/html5/thumbnails/9.jpg)
![Page 10: Creating SaltStack State data with Pyobjects](https://reader033.fdocuments.in/reader033/viewer/2022050818/55a524d01a28abe50e8b45e4/html5/thumbnails/10.jpg)
![Page 11: Creating SaltStack State data with Pyobjects](https://reader033.fdocuments.in/reader033/viewer/2022050818/55a524d01a28abe50e8b45e4/html5/thumbnails/11.jpg)
![Page 12: Creating SaltStack State data with Pyobjects](https://reader033.fdocuments.in/reader033/viewer/2022050818/55a524d01a28abe50e8b45e4/html5/thumbnails/12.jpg)
Basics
![Page 13: Creating SaltStack State data with Pyobjects](https://reader033.fdocuments.in/reader033/viewer/2022050818/55a524d01a28abe50e8b45e4/html5/thumbnails/13.jpg)
Batteries available
• Fully fledged Python environment
• Import whatever you need to compile your data
• Here be dragons:
– Blocking operations
– Monkey patching (gevent, etc)
![Page 14: Creating SaltStack State data with Pyobjects](https://reader033.fdocuments.in/reader033/viewer/2022050818/55a524d01a28abe50e8b45e4/html5/thumbnails/14.jpg)
No boilerplate
• Built-in run() function takes care of collecting and
returning data.
• No need to import import anything from
Pyobjects, everything is included
• Should be as natural as writing YAML
![Page 15: Creating SaltStack State data with Pyobjects](https://reader033.fdocuments.in/reader033/viewer/2022050818/55a524d01a28abe50e8b45e4/html5/thumbnails/15.jpg)
State objects
• One per State available on the minion
• CapWords version of the name
– file -> File
– postgres_user -> PostgresUser
– ssh_known_hosts -> SshKnownHosts
• Exposes each state function as a function
– File.managed, Pkg.installed, etc…
![Page 16: Creating SaltStack State data with Pyobjects](https://reader033.fdocuments.in/reader033/viewer/2022050818/55a524d01a28abe50e8b45e4/html5/thumbnails/16.jpg)
Using State objects
![Page 17: Creating SaltStack State data with Pyobjects](https://reader033.fdocuments.in/reader033/viewer/2022050818/55a524d01a28abe50e8b45e4/html5/thumbnails/17.jpg)
Including
• include() function takes a variable number of
arguments
• One per state to include
• Can include any state, not just pyobjects
![Page 18: Creating SaltStack State data with Pyobjects](https://reader033.fdocuments.in/reader033/viewer/2022050818/55a524d01a28abe50e8b45e4/html5/thumbnails/18.jpg)
Extending
• The extend() function is used in place of the ID
being passed to the state.
• Works exactly the same as the extend
declaration in YAML
![Page 19: Creating SaltStack State data with Pyobjects](https://reader033.fdocuments.in/reader033/viewer/2022050818/55a524d01a28abe50e8b45e4/html5/thumbnails/19.jpg)
Importing
• State trees will eventually become complex
enough that you’ll want to create reusability
• Pyobjects provides an augmentation to the import
statement to allow salt:// URIs
![Page 20: Creating SaltStack State data with Pyobjects](https://reader033.fdocuments.in/reader033/viewer/2022050818/55a524d01a28abe50e8b45e4/html5/thumbnails/20.jpg)
Context Managers / Requisites
![Page 21: Creating SaltStack State data with Pyobjects](https://reader033.fdocuments.in/reader033/viewer/2022050818/55a524d01a28abe50e8b45e4/html5/thumbnails/21.jpg)
Convenience Objects
![Page 22: Creating SaltStack State data with Pyobjects](https://reader033.fdocuments.in/reader033/viewer/2022050818/55a524d01a28abe50e8b45e4/html5/thumbnails/22.jpg)
Maps
![Page 23: Creating SaltStack State data with Pyobjects](https://reader033.fdocuments.in/reader033/viewer/2022050818/55a524d01a28abe50e8b45e4/html5/thumbnails/23.jpg)
Maps 101
![Page 24: Creating SaltStack State data with Pyobjects](https://reader033.fdocuments.in/reader033/viewer/2022050818/55a524d01a28abe50e8b45e4/html5/thumbnails/24.jpg)
Targeting using grains
• Preferred way of building configuration
• Allows for dynamically changing values without
conditionals
• Matches against the inner class name
• The grain defaults to os_family
– Add __grain__ to the class to change it
![Page 25: Creating SaltStack State data with Pyobjects](https://reader033.fdocuments.in/reader033/viewer/2022050818/55a524d01a28abe50e8b45e4/html5/thumbnails/25.jpg)
Merging with Pillar data
• Often times you’ll want to change configuration
under some special conditions
• Pillars provide the perfect platform for this
• Specify the pillar name in the merge attribute in
the main class
![Page 26: Creating SaltStack State data with Pyobjects](https://reader033.fdocuments.in/reader033/viewer/2022050818/55a524d01a28abe50e8b45e4/html5/thumbnails/26.jpg)
Best Practices
![Page 27: Creating SaltStack State data with Pyobjects](https://reader033.fdocuments.in/reader033/viewer/2022050818/55a524d01a28abe50e8b45e4/html5/thumbnails/27.jpg)
Pyobjects compliments YAML
• Pyobjects only makes sense when you need to
introduce logic into your state generation
• YAML should still be used when do you need
programmatic functions
![Page 28: Creating SaltStack State data with Pyobjects](https://reader033.fdocuments.in/reader033/viewer/2022050818/55a524d01a28abe50e8b45e4/html5/thumbnails/28.jpg)
Move logic out of templates
![Page 29: Creating SaltStack State data with Pyobjects](https://reader033.fdocuments.in/reader033/viewer/2022050818/55a524d01a28abe50e8b45e4/html5/thumbnails/29.jpg)
Pillars + Maps
• Maps provide default values for all of your
baseline settings
• Pillars allow customization using Salt’s targeting
system
• Keeps all of the configuration with the code
![Page 30: Creating SaltStack State data with Pyobjects](https://reader033.fdocuments.in/reader033/viewer/2022050818/55a524d01a28abe50e8b45e4/html5/thumbnails/30.jpg)
Future improvements
![Page 31: Creating SaltStack State data with Pyobjects](https://reader033.fdocuments.in/reader033/viewer/2022050818/55a524d01a28abe50e8b45e4/html5/thumbnails/31.jpg)
Lazy loading of modules
• The loader is lazy (pull #20274)
• Pyobjects triggers loading of all state modules
• Execution time on busy minions can be
decreased
![Page 32: Creating SaltStack State data with Pyobjects](https://reader033.fdocuments.in/reader033/viewer/2022050818/55a524d01a28abe50e8b45e4/html5/thumbnails/32.jpg)
Reactor files
• Reactor files are prime candidates to benefit from
the separation of logic & presentation
• Need to support the Local, Runner & Wheel
prefixes in a Pythonic way
![Page 33: Creating SaltStack State data with Pyobjects](https://reader033.fdocuments.in/reader033/viewer/2022050818/55a524d01a28abe50e8b45e4/html5/thumbnails/33.jpg)
Map support for complex Grains
• Currently Grains are limited to matching against
the syntax supported by Python class names
• Will be adding support through another “dunder”
attribute that allows for complex grain matching
– All numbers
– Patterns
![Page 34: Creating SaltStack State data with Pyobjects](https://reader033.fdocuments.in/reader033/viewer/2022050818/55a524d01a28abe50e8b45e4/html5/thumbnails/34.jpg)
FIN.
Please provide session feedback in the SaltConf15 mobile app
@borgstr / #SaltConf15