index.rst 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. .. _targeting:
  2. =================
  3. Targeting Minions
  4. =================
  5. Targeting minions is specifying which minions should run a command or execute a
  6. state by matching against hostnames, or system information, or defined groups,
  7. or even combinations thereof.
  8. For example the command ``salt web1 apache.signal restart`` to restart the
  9. Apache httpd server specifies the machine ``web1`` as the target and the
  10. command will only be run on that one minion.
  11. Similarly when using States, the following :term:`top file` specifies that only
  12. the ``web1`` minion should execute the contents of ``webserver.sls``:
  13. .. code-block:: yaml
  14. base:
  15. 'web1':
  16. - webserver
  17. The simple target specifications, glob, regex, and list will cover many use
  18. cases, and for some will cover all use cases, but more powerful options exist.
  19. Targeting with Grains
  20. =====================
  21. The Grains interface was built into Salt to allow minions to be targeted by
  22. system properties. So minions running on a particular operating system can
  23. be called to execute a function, or a specific kernel.
  24. Calling via a grain is done by passing the -G option to salt, specifying
  25. a grain and a glob expression to match the value of the grain. The syntax for
  26. the target is the grain key followed by a glob expression: "os:Arch*".
  27. .. code-block:: bash
  28. salt -G 'os:Fedora' test.version
  29. Will return True from all of the minions running Fedora.
  30. To discover what grains are available and what the values are, execute the
  31. grains.item salt function:
  32. .. code-block:: bash
  33. salt '*' grains.items
  34. More info on using targeting with grains can be found :ref:`here
  35. <targeting-grains>`.
  36. Compound Targeting
  37. ==================
  38. .. versionadded:: 0.9.5
  39. Multiple target interfaces can be used in conjunction to determine the command
  40. targets. These targets can then be combined using ``and`` or ``or`` statements.
  41. This is well defined with an example:
  42. .. code-block:: bash
  43. salt -C 'G@os:Debian and webser* or E@db.*' test.version
  44. In this example any minion who's id starts with ``webser`` and is running
  45. Debian, or any minion who's id starts with db will be matched.
  46. The type of matcher defaults to glob, but can be specified with the
  47. corresponding letter followed by the ``@`` symbol. In the above example a grain
  48. is used with ``G@`` as well as a regular expression with ``E@``. The
  49. ``webser*`` target does not need to be prefaced with a target type specifier
  50. because it is a glob.
  51. More info on using compound targeting can be found :ref:`here
  52. <targeting-compound>`.
  53. Node Group Targeting
  54. ====================
  55. .. versionadded:: 0.9.5
  56. For certain cases, it can be convenient to have a predefined group of minions
  57. on which to execute commands. This can be accomplished using what are called
  58. :ref:`nodegroups <targeting-nodegroups>`. Nodegroups allow for predefined
  59. compound targets to be declared in the master configuration file, as a sort of
  60. shorthand for having to type out complicated compound expressions.
  61. .. code-block:: yaml
  62. nodegroups:
  63. group1: 'L@foo.domain.com,bar.domain.com,baz.domain.com and bl*.domain.com'
  64. group2: 'G@os:Debian and foo.domain.com'
  65. group3: 'G@os:Debian and N@group1'
  66. Advanced Targeting Methods
  67. ==========================
  68. There are many ways to target individual minions or groups of minions in Salt:
  69. .. toctree::
  70. :maxdepth: 2
  71. globbing
  72. grains
  73. pillar
  74. ipcidr
  75. compound
  76. nodegroups
  77. batch
  78. range
  79. Loadable Matchers
  80. =================
  81. .. versionadded:: 2019.2.0
  82. Internally targeting is implemented with chunks of code called Matchers. As of
  83. the 2019.2.0 release, matchers can be loaded dynamically. Currently new matchers
  84. cannot be created, but existing matchers can have their functionality altered or
  85. extended. For more information on Matchers see
  86. .. toctree::
  87. :maxdepth: 2
  88. Loadable Matchers <../matchers/index.rst>