1
0

index.rst 3.9 KB

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