index.rst 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. .. _grains:
  2. ======
  3. Grains
  4. ======
  5. Salt comes with an interface to derive information about the underlying system.
  6. This is called the grains interface, because it presents salt with grains of
  7. information. Grains are collected for the operating system, domain name,
  8. IP address, kernel, OS type, memory, and many other system properties.
  9. The grains interface is made available to Salt modules and components so that
  10. the right salt minion commands are automatically available on the right
  11. systems.
  12. Grain data is relatively static, though if system information changes
  13. (for example, if network settings are changed), or if a new value is assigned
  14. to a custom grain, grain data is refreshed.
  15. .. note::
  16. Grains resolve to lowercase letters. For example, ``FOO``, and ``foo``
  17. target the same grain.
  18. Listing Grains
  19. ==============
  20. Available grains can be listed by using the 'grains.ls' module:
  21. .. code-block:: bash
  22. salt '*' grains.ls
  23. Grains data can be listed by using the 'grains.items' module:
  24. .. code-block:: bash
  25. salt '*' grains.items
  26. .. _static-custom-grains:
  27. Grains in the Minion Config
  28. ===========================
  29. Grains can also be statically assigned within the minion configuration file.
  30. Just add the option :conf_minion:`grains` and pass options to it:
  31. .. code-block:: yaml
  32. grains:
  33. roles:
  34. - webserver
  35. - memcache
  36. deployment: datacenter4
  37. cabinet: 13
  38. cab_u: 14-15
  39. Then status data specific to your servers can be retrieved via Salt, or used
  40. inside of the State system for matching. It also makes targeting, in the case
  41. of the example above, simply based on specific data about your deployment.
  42. Grains in /etc/salt/grains
  43. ==========================
  44. If you do not want to place your custom static grains in the minion config
  45. file, you can also put them in ``/etc/salt/grains`` on the minion. They are configured in the
  46. same way as in the above example, only without a top-level ``grains:`` key:
  47. .. code-block:: yaml
  48. roles:
  49. - webserver
  50. - memcache
  51. deployment: datacenter4
  52. cabinet: 13
  53. cab_u: 14-15
  54. .. note::
  55. Grains in ``/etc/salt/grains`` are ignored if you specify the same grains in the minion config.
  56. .. note::
  57. Grains are static, and since they are not often changed, they will need a grains refresh when they are updated. You can do this by calling: ``salt minion saltutil.refresh_modules``
  58. .. note::
  59. You can equally configure static grains for Proxy Minions.
  60. As multiple Proxy Minion processes can run on the same machine, you need
  61. to index the files using the Minion ID, under ``/etc/salt/proxy.d/<minion ID>/grains``.
  62. For example, the grains for the Proxy Minion ``router1`` can be defined
  63. under ``/etc/salt/proxy.d/router1/grains``, while the grains for the
  64. Proxy Minion ``switch7`` can be put in ``/etc/salt/proxy.d/switch7/grains``.
  65. Matching Grains in the Top File
  66. ===============================
  67. With correctly configured grains on the Minion, the :term:`top file` used in
  68. Pillar or during Highstate can be made very efficient. For example, consider
  69. the following configuration:
  70. .. code-block:: yaml
  71. 'roles:webserver':
  72. - match: grain
  73. - state0
  74. 'roles:memcache':
  75. - match: grain
  76. - state1
  77. - state2
  78. For this example to work, you would need to have defined the grain
  79. ``role`` for the minions you wish to match.
  80. .. _writing-grains:
  81. Writing Grains
  82. ==============
  83. The grains are derived by executing all of the "public" functions (i.e. those
  84. which do not begin with an underscore) found in the modules located in the
  85. Salt's core grains code, followed by those in any custom grains modules. The
  86. functions in a grains module must return a :ref:`Python dictionary
  87. <python:typesmapping>`, where the dictionary keys are the names of grains, and
  88. each key's value is that value for that grain.
  89. Custom grains modules should be placed in a subdirectory named ``_grains``
  90. located under the :conf_master:`file_roots` specified by the master config
  91. file. The default path would be ``/srv/salt/_grains``. Custom grains modules
  92. will be distributed to the minions when :mod:`state.highstate
  93. <salt.modules.state.highstate>` is run, or by executing the
  94. :mod:`saltutil.sync_grains <salt.modules.saltutil.sync_grains>` or
  95. :mod:`saltutil.sync_all <salt.modules.saltutil.sync_all>` functions.
  96. Grains modules are easy to write, and (as noted above) only need to return a
  97. dictionary. For example:
  98. .. code-block:: python
  99. def yourfunction():
  100. # initialize a grains dictionary
  101. grains = {}
  102. # Some code for logic that sets grains like
  103. grains['yourcustomgrain'] = True
  104. grains['anothergrain'] = 'somevalue'
  105. return grains
  106. The name of the function does not matter and will not factor into the grains
  107. data at all; only the keys/values returned become part of the grains.
  108. When to Use a Custom Grain
  109. --------------------------
  110. Before adding new grains, consider what the data is and remember that grains
  111. should (for the most part) be static data.
  112. If the data is something that is likely to change, consider using :ref:`Pillar
  113. <pillar>` or an execution module instead. If it's a simple set of
  114. key/value pairs, pillar is a good match. If compiling the information requires
  115. that system commands be run, then putting this information in an execution
  116. module is likely a better idea.
  117. Good candidates for grains are data that is useful for targeting minions in the
  118. :ref:`top file <states-top>` or the Salt CLI. The name and data structure of
  119. the grain should be designed to support many platforms, operating systems or
  120. applications. Also, keep in mind that Jinja templating in Salt supports
  121. referencing pillar data as well as invoking functions from execution modules,
  122. so there's no need to place information in grains to make it available to Jinja
  123. templates. For example:
  124. .. code-block:: text
  125. ...
  126. ...
  127. {{ salt['module.function_name']('argument_1', 'argument_2') }}
  128. {{ pillar['my_pillar_key'] }}
  129. ...
  130. ...
  131. .. warning::
  132. Custom grains will not be available in the top file until after the first
  133. :ref:`highstate <running-highstate>`. To make custom grains available on a
  134. minion's first highstate, it is recommended to use :ref:`this example
  135. <minion-start-reactor>` to ensure that the custom grains are synced when
  136. the minion starts.
  137. Loading Custom Grains
  138. ---------------------
  139. If you have multiple functions specifying grains that are called from a ``main``
  140. function, be sure to prepend grain function names with an underscore. This prevents
  141. Salt from including the loaded grains from the grain functions in the final
  142. grain data structure. For example, consider this custom grain file:
  143. .. code-block:: python
  144. #!/usr/bin/env python
  145. def _my_custom_grain():
  146. my_grain = {'foo': 'bar', 'hello': 'world'}
  147. return my_grain
  148. def main():
  149. # initialize a grains dictionary
  150. grains = {}
  151. grains['my_grains'] = _my_custom_grain()
  152. return grains
  153. The output of this example renders like so:
  154. .. code-block:: bash
  155. # salt-call --local grains.items
  156. local:
  157. ----------
  158. <Snipped for brevity>
  159. my_grains:
  160. ----------
  161. foo:
  162. bar
  163. hello:
  164. world
  165. However, if you don't prepend the ``my_custom_grain`` function with an underscore,
  166. the function will be rendered twice by Salt in the items output: once for the
  167. ``my_custom_grain`` call itself, and again when it is called in the ``main``
  168. function:
  169. .. code-block:: bash
  170. # salt-call --local grains.items
  171. local:
  172. ----------
  173. <Snipped for brevity>
  174. foo:
  175. bar
  176. <Snipped for brevity>
  177. hello:
  178. world
  179. <Snipped for brevity>
  180. my_grains:
  181. ----------
  182. foo:
  183. bar
  184. hello:
  185. world
  186. Precedence
  187. ==========
  188. Core grains can be overridden by custom grains. As there are several ways of
  189. defining custom grains, there is an order of precedence which should be kept in
  190. mind when defining them. The order of evaluation is as follows:
  191. 1. Core grains.
  192. 2. Custom grains in ``/etc/salt/grains``.
  193. 3. Custom grains in ``/etc/salt/minion``.
  194. 4. Custom grain modules in ``_grains`` directory, synced to minions.
  195. Each successive evaluation overrides the previous ones, so any grains defined
  196. by custom grains modules synced to minions that have the same name as a core
  197. grain will override that core grain. Similarly, grains from
  198. ``/etc/salt/minion`` override both core grains and custom grain modules, and
  199. grains in ``_grains`` will override *any* grains of the same name.
  200. Examples of Grains
  201. ==================
  202. The core module in the grains package is where the main grains are loaded by
  203. the Salt minion and provides the principal example of how to write grains:
  204. :blob:`salt/grains/core.py`
  205. Syncing Grains
  206. ==============
  207. Syncing grains can be done a number of ways, they are automatically synced when
  208. :mod:`state.highstate <salt.modules.state.highstate>` is called, or (as noted
  209. above) the grains can be manually synced and reloaded by calling the
  210. :mod:`saltutil.sync_grains <salt.modules.saltutil.sync_grains>` or
  211. :mod:`saltutil.sync_all <salt.modules.saltutil.sync_all>` functions.
  212. .. note::
  213. When the :conf_minion:`grains_cache` is set to False, the grains dictionary is built
  214. and stored in memory on the minion. Every time the minion restarts or
  215. ``saltutil.refresh_grains`` is run, the grain dictionary is rebuilt from scratch.