Wednesday, 27 September 2017

SaltStack Targeting Methods - Perl Compatible Regular Expressions (PCRE) Matching

We have been discussing on targeting minions using minion IDs on Salt master. In the last article, we targetted minions usling glob matching method. In this article, we will learn another method to target minions using minion IDs - Perl compatible regular expression / PCRE matching. This method is very useful when we need to target minions with complex matches, against their minion IDs. PCRE matching makes use of Python re module to parse the regular expressions. It requires a basic understanding of regular expressions to write PCRE strings, so I recommend you to review the documents on regular expressions provided on Python website.


PCRE matching works differently than glob matching, as PCRE match can accept partial matches. In order to indicate Salt that we are using PCRE string to target minions, we need to use an extra option -E or --pcre with salt command.

1. . matches zero or more characters


In our article on glob matching, we observed that * matches anything. Similarly, to match anything (any character appearing zero or more times) using PCRE, we need to use .*.

$ salt -E '.*' test.ping
redhat-rack2:
    True
centos-rack1:
    True


$ salt -E '.*rack1' test.ping
centos-rack1:
    True


$ salt -E 'centos-.*' test.ping
centos-rack1:
    True

2. ^ matches beginning of the line


In order to target minions with IDs starting with centos, we would use ^centos as the target string. This will match centos-rack1 and centossrv, but not some-centos-srv or rack1-centos.

$ salt -E '^centos' test.ping
centos-rack1:
    True 

3. $ matches end of the line


Consider that, all the servers installed in the first rack in our data center are having minion IDs ending with rack1, e.g. centos-rack1,
redhat-rack1, websrv-rack1, etc. When targeting all these servers, we need to use a PCRE which should match minions whose IDs end with the string rack1. In this case, $ comes to the rescue, which matches end of the line, but don't forget about .*.

Note that, .*rack1$ will match all of these minion IDs, but not rack1-web or fedora-rack10.

$ salt -E '.*rack1$' test.ping
centos-rack1:
    True

4. ? matches zero or one occurrences of target string


Questions marks are used for optional matches, to match a string that may or may not be present.

# This matches 'centos-rack1' or 'rack1'
$ salt -E '(centos-)?rack1' test.ping
centos-rack1:
    True

# This matches 'centos-rack' followed by an optional literal
# 'centos-rack1', 'centos-rackA', 'centos-rack' will match, 'centos-rack10' won't
salt -E 'centos-rack.?' test.ping
centos-rack1:
    True

5. | is used for multiple matches


If we were to match centos or redhat followed by -rack1, we can use | operator. Thus, (centos|redhat) will try to match either centos or redhat.

$ salt -E '(centos|redhat)-rack' test.ping
centos-rack1:
    True
redhat-rack2:
    True
 
$ salt -E '(centos|ubuntu)-rack' test.ping
centos-rack1:
    True

That's all for this article. The scope of PCRE being too large, I could not include everything in this tutorial, but the intention was only to explain their use in a salt command. Do remember to learn more about PCRE from Python documentation and stay tuned for more tutorials.

0 comments:

Post a Comment