1
0

index.rst 9.8 KB

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