Skip to content

Instantly share code, notes, and snippets.

@sirkonst
Created February 24, 2012 13:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sirkonst/1900869 to your computer and use it in GitHub Desktop.
Save sirkonst/1900869 to your computer and use it in GitHub Desktop.
State of states module for salt (saltstack)
"""
State module
============
The state module for logical union of several states.
Available Functions
-------------------
The module only has a single function, the ``present`` function
present
Combining multiple states in one
example:
.. code-block:: yaml
# some service configs in one state
some_config:
state:
- present
- tag: v1.0 # you can mark state by tag, but not require
- watch: # or require declaration
- file: /etc/some_service/conf_1.conf
- file: /etc/some_service/conf_2.conf
- file: /etc/some_service/conf_3.conf
some_service:
service:
- running
# if will changed some of some_config watchers OR tag
# service will be restarted
- watch:
- state: some_config
another_service:
service:
- running
# Service check running if all watch (or require) state in
# some_config and state file: /etc/another_service/conf.conf
# is true
- require:
- state: some_config
- file: /etc/another_service/conf.conf
# union two state service as one state
services_running:
state:
- present
- require:
- service: some_service
- service: another_service
do_some_think:
cmd:
- wait
- watch:
- state: services_running
"""
def present(name, tag=None):
"""
State
:param name: state name
:param tag: tag
"""
if tag:
data = __salt__['data.load']()
key = 'state/%s/tag' % name
val = data.get(key, None)
if not val:
__salt__['data.update'](key, tag)
return {'name': name,
'result': True,
'changes': {'new': 'State tagged as %s' % tag},
'comment': 'State present'}
if val != tag:
__salt__['data.update'](key, tag)
return {'name': name,
'result': True,
'changes': {'diff': 'State updated tag %s -> %s'
% (val, tag)},
'comment': 'State present'}
return {'name': name,
'result': True,
'changes': {},
'comment': 'State present'}
# ----------------------------------------------------------------------------
def watcher(name, tag=None):
res = present(name, tag)
res['changes'].update({'state': 'Watch requires changed'})
return res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment