1
0

index.rst 9.8 KB

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