Saltstack custom states

From wikinotes
Revision as of 23:21, 8 February 2021 by Will (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

If you find yourself doing very complex things with jinja, you might want to consider writing your own salt-state instead for readability. See the following links:

Documentation

official docs https://docs.saltstack.com/en/latest/ref/states/writing.html
gh issue that helps explain https://github.com/saltstack/salt/issues/31400


salt-call saltutil.sync_states  # refresh custom states

Using Salt Information

__grains__['os_family']             # access grain from minion
__states__['cmd.script'](...)       # call salt.states.*
__salt__['file.file_exists'](...)   # call salt.modules.*

# __pillar__  # ?

Basics

States themselves are very simple to write. The state is simply a function within the python module.

def write_hi_to_disk():
    print('hi')
    retval = {
        'name': 'mymodule.print_hi',
        'result': True,
        'changes': {
            'console': {'old': '', 'new': 'hi'},
            ...
        },
        'comment': 'printed hi',
    }
    return retval