1
0

index.rst 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. .. _writing-execution-modules:
  2. =========================
  3. Writing Execution Modules
  4. =========================
  5. Salt execution modules are the functions called by the :command:`salt` command.
  6. Modules Are Easy to Write!
  7. ==========================
  8. Writing Salt execution modules is straightforward.
  9. A Salt execution module is a Python or `Cython`_ module placed in a directory
  10. called ``_modules/`` at the root of the Salt fileserver. When using the default
  11. fileserver backend (i.e. :py:mod:`roots <salt.fileserver.roots>`), unless
  12. environments are otherwise defined in the :conf_master:`file_roots` config
  13. option, the ``_modules/`` directory would be located in ``/srv/salt/_modules``
  14. on most systems.
  15. Modules placed in ``_modules/`` will be synced to the minions when any of the
  16. following Salt functions are called:
  17. * :mod:`state.highstate <salt.modules.state.highstate>` (or :mod:`state.apply
  18. <salt.modules.state.apply_>` with no state argument)
  19. * :mod:`saltutil.sync_modules <salt.modules.saltutil.sync_modules>`
  20. * :mod:`saltutil.sync_all <salt.modules.saltutil.sync_all>`
  21. Modules placed in ``_modules/`` will be synced to masters when any of the
  22. following Salt runners are called:
  23. * :mod:`saltutil.sync_modules <salt.runners.saltutil.sync_modules>`
  24. * :mod:`saltutil.sync_all <salt.runners.saltutil.sync_all>`
  25. Note that a module's default name is its filename
  26. (i.e. ``foo.py`` becomes module ``foo``), but that its name can be overridden
  27. by using a :ref:`__virtual__ function <virtual-modules>`.
  28. If a Salt module has errors and cannot be imported, the Salt minion will continue
  29. to load without issue and the module with errors will simply be omitted.
  30. If adding a Cython module the file must be named ``<modulename>.pyx`` so that
  31. the loader knows that the module needs to be imported as a Cython module. The
  32. compilation of the Cython module is automatic and happens when the minion
  33. starts, so only the ``*.pyx`` file is required.
  34. .. _`Cython`: https://cython.org/
  35. Zip Archives as Modules
  36. =======================
  37. Python 2.3 and higher allows developers to directly import zip archives
  38. containing Python code. By setting :conf_minion:`enable_zip_modules` to
  39. ``True`` in the minion config, the Salt loader will be able to import ``.zip``
  40. files in this fashion. This allows Salt module developers to package
  41. dependencies with their modules for ease of deployment, isolation, etc.
  42. For a user, Zip Archive modules behave just like other modules. When executing
  43. a function from a module provided as the file ``my_module.zip``, a user would
  44. call a function within that module as ``my_module.<function>``.
  45. Creating a Zip Archive Module
  46. -----------------------------
  47. A Zip Archive module is structured similarly to a simple `Python package`_.
  48. The ``.zip`` file contains a single directory with the same name as the module.
  49. The module code traditionally in ``<module_name>.py`` goes in
  50. ``<module_name>/__init__.py``. The dependency packages are subdirectories of
  51. ``<module_name>/``.
  52. Here is an example directory structure for the ``lumberjack`` module, which has
  53. two library dependencies (``sleep`` and ``work``) to be included.
  54. .. code-block:: bash
  55. modules $ ls -R lumberjack
  56. __init__.py sleep work
  57. lumberjack/sleep:
  58. __init__.py
  59. lumberjack/work:
  60. __init__.py
  61. The contents of ``lumberjack/__init__.py`` show how to import and use these
  62. included libraries.
  63. .. code-block:: python
  64. # Libraries included in lumberjack.zip
  65. from lumberjack import sleep, work
  66. def is_ok(person):
  67. ''' Checks whether a person is really a lumberjack '''
  68. return sleep.all_night(person) and work.all_day(person)
  69. Then, create the zip:
  70. .. code-block:: bash
  71. modules $ zip -r lumberjack lumberjack
  72. adding: lumberjack/ (stored 0%)
  73. adding: lumberjack/__init__.py (deflated 39%)
  74. adding: lumberjack/sleep/ (stored 0%)
  75. adding: lumberjack/sleep/__init__.py (deflated 7%)
  76. adding: lumberjack/work/ (stored 0%)
  77. adding: lumberjack/work/__init__.py (deflated 7%)
  78. modules $ unzip -l lumberjack.zip
  79. Archive: lumberjack.zip
  80. Length Date Time Name
  81. -------- ---- ---- ----
  82. 0 08-21-15 20:08 lumberjack/
  83. 348 08-21-15 20:08 lumberjack/__init__.py
  84. 0 08-21-15 19:53 lumberjack/sleep/
  85. 83 08-21-15 19:53 lumberjack/sleep/__init__.py
  86. 0 08-21-15 19:53 lumberjack/work/
  87. 81 08-21-15 19:21 lumberjack/work/__init__.py
  88. -------- -------
  89. 512 6 files
  90. Once placed in :conf_master:`file_roots`, Salt users can distribute and use
  91. ``lumberjack.zip`` like any other module.
  92. .. code-block:: bash
  93. $ sudo salt minion1 saltutil.sync_modules
  94. minion1:
  95. - modules.lumberjack
  96. $ sudo salt minion1 lumberjack.is_ok 'Michael Palin'
  97. minion1:
  98. True
  99. .. _`Python package`: https://docs.python.org/2/tutorial/modules.html#packages
  100. .. _cross-calling-execution-modules:
  101. Cross Calling Execution Modules
  102. ===============================
  103. All of the Salt execution modules are available to each other and modules can
  104. call functions available in other execution modules.
  105. The variable ``__salt__`` is packed into the modules after they are loaded into
  106. the Salt minion.
  107. The ``__salt__`` variable is a :ref:`Python dictionary <python:typesmapping>`
  108. containing all of the Salt functions. Dictionary keys are strings representing
  109. the names of the modules and the values are the functions themselves.
  110. Salt modules can be cross-called by accessing the value in the ``__salt__``
  111. dict:
  112. .. code-block:: python
  113. def foo(bar):
  114. return __salt__['cmd.run'](bar)
  115. This code will call the `run` function in the :mod:`cmd <salt.modules.cmdmod>`
  116. module and pass the argument ``bar`` to it.
  117. Calling Execution Modules on the Salt Master
  118. ============================================
  119. .. versionadded:: 2016.11.0
  120. Execution modules can now also be called via the :command:`salt-run` command
  121. using the :ref:`salt runner <salt_salt_runner>`.
  122. Preloaded Execution Module Data
  123. ===============================
  124. When interacting with execution modules often it is nice to be able to read
  125. information dynamically about the minion or to load in configuration parameters
  126. for a module.
  127. Salt allows for different types of data to be loaded into the modules by the
  128. minion.
  129. Grains Data
  130. -----------
  131. The values detected by the Salt Grains on the minion are available in a
  132. :ref:`Python dictionary <python:typesmapping>` named ``__grains__`` and can be
  133. accessed from within callable objects in the Python modules.
  134. To see the contents of the grains dictionary for a given system in your
  135. deployment run the :func:`grains.items` function:
  136. .. code-block:: bash
  137. salt 'hostname' grains.items --output=pprint
  138. Any value in a grains dictionary can be accessed as any other Python
  139. dictionary. For example, the grain representing the minion ID is stored in the
  140. ``id`` key and from an execution module, the value would be stored in
  141. ``__grains__['id']``.
  142. Module Configuration
  143. --------------------
  144. Since parameters for configuring a module may be desired, Salt allows for
  145. configuration information from the minion configuration file to be passed to
  146. execution modules.
  147. Since the minion configuration file is a YAML document, arbitrary configuration
  148. data can be passed in the minion config that is read by the modules. It is
  149. therefore **strongly** recommended that the values passed in the configuration
  150. file match the module name. A value intended for the ``test`` execution module
  151. should be named ``test.<value>``.
  152. The test execution module contains usage of the module configuration and the
  153. default configuration file for the minion contains the information and format
  154. used to pass data to the modules. :mod:`salt.modules.test`,
  155. :file:`conf/minion`.
  156. .. _module_init:
  157. ``__init__`` Function
  158. ---------------------
  159. If you want your module to have different execution modes based on minion
  160. configuration, you can use the ``__init__(opts)`` function to perform initial
  161. module setup. The parameter ``opts`` is the complete minion configuration,
  162. as also available in the ``__opts__`` dict.
  163. .. code-block:: python
  164. '''
  165. Cheese module initialization example
  166. '''
  167. def __init__(opts):
  168. '''
  169. Allow foreign imports if configured to do so
  170. '''
  171. if opts.get('cheese.allow_foreign', False):
  172. _enable_foreign_products()
  173. Strings and Unicode
  174. ===================
  175. An execution module author should always assume that strings fed to the module
  176. have already decoded from strings into Unicode. In Python 2, these will
  177. be of type 'Unicode' and in Python 3 they will be of type ``str``. Calling
  178. from a state to other Salt sub-systems, should pass Unicode (or bytes if passing binary data). In the
  179. rare event that a state needs to write directly to disk, Unicode should be
  180. encoded to a string immediately before writing to disk. An author may use
  181. ``__salt_system_encoding__`` to learn what the encoding type of the system is.
  182. For example, `'my_string'.encode(__salt_system_encoding__')`.
  183. Outputter Configuration
  184. =======================
  185. Since execution module functions can return different data, and the way the
  186. data is printed can greatly change the presentation, Salt allows for a specific
  187. outputter to be set on a function-by-function basis.
  188. This is done be declaring an ``__outputter__`` dictionary in the global scope
  189. of the module. The ``__outputter__`` dictionary contains a mapping of function
  190. names to Salt :ref:`outputters <all-salt.output>`.
  191. .. code-block:: python
  192. __outputter__ = {
  193. 'run': 'txt'
  194. }
  195. This will ensure that the ``txt`` outputter is used to display output from the
  196. ``run`` function.
  197. .. _virtual-modules:
  198. Virtual Modules
  199. ===============
  200. Virtual modules let you override the name of a module in order to use the same
  201. name to refer to one of several similar modules. The specific module that is
  202. loaded for a virtual name is selected based on the current platform or
  203. environment.
  204. For example, packages are managed across platforms using the ``pkg`` module.
  205. ``pkg`` is a virtual module name that is an alias for the specific package
  206. manager module that is loaded on a specific system (for example, :mod:`yumpkg
  207. <salt.modules.yumpkg>` on RHEL/CentOS systems , and :mod:`aptpkg
  208. <salt.modules.aptpkg>` on Ubuntu).
  209. Virtual module names are set using the ``__virtual__`` function and the
  210. :ref:`virtual name <modules-virtual-name>`.
  211. ``__virtual__`` Function
  212. ========================
  213. The ``__virtual__`` function returns either a :ref:`string <python:typesseq>`,
  214. :py:data:`True`, :py:data:`False`, or :py:data:`False` with an :ref:`error
  215. string <modules-error-info>`. If a string is returned then the module is loaded
  216. using the name of the string as the virtual name. If ``True`` is returned the
  217. module is loaded using the current module name. If ``False`` is returned the
  218. module is not loaded. ``False`` lets the module perform system checks and
  219. prevent loading if dependencies are not met.
  220. Since ``__virtual__`` is called before the module is loaded, ``__salt__`` will
  221. be unreliable as not all modules will be available at this point in time. The
  222. ``__pillar__`` and ``__grains__`` :ref:`"dunder" dictionaries <dunder-dictionaries>`
  223. are available however.
  224. .. note::
  225. Modules which return a string from ``__virtual__`` that is already used by
  226. a module that ships with Salt will _override_ the stock module.
  227. .. _modules-error-info:
  228. Returning Error Information from ``__virtual__``
  229. ------------------------------------------------
  230. Optionally, Salt plugin modules, such as execution, state, returner, beacon,
  231. etc. modules may additionally return a string containing the reason that a
  232. module could not be loaded. For example, an execution module called ``cheese``
  233. and a corresponding state module also called ``cheese``, both depending on a
  234. utility called ``enzymes`` should have ``__virtual__`` functions that handle
  235. the case when the dependency is unavailable.
  236. .. code-block:: python
  237. '''
  238. Cheese execution (or returner/beacon/etc.) module
  239. '''
  240. try:
  241. import enzymes
  242. HAS_ENZYMES = True
  243. except ImportError:
  244. HAS_ENZYMES = False
  245. def __virtual__():
  246. '''
  247. only load cheese if enzymes are available
  248. '''
  249. if HAS_ENZYMES:
  250. return 'cheese'
  251. else:
  252. return False, 'The cheese execution module cannot be loaded: enzymes unavailable.'
  253. def slice():
  254. pass
  255. .. code-block:: python
  256. '''
  257. Cheese state module. Note that this works in state modules because it is
  258. guaranteed that execution modules are loaded first
  259. '''
  260. def __virtual__():
  261. '''
  262. only load cheese if enzymes are available
  263. '''
  264. # predicate loading of the cheese state on the corresponding execution module
  265. if 'cheese.slice' in __salt__:
  266. return 'cheese'
  267. else:
  268. return False, 'The cheese state module cannot be loaded: enzymes unavailable.'
  269. Examples
  270. --------
  271. The package manager modules are among the best examples of using the
  272. ``__virtual__`` function. A table of all the virtual ``pkg`` modules can be
  273. found :ref:`here <virtual-pkg>`.
  274. .. _module-provider-override:
  275. Overriding Virtual Module Providers
  276. -----------------------------------
  277. Salt often uses OS grains (``os``, ``osrelease``, ``os_family``, etc.) to
  278. determine which module should be loaded as the virtual module for ``pkg``,
  279. ``service``, etc. Sometimes this OS detection is incomplete, with new distros
  280. popping up, existing distros changing init systems, etc. The virtual modules
  281. likely to be affected by this are in the list below (click each item for more
  282. information):
  283. - :ref:`pkg <virtual-pkg>`
  284. - :ref:`service <virtual-service>`
  285. - :ref:`user <virtual-user>`
  286. - :ref:`shadow <virtual-shadow>`
  287. - :ref:`group <virtual-group>`
  288. If Salt is using the wrong module for one of these, first of all, please
  289. `report it on the issue tracker`__, so that this issue can be resolved for a
  290. future release. To make it easier to troubleshoot, please also provide the
  291. :py:mod:`grains.items <salt.modules.grains.items>` output, taking care to
  292. redact any sensitive information.
  293. Then, while waiting for the SaltStack development team to fix the issue, Salt
  294. can be made to use the correct module using the :conf_minion:`providers` option
  295. in the minion config file:
  296. .. code-block:: yaml
  297. providers:
  298. service: systemd
  299. pkg: aptpkg
  300. The above example will force the minion to use the :py:mod:`systemd
  301. <salt.modules.systemd>` module to provide service management, and the
  302. :py:mod:`aptpkg <salt.modules.aptpkg>` module to provide package management.
  303. .. __: https://github.com/saltstack/salt/issues/new
  304. Logging Restrictions
  305. --------------------
  306. As a rule, logging should not be done anywhere in a Salt module before it is
  307. loaded. This rule apples to all code that would run before the ``__virtual__()``
  308. function, as well as the code within the ``__virtual__()`` function itself.
  309. If logging statements are made before the virtual function determines if
  310. the module should be loaded, then those logging statements will be called
  311. repeatedly. This clutters up log files unnecessarily.
  312. Exceptions may be considered for logging statements made at the ``trace`` level.
  313. However, it is better to provide the necessary information by another means.
  314. One method is to :ref:`return error information <modules-error-info>` in the
  315. ``__virtual__()`` function.
  316. .. _modules-virtual-name:
  317. ``__virtualname__``
  318. ===================
  319. ``__virtualname__`` is a variable that is used by the documentation build
  320. system to know the virtual name of a module without calling the ``__virtual__``
  321. function. Modules that return a string from the ``__virtual__`` function
  322. must also set the ``__virtualname__`` variable.
  323. To avoid setting the virtual name string twice, you can implement
  324. ``__virtual__`` to return the value set for ``__virtualname__`` using a pattern
  325. similar to the following:
  326. .. code-block:: python
  327. # Define the module's virtual name
  328. __virtualname__ = 'pkg'
  329. def __virtual__():
  330. '''
  331. Confine this module to Mac OS with Homebrew.
  332. '''
  333. if salt.utils.path.which('brew') and __grains__['os'] == 'MacOS':
  334. return __virtualname__
  335. return False
  336. The ``__virtual__()`` function can return a ``True`` or ``False`` boolean, a tuple,
  337. or a string. If it returns a ``True`` value, this ``__virtualname__`` module-level
  338. attribute can be set as seen in the above example. This is the string that the module
  339. should be referred to as.
  340. When ``__virtual__()`` returns a tuple, the first item should be a boolean and the
  341. second should be a string. This is typically done when the module should not load. The
  342. first value of the tuple is ``False`` and the second is the error message to display
  343. for why the module did not load.
  344. For example:
  345. .. code-block:: python
  346. def __virtual__():
  347. '''
  348. Only load if git exists on the system
  349. '''
  350. if salt.utils.path.which('git') is None:
  351. return (False,
  352. 'The git execution module cannot be loaded: git unavailable.')
  353. else:
  354. return True
  355. Documentation
  356. =============
  357. Salt execution modules are documented. The :func:`sys.doc` function will return
  358. the documentation for all available modules:
  359. .. code-block:: bash
  360. salt '*' sys.doc
  361. The ``sys.doc`` function simply prints out the docstrings found in the modules;
  362. when writing Salt execution modules, please follow the formatting conventions
  363. for docstrings as they appear in the other modules.
  364. Adding Documentation to Salt Modules
  365. ------------------------------------
  366. It is strongly suggested that all Salt modules have documentation added.
  367. To add documentation add a `Python docstring`_ to the function.
  368. .. code-block:: python
  369. def spam(eggs):
  370. '''
  371. A function to make some spam with eggs!
  372. CLI Example::
  373. salt '*' test.spam eggs
  374. '''
  375. return eggs
  376. Now when the sys.doc call is executed the docstring will be cleanly returned
  377. to the calling terminal.
  378. .. _`Python docstring`: https://docs.python.org/3/glossary.html#term-docstring
  379. Documentation added to execution modules in docstrings will automatically be
  380. added to the online web-based documentation.
  381. Add Execution Module Metadata
  382. -----------------------------
  383. When writing a Python docstring for an execution module, add information about
  384. the module using the following field lists:
  385. .. code-block:: text
  386. :maintainer: Thomas Hatch <thatch@saltstack.com, Seth House <shouse@saltstack.com>
  387. :maturity: new
  388. :depends: python-mysqldb
  389. :platform: all
  390. The maintainer field is a comma-delimited list of developers who help maintain
  391. this module.
  392. The maturity field indicates the level of quality and testing for this module.
  393. Standard labels will be determined.
  394. The depends field is a comma-delimited list of modules that this module depends
  395. on.
  396. The platform field is a comma-delimited list of platforms that this module is
  397. known to run on.
  398. Log Output
  399. ==========
  400. You can call the logger from custom modules to write messages to the minion
  401. logs. The following code snippet demonstrates writing log messages:
  402. .. code-block:: python
  403. import logging
  404. log = logging.getLogger(__name__)
  405. log.info('Here is Some Information')
  406. log.warning('You Should Not Do That')
  407. log.error('It Is Busted')
  408. Aliasing Functions
  409. ==================
  410. Sometimes one wishes to use a function name that would shadow a python built-in.
  411. A common example would be ``set()``. To support this, append an underscore to
  412. the function definition, ``def set_():``, and use the ``__func_alias__`` feature
  413. to provide an alias to the function.
  414. ``__func_alias__`` is a dictionary where each key is the name of a function in
  415. the module, and each value is a string representing the alias for that function.
  416. When calling an aliased function from a different execution module, state
  417. module, or from the cli, the alias name should be used.
  418. .. code-block:: python
  419. __func_alias__ = {
  420. 'set_': 'set',
  421. 'list_': 'list',
  422. }
  423. Private Functions
  424. =================
  425. In Salt, Python callable objects contained within an execution module are made
  426. available to the Salt minion for use. The only exception to this rule is a
  427. callable object with a name starting with an underscore ``_``.
  428. Objects Loaded Into the Salt Minion
  429. -----------------------------------
  430. .. code-block:: python
  431. def foo(bar):
  432. return bar
  433. Objects NOT Loaded into the Salt Minion
  434. ---------------------------------------
  435. .. code-block:: python
  436. def _foobar(baz): # Preceded with an _
  437. return baz
  438. cheese = {} # Not a callable Python object
  439. Useful Decorators for Modules
  440. =============================
  441. Depends Decorator
  442. -----------------
  443. When writing execution modules there are many times where some of the module
  444. will work on all hosts but some functions have an external dependency, such as
  445. a service that needs to be installed or a binary that needs to be present on
  446. the system.
  447. Instead of trying to wrap much of the code in large try/except blocks, a
  448. decorator can be used.
  449. If the dependencies passed to the decorator don't exist, then the salt minion
  450. will remove those functions from the module on that host.
  451. If a ``fallback_function`` is defined, it will replace the function instead of
  452. removing it
  453. .. code-block:: python
  454. import logging
  455. from salt.utils.decorators import depends
  456. log = logging.getLogger(__name__)
  457. try:
  458. import dependency_that_sometimes_exists
  459. except ImportError as e:
  460. log.trace('Failed to import dependency_that_sometimes_exists: {0}'.format(e))
  461. @depends('dependency_that_sometimes_exists')
  462. def foo():
  463. '''
  464. Function with a dependency on the "dependency_that_sometimes_exists" module,
  465. if the "dependency_that_sometimes_exists" is missing this function will not exist
  466. '''
  467. return True
  468. def _fallback():
  469. '''
  470. Fallback function for the depends decorator to replace a function with
  471. '''
  472. return '"dependency_that_sometimes_exists" needs to be installed for this function to exist'
  473. @depends('dependency_that_sometimes_exists', fallback_function=_fallback)
  474. def foo():
  475. '''
  476. Function with a dependency on the "dependency_that_sometimes_exists" module.
  477. If the "dependency_that_sometimes_exists" is missing this function will be
  478. replaced with "_fallback"
  479. '''
  480. return True
  481. In addition to global dependencies the depends decorator also supports raw
  482. booleans.
  483. .. code-block:: python
  484. from salt.utils.decorators import depends
  485. HAS_DEP = False
  486. try:
  487. import dependency_that_sometimes_exists
  488. HAS_DEP = True
  489. except ImportError:
  490. pass
  491. @depends(HAS_DEP)
  492. def foo():
  493. return True