writing.rst 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. .. _state-modules:
  2. =============
  3. State Modules
  4. =============
  5. State Modules are the components that map to actual enforcement and management
  6. of Salt states.
  7. .. _writing-state-modules:
  8. States are Easy to Write!
  9. =========================
  10. State Modules should be easy to write and straightforward. The information
  11. passed to the SLS data structures will map directly to the states modules.
  12. Mapping the information from the SLS data is simple, this example should
  13. illustrate:
  14. .. code-block:: yaml
  15. /etc/salt/master: # maps to "name", unless a "name" argument is specified below
  16. file.managed: # maps to <filename>.<function> - e.g. "managed" in https://github.com/saltstack/salt/tree/develop/salt/states/file.py
  17. - user: root # one of many options passed to the manage function
  18. - group: root
  19. - mode: 644
  20. - source: salt://salt/master
  21. Therefore this SLS data can be directly linked to a module, function, and
  22. arguments passed to that function.
  23. This does issue the burden, that function names, state names and function
  24. arguments should be very human readable inside state modules, since they
  25. directly define the user interface.
  26. .. admonition:: Keyword Arguments
  27. Salt passes a number of keyword arguments to states when rendering them,
  28. including the environment, a unique identifier for the state, and more.
  29. Additionally, keep in mind that the requisites for a state are part of the
  30. keyword arguments. Therefore, if you need to iterate through the keyword
  31. arguments in a state, these must be considered and handled appropriately.
  32. One such example is in the :mod:`pkgrepo.managed
  33. <salt.states.pkgrepo.managed>` state, which needs to be able to handle
  34. arbitrary keyword arguments and pass them to module execution functions.
  35. An example of how these keyword arguments can be handled can be found
  36. here_.
  37. .. _here: https://github.com/saltstack/salt/blob/v0.16.2/salt/states/pkgrepo.py#L163-183
  38. Best Practices
  39. ==============
  40. A well-written state function will follow these steps:
  41. .. note::
  42. This is an extremely simplified example. Feel free to browse the `source
  43. code`_ for Salt's state modules to see other examples.
  44. .. _`source code`: https://github.com/saltstack/salt/tree/develop/salt/states
  45. 1. Set up the return dictionary and perform any necessary input validation
  46. (type checking, looking for use of mutually-exclusive arguments, etc.).
  47. .. code-block:: python
  48. ret = {'name': name,
  49. 'result': False,
  50. 'changes': {},
  51. 'comment': ''}
  52. if foo and bar:
  53. ret['comment'] = 'Only one of foo and bar is permitted'
  54. return ret
  55. 2. Check if changes need to be made. This is best done with an
  56. information-gathering function in an accompanying :ref:`execution module
  57. <writing-execution-modules>`. The state should be able to use the return
  58. from this function to tell whether or not the minion is already in the
  59. desired state.
  60. .. code-block:: python
  61. result = __salt__['modname.check'](name)
  62. 3. If step 2 found that the minion is already in the desired state, then exit
  63. immediately with a ``True`` result and without making any changes.
  64. .. code-block:: python
  65. if result:
  66. ret['result'] = True
  67. ret['comment'] = '{0} is already installed'.format(name)
  68. return ret
  69. 4. If step 2 found that changes *do* need to be made, then check to see if the
  70. state was being run in test mode (i.e. with ``test=True``). If so, then exit
  71. with a ``None`` result, a relevant comment, and (if possible) a ``changes``
  72. entry describing what changes would be made.
  73. .. code-block:: python
  74. if __opts__['test']:
  75. ret['result'] = None
  76. ret['comment'] = '{0} would be installed'.format(name)
  77. ret['changes'] = result
  78. return ret
  79. 5. Make the desired changes. This should again be done using a function from an
  80. accompanying execution module. If the result of that function is enough to
  81. tell you whether or not an error occurred, then you can exit with a
  82. ``False`` result and a relevant comment to explain what happened.
  83. .. code-block:: python
  84. result = __salt__['modname.install'](name)
  85. 6. Perform the same check from step 2 again to confirm whether or not the
  86. minion is in the desired state. Just as in step 2, this function should be
  87. able to tell you by its return data whether or not changes need to be made.
  88. .. code-block:: python
  89. ret['changes'] = __salt__['modname.check'](name)
  90. As you can see here, we are setting the ``changes`` key in the return
  91. dictionary to the result of the ``modname.check`` function (just as we did
  92. in step 4). The assumption here is that the information-gathering function
  93. will return a dictionary explaining what changes need to be made. This may
  94. or may not fit your use case.
  95. 7. Set the return data and return!
  96. .. code-block:: python
  97. if ret['changes']:
  98. ret['comment'] = '{0} failed to install'.format(name)
  99. else:
  100. ret['result'] = True
  101. ret['comment'] = '{0} was installed'.format(name)
  102. return ret
  103. Using Custom State Modules
  104. ==========================
  105. Before the state module can be used, it must be distributed to minions. This
  106. can be done by placing them into ``salt://_states/``. They can then be
  107. distributed manually to minions by running :mod:`saltutil.sync_states
  108. <salt.modules.saltutil.sync_states>` or :mod:`saltutil.sync_all
  109. <salt.modules.saltutil.sync_all>`. Alternatively, when running a
  110. :ref:`highstate <running-highstate>` custom types will automatically be synced.
  111. NOTE: Writing state modules with hyphens in the filename will cause issues
  112. with !pyobjects routines. Best practice to stick to underscores.
  113. Any custom states which have been synced to a minion, that are named the same
  114. as one of Salt's default set of states, will take the place of the default
  115. state with the same name. Note that a state module's name defaults to one based
  116. on its filename (i.e. ``foo.py`` becomes state module ``foo``), but that its
  117. name can be overridden by using a :ref:`__virtual__ function
  118. <virtual-modules>`.
  119. Cross Calling Execution Modules from States
  120. ===========================================
  121. As with Execution Modules, State Modules can also make use of the ``__salt__``
  122. and ``__grains__`` data. See :ref:`cross calling execution modules
  123. <cross-calling-execution-modules>`.
  124. It is important to note that the real work of state management should not be
  125. done in the state module unless it is needed. A good example is the pkg state
  126. module. This module does not do any package management work, it just calls the
  127. pkg execution module. This makes the pkg state module completely generic, which
  128. is why there is only one pkg state module and many backend pkg execution
  129. modules.
  130. On the other hand some modules will require that the logic be placed in the
  131. state module, a good example of this is the file module. But in the vast
  132. majority of cases this is not the best approach, and writing specific
  133. execution modules to do the backend work will be the optimal solution.
  134. .. _cross-calling-state-modules:
  135. Cross Calling State Modules
  136. ===========================
  137. All of the Salt state modules are available to each other and state modules can call
  138. functions available in other state modules.
  139. The variable ``__states__`` is packed into the modules after they are loaded into
  140. the Salt minion.
  141. The ``__states__`` variable is a :ref:`Python dictionary <python:typesmapping>`
  142. containing all of the state modules. Dictionary keys are strings representing
  143. the names of the modules and the values are the functions themselves.
  144. Salt state modules can be cross-called by accessing the value in the
  145. ``__states__`` dict:
  146. .. code-block:: python
  147. ret = __states__['file.managed'](name='/tmp/myfile', source='salt://myfile')
  148. This code will call the `managed` function in the :mod:`file
  149. <salt.states.file>` state module and pass the arguments ``name`` and ``source``
  150. to it.
  151. .. _state-return-data:
  152. Return Data
  153. ===========
  154. A State Module must return a dict containing the following keys/values:
  155. - **name:** The same value passed to the state as "name".
  156. - **changes:** A dict describing the changes made. Each thing changed should
  157. be a key, with its value being another dict with keys called "old" and "new"
  158. containing the old/new values. For example, the pkg state's **changes** dict
  159. has one key for each package changed, with the "old" and "new" keys in its
  160. sub-dict containing the old and new versions of the package. For example,
  161. the final changes dictionary for this scenario would look something like this:
  162. .. code-block:: python
  163. ret['changes'].update({'my_pkg_name': {'old': '',
  164. 'new': 'my_pkg_name-1.0'}})
  165. - **result:** A tristate value. ``True`` if the action was successful,
  166. ``False`` if it was not, or ``None`` if the state was run in test mode,
  167. ``test=True``, and changes would have been made if the state was not run in
  168. test mode.
  169. +--------------------+-----------+------------------------+
  170. | | live mode | test mode |
  171. +====================+===========+========================+
  172. | no changes | ``True`` | ``True`` |
  173. +--------------------+-----------+------------------------+
  174. | successful changes | ``True`` | ``None`` |
  175. +--------------------+-----------+------------------------+
  176. | failed changes | ``False`` | ``False`` or ``None`` |
  177. +--------------------+-----------+------------------------+
  178. .. note::
  179. Test mode does not predict if the changes will be successful or not,
  180. and hence the result for pending changes is usually ``None``.
  181. However, if a state is going to fail and this can be determined
  182. in test mode without applying the change, ``False`` can be returned.
  183. - **comment:** A list of strings or a single string summarizing the result.
  184. Note that support for lists of strings is available as of Salt 2018.3.0.
  185. Lists of strings will be joined with newlines to form the final comment;
  186. this is useful to allow multiple comments from subparts of a state.
  187. Prefer to keep line lengths short (use multiple lines as needed),
  188. and end with punctuation (e.g. a period) to delimit multiple comments.
  189. .. note::
  190. States should not return data which cannot be serialized such as frozensets.
  191. Test State
  192. ==========
  193. All states should check for and support ``test`` being passed in the options.
  194. This will return data about what changes would occur if the state were actually
  195. run. An example of such a check could look like this:
  196. .. code-block:: python
  197. # Return comment of changes if test.
  198. if __opts__['test']:
  199. ret['result'] = None
  200. ret['comment'] = 'State Foo will execute with param {0}'.format(bar)
  201. return ret
  202. Make sure to test and return before performing any real actions on the minion.
  203. .. note::
  204. Be sure to refer to the ``result`` table listed above and displaying any
  205. possible changes when writing support for ``test``. Looking for changes in
  206. a state is essential to ``test=true`` functionality. If a state is predicted
  207. to have no changes when ``test=true`` (or ``test: true`` in a config file)
  208. is used, then the result of the final state **should not** be ``None``.
  209. Watcher Function
  210. ================
  211. If the state being written should support the watch requisite then a watcher
  212. function needs to be declared. The watcher function is called whenever the
  213. watch requisite is invoked and should be generic to the behavior of the state
  214. itself.
  215. The watcher function should accept all of the options that the normal state
  216. functions accept (as they will be passed into the watcher function).
  217. A watcher function typically is used to execute state specific reactive
  218. behavior, for instance, the watcher for the service module restarts the
  219. named service and makes it useful for the watcher to make the service
  220. react to changes in the environment.
  221. The watcher function also needs to return the same data that a normal state
  222. function returns.
  223. Mod_init Interface
  224. ==================
  225. Some states need to execute something only once to ensure that an environment
  226. has been set up, or certain conditions global to the state behavior can be
  227. predefined. This is the realm of the mod_init interface.
  228. A state module can have a function called **mod_init** which executes when the
  229. first state of this type is called. This interface was created primarily to
  230. improve the pkg state. When packages are installed the package metadata needs
  231. to be refreshed, but refreshing the package metadata every time a package is
  232. installed is wasteful. The mod_init function for the pkg state sets a flag down
  233. so that the first, and only the first, package installation attempt will refresh
  234. the package database (the package database can of course be manually called to
  235. refresh via the ``refresh`` option in the pkg state).
  236. The mod_init function must accept the **Low State Data** for the given
  237. executing state as an argument. The low state data is a dict and can be seen by
  238. executing the state.show_lowstate function. Then the mod_init function must
  239. return a bool. If the return value is True, then the mod_init function will not
  240. be executed again, meaning that the needed behavior has been set up. Otherwise,
  241. if the mod_init function returns False, then the function will be called the
  242. next time.
  243. A good example of the mod_init function is found in the pkg state module:
  244. .. code-block:: python
  245. def mod_init(low):
  246. '''
  247. Refresh the package database here so that it only needs to happen once
  248. '''
  249. if low['fun'] == 'installed' or low['fun'] == 'latest':
  250. rtag = __gen_rtag()
  251. if not os.path.exists(rtag):
  252. open(rtag, 'w+').write('')
  253. return True
  254. else:
  255. return False
  256. The mod_init function in the pkg state accepts the low state data as ``low``
  257. and then checks to see if the function being called is going to install
  258. packages, if the function is not going to install packages then there is no
  259. need to refresh the package database. Therefore if the package database is
  260. prepared to refresh, then return True and the mod_init will not be called
  261. the next time a pkg state is evaluated, otherwise return False and the mod_init
  262. will be called next time a pkg state is evaluated.
  263. Log Output
  264. ==========
  265. You can call the logger from custom modules to write messages to the minion
  266. logs. The following code snippet demonstrates writing log messages:
  267. .. code-block:: python
  268. import logging
  269. log = logging.getLogger(__name__)
  270. log.info('Here is Some Information')
  271. log.warning('You Should Not Do That')
  272. log.error('It Is Busted')
  273. Strings and Unicode
  274. ===================
  275. A state module author should always assume that strings fed to the module
  276. have already decoded from strings into Unicode. In Python 2, these will
  277. be of type 'Unicode' and in Python 3 they will be of type ``str``. Calling
  278. from a state to other Salt sub-systems, such as execution modules should
  279. pass Unicode (or bytes if passing binary data). In the rare event that a state needs to write directly
  280. to disk, Unicode should be encoded to a string immediately before writing
  281. to disk. An author may use ``__salt_system_encoding__`` to learn what the
  282. encoding type of the system is. For example,
  283. `'my_string'.encode(__salt_system_encoding__')`.
  284. Full State Module Example
  285. =========================
  286. The following is a simplistic example of a full state module and function.
  287. Remember to call out to execution modules to perform all the real work. The
  288. state module should only perform "before" and "after" checks.
  289. 1. Make a custom state module by putting the code into a file at the following
  290. path: **/srv/salt/_states/my_custom_state.py**.
  291. 2. Distribute the custom state module to the minions:
  292. .. code-block:: bash
  293. salt '*' saltutil.sync_states
  294. 3. Write a new state to use the custom state by making a new state file, for
  295. instance **/srv/salt/my_custom_state.sls**.
  296. 4. Add the following SLS configuration to the file created in Step 3:
  297. .. code-block:: yaml
  298. human_friendly_state_id: # An arbitrary state ID declaration.
  299. my_custom_state: # The custom state module name.
  300. - enforce_custom_thing # The function in the custom state module.
  301. - name: a_value # Maps to the ``name`` parameter in the custom function.
  302. - foo: Foo # Specify the required ``foo`` parameter.
  303. - bar: False # Override the default value for the ``bar`` parameter.
  304. Example state module
  305. --------------------
  306. .. code-block:: python
  307. import salt.exceptions
  308. def enforce_custom_thing(name, foo, bar=True):
  309. '''
  310. Enforce the state of a custom thing
  311. This state module does a custom thing. It calls out to the execution module
  312. ``my_custom_module`` in order to check the current system and perform any
  313. needed changes.
  314. name
  315. The thing to do something to
  316. foo
  317. A required argument
  318. bar : True
  319. An argument with a default value
  320. '''
  321. ret = {
  322. 'name': name,
  323. 'changes': {},
  324. 'result': False,
  325. 'comment': '',
  326. }
  327. # Start with basic error-checking. Do all the passed parameters make sense
  328. # and agree with each-other?
  329. if bar == True and foo.startswith('Foo'):
  330. raise salt.exceptions.SaltInvocationError(
  331. 'Argument "foo" cannot start with "Foo" if argument "bar" is True.')
  332. # Check the current state of the system. Does anything need to change?
  333. current_state = __salt__['my_custom_module.current_state'](name)
  334. if current_state == foo:
  335. ret['result'] = True
  336. ret['comment'] = 'System already in the correct state'
  337. return ret
  338. # The state of the system does need to be changed. Check if we're running
  339. # in ``test=true`` mode.
  340. if __opts__['test'] == True:
  341. ret['comment'] = 'The state of "{0}" will be changed.'.format(name)
  342. ret['changes'] = {
  343. 'old': current_state,
  344. 'new': 'Description, diff, whatever of the new state',
  345. }
  346. # Return ``None`` when running with ``test=true``.
  347. ret['result'] = None
  348. return ret
  349. # Finally, make the actual change and return the result.
  350. new_state = __salt__['my_custom_module.change_state'](name, foo)
  351. ret['comment'] = 'The state of "{0}" was changed!'.format(name)
  352. ret['changes'] = {
  353. 'old': current_state,
  354. 'new': new_state,
  355. }
  356. ret['result'] = True
  357. return ret