remote_execution.rst 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. ================================
  2. Running Commands on Salt Minions
  3. ================================
  4. Salt can be controlled by a command line client by the root user on the Salt
  5. master. The Salt command line client uses the Salt client API to communicate
  6. with the Salt master server. The Salt client is straightforward and simple
  7. to use.
  8. Using the Salt client commands can be easily sent to the minions.
  9. Each of these commands accepts an explicit `--config` option to point to either
  10. the master or minion configuration file. If this option is not provided and
  11. the default configuration file does not exist then Salt falls back to use the
  12. environment variables ``SALT_MASTER_CONFIG`` and ``SALT_MINION_CONFIG``.
  13. .. seealso::
  14. :ref:`Configuration <configuring-salt>`
  15. Using the Salt Command
  16. ======================
  17. The Salt command needs a few components to send information to the Salt
  18. minions. The target minions need to be defined, the function to call and any
  19. arguments the function requires.
  20. Defining the Target Minions
  21. ---------------------------
  22. The first argument passed to salt, defines the target minions, the target
  23. minions are accessed via their hostname. The default target type is a bash
  24. glob:
  25. .. code-block:: bash
  26. salt '*foo.com' sys.doc
  27. Salt can also define the target minions with regular expressions:
  28. .. code-block:: bash
  29. salt -E '.*' cmd.run 'ls -l | grep foo'
  30. Or to explicitly list hosts, salt can take a list:
  31. .. code-block:: bash
  32. salt -L foo.bar.baz,quo.qux cmd.run 'ps aux | grep foo'
  33. More Powerful Targets
  34. ---------------------
  35. See :ref:`Targeting <targeting>`.
  36. Calling the Function
  37. --------------------
  38. The function to call on the specified target is placed after the target
  39. specification.
  40. .. versionadded:: 0.9.8
  41. Functions may also accept arguments, space-delimited:
  42. .. code-block:: bash
  43. salt '*' cmd.exec_code python 'import sys; print sys.version'
  44. Optional, keyword arguments are also supported:
  45. .. code-block:: bash
  46. salt '*' pip.install salt timeout=5 upgrade=True
  47. They are always in the form of ``kwarg=argument``.
  48. Arguments are formatted as YAML:
  49. .. code-block:: bash
  50. salt '*' cmd.run 'echo "Hello: $FIRST_NAME"' env='{FIRST_NAME: "Joe"}'
  51. Note: dictionaries must have curly braces around them (like the ``env``
  52. keyword argument above). This was changed in 0.15.1: in the above example,
  53. the first argument used to be parsed as the dictionary
  54. ``{'echo "Hello': '$FIRST_NAME"'}``. This was generally not the expected
  55. behavior.
  56. If you want to test what parameters are actually passed to a module, use the
  57. ``test.arg_repr`` command:
  58. .. code-block:: bash
  59. salt '*' test.arg_repr 'echo "Hello: $FIRST_NAME"' env='{FIRST_NAME: "Joe"}'
  60. Finding available minion functions
  61. ``````````````````````````````````
  62. The Salt functions are self documenting, all of the function documentation can
  63. be retried from the minions via the :func:`sys.doc` function:
  64. .. code-block:: bash
  65. salt '*' sys.doc
  66. Compound Command Execution
  67. --------------------------
  68. If a series of commands needs to be sent to a single target specification then
  69. the commands can be sent in a single publish. This can make gathering
  70. groups of information faster, and lowers the stress on the network for repeated
  71. commands.
  72. Compound command execution works by sending a list of functions and arguments
  73. instead of sending a single function and argument. The functions are executed
  74. on the minion in the order they are defined on the command line, and then the
  75. data from all of the commands are returned in a dictionary. This means that
  76. the set of commands are called in a predictable way, and the returned data can
  77. be easily interpreted.
  78. Executing compound commands if done by passing a comma delimited list of
  79. functions, followed by a comma delimited list of arguments:
  80. .. code-block:: bash
  81. salt '*' cmd.run,test.ping,test.echo 'cat /proc/cpuinfo',,foo
  82. The trick to look out for here, is that if a function is being passed no
  83. arguments, then there needs to be a placeholder for the absent arguments. This
  84. is why in the above example, there are two commas right next to each other.
  85. ``test.ping`` takes no arguments, so we need to add another comma, otherwise
  86. Salt would attempt to pass "foo" to ``test.ping``.
  87. If you need to pass arguments that include commas, then make sure you add
  88. spaces around the commas that separate arguments. For example:
  89. .. code-block:: bash
  90. salt '*' cmd.run,test.ping,test.echo 'echo "1,2,3"' , , foo
  91. You may change the arguments separator using the ``--args-separator`` option:
  92. .. code-block:: bash
  93. salt --args-separator=:: '*' some.fun,test.echo params with , comma :: foo
  94. CLI Completion
  95. ==============
  96. Shell completion scripts for the Salt CLI are available in the ``pkg`` Salt
  97. `source directory`_.
  98. .. _source directory: https://github.com/saltstack/salt/tree/develop/pkg