1
0

index.rst 22 KB

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