1
0

index.rst 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. .. _matchers:
  2. ========
  3. Matchers
  4. ========
  5. .. versionadded:: 3000
  6. Matchers are modules that provide Salt's targeting abilities. As of the
  7. 3000 release, matchers can be dynamically loaded. Currently new matchers
  8. cannot be created because the required plumbing for the CLI does not exist yet.
  9. Existing matchers may have their functionality altered or extended.
  10. For details of targeting methods, see the :ref:`Targeting <targeting>` topic.
  11. A matcher module must have a function called ``match()``. This function ends up
  12. becoming a method on the Matcher class. All matcher functions require at least
  13. two arguments, ``self`` (because the function will be turned into a method), and
  14. ``tgt``, which is the actual target string. The grains and pillar matchers also
  15. take a ``delimiter`` argument and should default to ``DEFAULT_TARGET_DELIM``.
  16. Like other Salt loadable modules, modules that override built-in functionality
  17. can be placed in ``file_roots`` in a special directory and then copied to the
  18. minion through the normal sync process. :py:func:`saltutil.sync_all <salt.modules.saltutil.sync_all>`
  19. will transfer all loadable modules, and the 3000 release introduces
  20. :py:func:`saltutil.sync_matchers <salt.modules.saltutil.sync_matchers>`. For matchers, the directory is
  21. ``/srv/salt/_matchers`` (assuming your ``file_roots`` is set to the default
  22. ``/srv/salt``).
  23. As an example, let's modify the ``list`` matcher to have the separator be a
  24. '``/``' instead of the default '``,``'.
  25. .. code-block:: python
  26. from __future__ import absolute_import, print_function, unicode_literals
  27. from salt.ext import six # pylint: disable=3rd-party-module-not-gated
  28. def match(self, tgt):
  29. """
  30. Determines if this host is on the list
  31. """
  32. if isinstance(tgt, six.string_types):
  33. # The stock matcher splits on `,`. Change to `/` below.
  34. tgt = tgt.split("/")
  35. return bool(self.opts["id"] in tgt)
  36. Place this code in a file called ``list_matcher.py`` in ``_matchers`` in your
  37. ``file_roots``. Sync this down to your minions with
  38. :py:func:`saltutil.sync_matchers <salt.modules.saltutil.sync_matchers>`.
  39. Then attempt to match with the following, replacing ``minionX`` with three of your minions.
  40. .. code-block:: shell
  41. salt -L 'minion1/minion2/minion3' test.ping
  42. Three of your minions should respond.
  43. The current supported matchers and associated filenames are
  44. =============== ====================== ===================
  45. Salt CLI Switch Match Type Filename
  46. =============== ====================== ===================
  47. <none> Glob glob_match.py
  48. -C Compound compound_match.py
  49. -E Perl-Compatible pcre_match.py
  50. Regular Expressions
  51. -L List list_match.py
  52. -G Grain grain_match.py
  53. -P Grain Perl-Compatible grain_pcre_match.py
  54. Regular Expressions
  55. -N Nodegroup nodegroup_match.py
  56. -R Range range_match.py
  57. -I Pillar pillar_match.py
  58. -J Pillar Perl-Compatible pillar_pcre.py
  59. Regular Expressions
  60. -S IP-Classless Internet ipcidr_match.py
  61. Domain Routing
  62. =============== ====================== ===================