index.rst 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. .. _file-server:
  2. ================
  3. Salt File Server
  4. ================
  5. Salt comes with a simple file server suitable for distributing files to the
  6. Salt minions. The file server is a stateless ZeroMQ server that is built into
  7. the Salt master.
  8. The main intent of the Salt file server is to present files for use in the
  9. Salt state system. With this said, the Salt file server can be used for any
  10. general file transfer from the master to the minions.
  11. .. toctree::
  12. :glob:
  13. *
  14. The cp Module
  15. -------------
  16. The cp module is the home of minion side file server operations. The cp module
  17. is used by the Salt state system, salt-cp, and can be used to distribute files
  18. presented by the Salt file server.
  19. Escaping Special Characters
  20. ```````````````````````````
  21. The ``salt://`` url format can potentially contain a query string, for example
  22. ``salt://dir/file.txt?saltenv=base``. You can prevent the fileclient/fileserver from
  23. interpreting ``?`` as the initial token of a query string by referencing the file
  24. with ``salt://|`` rather than ``salt://``.
  25. .. code-block:: yaml
  26. /etc/marathon/conf/?checkpoint:
  27. file.managed:
  28. - source: salt://|hw/config/?checkpoint
  29. - makedirs: True
  30. Environments
  31. ````````````
  32. Since the file server is made to work with the Salt state system, it supports
  33. environments. The environments are defined in the master config file and
  34. when referencing an environment the file specified will be based on the root
  35. directory of the environment.
  36. get_file
  37. ````````
  38. The cp.get_file function can be used on the minion to download a file from
  39. the master, the syntax looks like this:
  40. .. code-block:: bash
  41. # salt '*' cp.get_file salt://vimrc /etc/vimrc
  42. This will instruct all Salt minions to download the vimrc file and copy it
  43. to /etc/vimrc
  44. Template rendering can be enabled on both the source and destination file names
  45. like so:
  46. .. code-block:: bash
  47. # salt '*' cp.get_file "salt://{{grains.os}}/vimrc" /etc/vimrc template=jinja
  48. This example would instruct all Salt minions to download the vimrc from a
  49. directory with the same name as their OS grain and copy it to /etc/vimrc
  50. For larger files, the cp.get_file module also supports gzip compression.
  51. Because gzip is CPU-intensive, this should only be used in
  52. scenarios where the compression ratio is very high (e.g. pretty-printed JSON
  53. or YAML files).
  54. To use compression, use the ``gzip`` named argument. Valid values are integers
  55. from 1 to 9, where 1 is the lightest compression and 9 the heaviest. In other
  56. words, 1 uses the least CPU on the master (and minion), while 9 uses the most.
  57. .. code-block:: bash
  58. # salt '*' cp.get_file salt://vimrc /etc/vimrc gzip=5
  59. Finally, note that by default cp.get_file does *not* create new destination
  60. directories if they do not exist. To change this, use the ``makedirs``
  61. argument:
  62. .. code-block:: bash
  63. # salt '*' cp.get_file salt://vimrc /etc/vim/vimrc makedirs=True
  64. In this example, /etc/vim/ would be created if it didn't already exist.
  65. get_dir
  66. ```````
  67. The cp.get_dir function can be used on the minion to download an entire
  68. directory from the master. The syntax is very similar to get_file:
  69. .. code-block:: bash
  70. # salt '*' cp.get_dir salt://etc/apache2 /etc
  71. cp.get_dir supports template rendering and gzip compression arguments just like
  72. get_file:
  73. .. code-block:: bash
  74. # salt '*' cp.get_dir salt://etc/{{pillar.webserver}} /etc gzip=5 template=jinja
  75. File Server Client Instance
  76. ---------------------------
  77. A client instance is available which allows for modules and applications to be
  78. written which make use of the Salt file server.
  79. The file server uses the same authentication and encryption used by the rest
  80. of the Salt system for network communication.
  81. fileclient Module
  82. `````````````````
  83. The ``salt/fileclient.py`` module is used to set up the communication from the
  84. minion to the master. When creating a client instance using the fileclient module,
  85. the minion configuration needs to be passed in. When using the fileclient module
  86. from within a minion module the built in ``__opts__`` data can be passed:
  87. .. code-block:: python
  88. import salt.minion
  89. import salt.fileclient
  90. def get_file(path, dest, saltenv="base"):
  91. """
  92. Used to get a single file from the Salt master
  93. CLI Example:
  94. salt '*' cp.get_file salt://vimrc /etc/vimrc
  95. """
  96. # Get the fileclient object
  97. client = salt.fileclient.get_file_client(__opts__)
  98. # Call get_file
  99. return client.get_file(path, dest, False, saltenv)
  100. Creating a fileclient instance outside of a minion module where the ``__opts__``
  101. data is not available, it needs to be generated:
  102. .. code-block:: python
  103. import salt.fileclient
  104. import salt.config
  105. def get_file(path, dest, saltenv="base"):
  106. """
  107. Used to get a single file from the Salt master
  108. """
  109. # Get the configuration data
  110. opts = salt.config.minion_config("/etc/salt/minion")
  111. # Get the fileclient object
  112. client = salt.fileclient.get_file_client(opts)
  113. # Call get_file
  114. return client.get_file(path, dest, False, saltenv)