/doc/ref/runners/index.rst

https://gitlab.com/ricardo.hernandez/salt · ReStructuredText · 125 lines · 87 code · 38 blank · 0 comment · 0 complexity · eeb1802daa6412effd507633824544e7 MD5 · raw file

  1. =======
  2. Runners
  3. =======
  4. Salt runners are convenience applications executed with the salt-run command.
  5. Salt runners work similarly to Salt execution modules however they execute on the
  6. Salt master itself instead of remote Salt minions.
  7. A Salt runner can be a simple client call or a complex application.
  8. .. seealso:: :ref:`The full list of runners <all-salt.runners>`
  9. Writing Salt Runners
  10. --------------------
  11. A Salt runner is written in a similar manner to a Salt execution module.
  12. Both are Python modules which contain functions and each public function
  13. is a runner which may be executed via the *salt-run* command.
  14. For example, if a Python module named ``test.py`` is created in the runners
  15. directory and contains a function called ``foo``, the ``test`` runner could be
  16. invoked with the following command:
  17. .. code-block:: bash
  18. # salt-run test.foo
  19. Runners have several options for controlling output.
  20. Any ``print`` statement in a runner is automatically also
  21. fired onto the master event bus where. For example:
  22. .. code-block:: python
  23. def a_runner(outputter=None, display_progress=False):
  24. print('Hello world')
  25. ...
  26. The above would result in an event fired as follows:
  27. .. code-block:: bash
  28. Event fired at Tue Jan 13 15:26:45 2015
  29. *************************
  30. Tag: salt/run/20150113152644070246/print
  31. Data:
  32. {'_stamp': '2015-01-13T15:26:45.078707',
  33. 'data': 'hello',
  34. 'outputter': 'pprint'}
  35. A runner may also send a progress event, which is displayed to the user during
  36. runner execution and is also passed across the event bus if the ``display_progress``
  37. argument to a runner is set to True.
  38. A custom runner may send its own progress event by using the
  39. ``__jid_event_.fire_event()`` method as shown here:
  40. .. code-block:: python
  41. if display_progress:
  42. __jid_event__.fire_event({'message': 'A progress message'}, 'progress')
  43. The above would produce output on the console reading: ``A progress message``
  44. as well as an event on the event similar to:
  45. .. code-block:: bash
  46. Event fired at Tue Jan 13 15:21:20 2015
  47. *************************
  48. Tag: salt/run/20150113152118341421/progress
  49. Data:
  50. {'_stamp': '2015-01-13T15:21:20.390053',
  51. 'message': "A progress message"}
  52. A runner could use the same approach to send an event with a customized tag
  53. onto the event bus by replacing the second argument (``progress``) with
  54. whatever tag is desired. However, this will not be shown on the command-line
  55. and will only be fired onto the event bus.
  56. Synchronous vs. Asynchronous
  57. ----------------------------
  58. A runner may be fired asychronously which will immediately return control. In
  59. this case, no output will be display to the user if ``salt-run`` is being used
  60. from the command-line. If used programatically, no results will be returned.
  61. If results are desired, they must be gathered either by firing events on the
  62. bus from the runner and then watching for them or by some other means.
  63. .. note::
  64. When running a runner in asyncronous mode, the ``--progress`` flag will
  65. not deliver output to the salt-run CLI. However, progress events will
  66. still be fired on the bus.
  67. In synchronous mode, which is the default, control will not be returned until
  68. the runner has finished executing.
  69. To add custom runners, put them in a directory and add it to
  70. :conf_master:`runner_dirs` in the master configuration file.
  71. Examples
  72. --------
  73. Examples of runners can be found in the Salt distribution:
  74. :blob:`salt/runners`
  75. A simple runner that returns a well-formatted list of the minions that are
  76. responding to Salt calls could look like this:
  77. .. code-block:: python
  78. # Import salt modules
  79. import salt.client
  80. def up():
  81. '''
  82. Print a list of all of the minions that are up
  83. '''
  84. client = salt.client.LocalClient(__opts__['conf_file'])
  85. minions = client.cmd('*', 'test.ping', timeout=1)
  86. for minion in sorted(minions):
  87. print minion