1
0

index.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. .. _salt-ssh:
  2. ========
  3. Salt SSH
  4. ========
  5. .. raw:: html
  6. :file: index.html
  7. Getting Started
  8. ===============
  9. Salt SSH is very easy to use, simply set up a basic :ref:`roster <ssh-roster>` file of the
  10. systems to connect to and run ``salt-ssh`` commands in a similar way as
  11. standard ``salt`` commands.
  12. - Salt ssh is considered production ready in version 2014.7.0
  13. - Python is required on the remote system (unless using the ``-r`` option to
  14. send raw ssh commands). The python version requirement is the same as that
  15. for a standard :ref:`Salt installation <installation-dependencies>`.
  16. - On many systems, the ``salt-ssh`` executable will be in its own package, usually named
  17. ``salt-ssh``
  18. - The Salt SSH system does not supersede the standard Salt communication
  19. systems, it simply offers an SSH-based alternative that does not require
  20. ZeroMQ and a remote agent. Be aware that since all communication with Salt SSH is
  21. executed via SSH it is substantially slower than standard Salt with ZeroMQ.
  22. - At the moment fileserver operations must be wrapped to ensure that the
  23. relevant files are delivered with the ``salt-ssh`` commands.
  24. The state module is an exception, which compiles the state run on the
  25. master, and in the process finds all the references to ``salt://`` paths and
  26. copies those files down in the same tarball as the state run.
  27. However, needed fileserver wrappers are still under development.
  28. Salt SSH Roster
  29. ===============
  30. The roster system in Salt allows for remote minions to be easily defined.
  31. .. note::
  32. See the :ref:`SSH roster docs <ssh-roster>` for more details.
  33. Simply create the roster file, the default location is `/etc/salt/roster`:
  34. .. code-block:: yaml
  35. web1: 192.168.42.1
  36. This is a very basic roster file where a Salt ID is being assigned to an IP
  37. address. A more elaborate roster can be created:
  38. .. code-block:: yaml
  39. web1:
  40. host: 192.168.42.1 # The IP addr or DNS hostname
  41. user: fred # Remote executions will be executed as user fred
  42. passwd: foobarbaz # The password to use for login, if omitted, keys are used
  43. sudo: True # Whether to sudo to root, not enabled by default
  44. web2:
  45. host: 192.168.42.2
  46. .. note::
  47. sudo works only if NOPASSWD is set for user in /etc/sudoers:
  48. ``fred ALL=(ALL) NOPASSWD: ALL``
  49. Deploy ssh key for salt-ssh
  50. ===========================
  51. By default, salt-ssh will generate key pairs for ssh, the default path will be
  52. ``/etc/salt/pki/master/ssh/salt-ssh.rsa``. The key generation happens when you run
  53. ``salt-ssh`` for the first time.
  54. You can use ssh-copy-id, (the OpenSSH key deployment tool) to deploy keys to your servers.
  55. .. code-block:: bash
  56. ssh-copy-id -i /etc/salt/pki/master/ssh/salt-ssh.rsa.pub user@server.demo.com
  57. One could also create a simple shell script, named salt-ssh-copy-id.sh as follows:
  58. .. code-block:: bash
  59. #!/bin/bash
  60. if [ -z $1 ]; then
  61. echo $0 user@host.com
  62. exit 0
  63. fi
  64. ssh-copy-id -i /etc/salt/pki/master/ssh/salt-ssh.rsa.pub $1
  65. .. note::
  66. Be certain to chmod +x salt-ssh-copy-id.sh.
  67. .. code-block:: bash
  68. ./salt-ssh-copy-id.sh user@server1.host.com
  69. ./salt-ssh-copy-id.sh user@server2.host.com
  70. Once keys are successfully deployed, salt-ssh can be used to control them.
  71. Alternatively ssh agent forwarding can be used by setting the priv to agent-forwarding.
  72. Calling Salt SSH
  73. ================
  74. .. note:: ``salt-ssh`` on target hosts without Python 3
  75. The ``salt-ssh`` command requires at least python 3, which is not
  76. installed by default on some target hosts. An easy workaround in this
  77. situation is to use the ``-r`` option to run a raw shell command that
  78. installs python26:
  79. .. code-block:: bash
  80. salt-ssh centos-5-minion -r 'yum -y install epel-release ; yum -y install python26'
  81. .. note:: ``salt-ssh`` on systems with Python 3.x
  82. Salt, before the 2017.7.0 release, does not support Python 3.x which is the
  83. default on for example the popular 16.04 LTS release of Ubuntu. An easy
  84. workaround for this scenario is to use the ``-r`` option similar to the
  85. example above:
  86. .. code-block:: bash
  87. salt-ssh ubuntu-1604-minion -r 'apt update ; apt install -y python-minimal'
  88. The ``salt-ssh`` command can be easily executed in the same way as a salt
  89. command:
  90. .. code-block:: bash
  91. salt-ssh '*' test.version
  92. Commands with ``salt-ssh`` follow the same syntax as the ``salt`` command.
  93. The standard salt functions are available! The output is the same as ``salt``
  94. and many of the same flags are available. Please see
  95. http://docs.saltstack.com/ref/cli/salt-ssh.html for all of the available
  96. options.
  97. Raw Shell Calls
  98. ---------------
  99. By default ``salt-ssh`` runs Salt execution modules on the remote system,
  100. but ``salt-ssh`` can also execute raw shell commands:
  101. .. code-block:: bash
  102. salt-ssh '*' -r 'ifconfig'
  103. States Via Salt SSH
  104. ===================
  105. The Salt State system can also be used with ``salt-ssh``. The state system
  106. abstracts the same interface to the user in ``salt-ssh`` as it does when using
  107. standard ``salt``. The intent is that Salt Formulas defined for standard
  108. ``salt`` will work seamlessly with ``salt-ssh`` and vice-versa.
  109. The standard Salt States walkthroughs function by simply replacing ``salt``
  110. commands with ``salt-ssh``.
  111. Targeting with Salt SSH
  112. =======================
  113. Due to the fact that the targeting approach differs in salt-ssh, only glob
  114. and regex targets are supported as of this writing, the remaining target
  115. systems still need to be implemented.
  116. .. note::
  117. By default, Grains are settable through ``salt-ssh``. By
  118. default, these grains will *not* be persisted across reboots.
  119. See the "thin_dir" setting in :ref:`Roster documentation <ssh-roster>`
  120. for more details.
  121. Configuring Salt SSH
  122. ====================
  123. Salt SSH takes its configuration from a master configuration file. Normally, this
  124. file is in ``/etc/salt/master``. If one wishes to use a customized configuration file,
  125. the ``-c`` option to Salt SSH facilitates passing in a directory to look inside for a
  126. configuration file named ``master``.
  127. Minion Config
  128. -------------
  129. .. versionadded:: 2015.5.1
  130. Minion config options can be defined globally using the master configuration
  131. option ``ssh_minion_opts``. It can also be defined on a per-minion basis with
  132. the ``minion_opts`` entry in the roster.
  133. Running Salt SSH as non-root user
  134. =================================
  135. By default, Salt read all the configuration from /etc/salt/. If you are running
  136. Salt SSH with a regular user you have to modify some paths or you will get
  137. "Permission denied" messages. You have to modify two parameters: ``pki_dir``
  138. and ``cachedir``. Those should point to a full path writable for the user.
  139. It's recommended not to modify /etc/salt for this purpose. Create a private copy
  140. of /etc/salt for the user and run the command with ``-c /new/config/path``.
  141. Define CLI Options with Saltfile
  142. ================================
  143. If you are commonly passing in CLI options to ``salt-ssh``, you can create
  144. a ``Saltfile`` to automatically use these options. This is common if you're
  145. managing several different salt projects on the same server.
  146. So you can ``cd`` into a directory that has a ``Saltfile`` with the following
  147. YAML contents:
  148. .. code-block:: yaml
  149. salt-ssh:
  150. config_dir: path/to/config/dir
  151. ssh_log_file: salt-ssh.log
  152. ssh_max_procs: 30
  153. ssh_wipe: True
  154. Instead of having to call
  155. ``salt-ssh --config-dir=path/to/config/dir --max-procs=30 --wipe \* test.version`` you
  156. can call ``salt-ssh \* test.version``.
  157. Boolean-style options should be specified in their YAML representation.
  158. .. note::
  159. The option keys specified must match the destination attributes for the
  160. options specified in the parser
  161. :py:class:`salt.utils.parsers.SaltSSHOptionParser`. For example, in the
  162. case of the ``--wipe`` command line option, its ``dest`` is configured to
  163. be ``ssh_wipe`` and thus this is what should be configured in the
  164. ``Saltfile``. Using the names of flags for this option, being ``wipe:
  165. True`` or ``w: True``, will not work.
  166. .. note::
  167. For the `Saltfile` to be automatically detected it needs to be named
  168. `Saltfile` with a capital `S` and be readable by the user running
  169. salt-ssh.
  170. At last you can create ``~/.salt/Saltfile`` and ``salt-ssh``
  171. will automatically load it by default.
  172. Advanced options with salt-ssh
  173. ==============================
  174. Salt's ability to allow users to have custom grains and custom modules
  175. is also applicable to using salt-ssh. This is done through first packing
  176. the custom grains into the thin tarball before it is deployed on the system.
  177. For this to happen, the ``config`` file must be explicit enough to indicate
  178. where the custom grains are located on the machine like so:
  179. .. code-block:: yaml
  180. file_client: local
  181. file_roots:
  182. base:
  183. - /home/user/.salt
  184. - /home/user/.salt/_states
  185. - /home/user/.salt/_grains
  186. module_dirs:
  187. - /home/user/.salt
  188. pillar_roots:
  189. base:
  190. - /home/user/.salt/_pillar
  191. root_dir: /tmp/.salt-root
  192. It's better to be explicit rather than implicit in this situation. This will
  193. allow urls all under `salt://` to be resolved such as `salt://_grains/custom_grain.py`.
  194. One can confirm this action by executing a properly setup salt-ssh minion with
  195. `salt-ssh minion grains.items`. During this process, a `saltutil.sync_all` is
  196. ran to discover the thin tarball and then consumed. Output similar to this
  197. indicates a successful sync with custom grains.
  198. .. code-block:: yaml
  199. local:
  200. ----------
  201. ...
  202. executors:
  203. grains:
  204. - grains.custom_grain
  205. log_handlers:
  206. ...
  207. This is especially important when using a custom `file_roots` that differ from
  208. `/etc/salt/`.
  209. .. note::
  210. Please see https://docs.saltstack.com/en/latest/topics/grains/ for more
  211. information on grains and custom grains.
  212. Debugging salt-ssh
  213. ==================
  214. One common approach for debugging ``salt-ssh`` is to simply use the tarball that salt
  215. ships to the remote machine and call ``salt-call`` directly.
  216. To determine the location of ``salt-call``, simply run ``salt-ssh`` with the ``-ltrace``
  217. flag and look for a line containing the string, ``SALT_ARGV``. This contains the ``salt-call``
  218. command that ``salt-ssh`` attempted to execute.
  219. It is recommended that one modify this command a bit by removing the ``-l quiet``,
  220. ``--metadata`` and ``--output json`` to get a better idea of what's going on the target system.
  221. .. toctree::
  222. roster
  223. ssh_ext_alternatives
  224. Different Python Versions
  225. =========================
  226. The 3001 release removed python 2 support in Salt. Even though this python 2 support
  227. is being dropped we have provided multiple ways to work around this with Salt-SSH. You
  228. can use the following options:
  229. * :ref:`ssh_pre_flight <ssh_pre_flight>`
  230. * Using the Salt-SSH raw shell calls to install Python3.
  231. * Use an older version of Salt on the target host that still supports Python 2 using the feature :ref:`SSH ext alternatives <ssh-ext-alternatives>`