index.rst 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. .. _salt-mine:
  2. .. index:: ! Mine, Salt Mine
  3. =============
  4. The Salt Mine
  5. =============
  6. The Salt Mine is used to collect arbitrary data from Minions and store it on
  7. the Master. This data is then made available to all Minions via the
  8. :py:mod:`salt.modules.mine` module.
  9. Mine data is gathered on the Minion and sent back to the Master where only the
  10. most recent data is maintained (if long term data is required use returners or
  11. the external job cache).
  12. Mine vs Grains
  13. ==============
  14. Mine data is designed to be much more up-to-date than grain data. Grains are
  15. refreshed on a very limited basis and are largely static data. Mines are
  16. designed to replace slow peer publishing calls when Minions need data from
  17. other Minions. Rather than having a Minion reach out to all the other Minions
  18. for a piece of data, the Salt Mine, running on the Master, can collect it from
  19. all the Minions every :ref:`mine_interval`, resulting in
  20. almost fresh data at any given time, with much less overhead.
  21. Mine Functions
  22. ==============
  23. To enable the Salt Mine the ``mine_functions`` option needs to be applied to a
  24. Minion. This option can be applied via the Minion's configuration file, or the
  25. Minion's Pillar. The ``mine_functions`` option dictates what functions are
  26. being executed and allows for arguments to be passed in. The list of
  27. functions are available in the :py:mod:`salt.module`. If no arguments
  28. are passed, an empty list must be added like in the ``test.ping`` function in
  29. the example below:
  30. .. code-block:: yaml
  31. mine_functions:
  32. test.ping: []
  33. network.ip_addrs:
  34. interface: eth0
  35. cidr: '10.0.0.0/8'
  36. In the example above :py:mod:`salt.modules.network.ip_addrs` has additional
  37. filters to help narrow down the results. In the above example IP addresses
  38. are only returned if they are on a eth0 interface and in the 10.0.0.0/8 IP
  39. range.
  40. Mine Functions Aliases
  41. ----------------------
  42. Function aliases can be used to provide friendly names, usage intentions or to
  43. allow multiple calls of the same function with different arguments. There is a
  44. different syntax for passing positional and key-value arguments. Mixing
  45. positional and key-value arguments is not supported.
  46. .. versionadded:: 2014.7.0
  47. .. code-block:: yaml
  48. mine_functions:
  49. network.ip_addrs: [eth0]
  50. networkplus.internal_ip_addrs: []
  51. internal_ip_addrs:
  52. mine_function: network.ip_addrs
  53. cidr: 192.168.0.0/16
  54. ip_list:
  55. - mine_function: grains.get
  56. - ip_interfaces
  57. .. _mine_interval:
  58. Mine Interval
  59. =============
  60. The Salt Mine functions are executed when the Minion starts and at a given
  61. interval by the scheduler. The default interval is every 60 minutes and can
  62. be adjusted for the Minion via the ``mine_interval`` option in the minion
  63. config:
  64. .. code-block:: yaml
  65. mine_interval: 60
  66. Mine in Salt-SSH
  67. ================
  68. As of the 2015.5.0 release of salt, salt-ssh supports ``mine.get``.
  69. Because the Minions cannot provide their own ``mine_functions`` configuration,
  70. we retrieve the args for specified mine functions in one of three places,
  71. searched in the following order:
  72. 1. Roster data
  73. 2. Pillar
  74. 3. Master config
  75. The ``mine_functions`` are formatted exactly the same as in normal salt, just
  76. stored in a different location. Here is an example of a flat roster containing
  77. ``mine_functions``:
  78. .. code-block:: yaml
  79. test:
  80. host: 104.237.131.248
  81. user: root
  82. mine_functions:
  83. cmd.run: ['echo "hello!"']
  84. network.ip_addrs:
  85. interface: eth0
  86. .. note::
  87. Because of the differences in the architecture of salt-ssh, ``mine.get``
  88. calls are somewhat inefficient. Salt must make a new salt-ssh call to each
  89. of the Minions in question to retrieve the requested data, much like a
  90. publish call. However, unlike publish, it must run the requested function
  91. as a wrapper function, so we can retrieve the function args from the pillar
  92. of the Minion in question. This results in a non-trivial delay in
  93. retrieving the requested data.
  94. Minions Targeting with Mine
  95. ===========================
  96. The ``mine.get`` function supports various methods of :ref:`Minions targeting
  97. <targeting>` to fetch Mine data from particular hosts, such as glob or regular
  98. expression matching on Minion id (name), grains, pillars and :ref:`compound
  99. matches <targeting-compound>`. See the :py:mod:`salt.modules.mine` module
  100. documentation for the reference.
  101. .. note::
  102. Pillar data needs to be cached on Master for pillar targeting to work with
  103. Mine. Read the note in :ref:`relevant section <targeting-pillar>`.
  104. Example
  105. =======
  106. One way to use data from Salt Mine is in a State. The values can be retrieved
  107. via Jinja and used in the SLS file. The following example is a partial HAProxy
  108. configuration file and pulls IP addresses from all Minions with the "web" grain
  109. to add them to the pool of load balanced servers.
  110. :file:`/srv/pillar/top.sls`:
  111. .. code-block:: yaml
  112. base:
  113. 'G@roles:web':
  114. - web
  115. :file:`/srv/pillar/web.sls`:
  116. .. code-block:: yaml
  117. mine_functions:
  118. network.ip_addrs: [eth0]
  119. Then trigger the minions to refresh their pillar data by running:
  120. .. code-block:: bash
  121. salt '*' saltutil.refresh_pillar
  122. Verify that the results are showing up in the pillar on the minions by
  123. executing the following and checking for ``network.ip_addrs`` in the output:
  124. .. code-block:: bash
  125. salt '*' pillar.items
  126. Which should show that the function is present on the minion, but not include
  127. the output:
  128. .. code-block:: shell
  129. minion1.example.com:
  130. ----------
  131. mine_functions:
  132. ----------
  133. network.ip_addrs:
  134. - eth0
  135. Mine data is typically only updated on the master every 60 minutes, this can
  136. be modified by setting:
  137. :file:`/etc/salt/minion.d/mine.conf`:
  138. .. code-block:: yaml
  139. mine_interval: 5
  140. To force the mine data to update immediately run:
  141. .. code-block:: bash
  142. salt '*' mine.update
  143. Setup the :py:mod:`salt.states.file.managed` state in
  144. :file:`/srv/salt/haproxy.sls`:
  145. .. code-block:: yaml
  146. haproxy_config:
  147. file.managed:
  148. - name: /etc/haproxy/config
  149. - source: salt://haproxy_config
  150. - template: jinja
  151. Create the Jinja template in :file:`/srv/salt/haproxy_config`:
  152. .. code-block:: yaml
  153. <...file contents snipped...>
  154. {% for server, addrs in salt['mine.get']('roles:web', 'network.ip_addrs', tgt_type='grain') | dictsort() %}
  155. server {{ server }} {{ addrs[0] }}:80 check
  156. {% endfor %}
  157. <...file contents snipped...>
  158. In the above example, ``server`` will be expanded to the ``minion_id``.
  159. .. note::
  160. The expr_form argument will be renamed to ``tgt_type`` in the 2017.7.0
  161. release of Salt.