saltstack的探索-salt指定目标和分组
一、探讨一下,如何针对指定的minion id来执行先了解官网文档的targeting这一节的内容:TargetingSalt allows for minions to be targeted based on a wide range of criteria. The default targeting system uses globular expressions to match minions, hence if there are minions named larry1, larry2, curly1, and curly2, a glob of larry* will match larry1 and larry2, and a glob of *1 will match larry1 and curly1.Many other targeting systems can be used other than globs, these systems include:Regular ExpressionsTarget using PCRE-compliant regular expressionsGrainsTarget based on grains data: Targeting with GrainsPillarTarget based on pillar data: Targeting with PillarIPTarget based on IP address/subnet/rangeCompoundCreate logic to target based on multiple targets: Targeting with CompoundNodegroupTarget with nodegroups: Targeting with Nodegroup二、通配符和正则5.1 Matching the minion id5.1.1 GlobbingThe default matching that Salt utilizes is shell-style globbing around the minion id. This also works for states in the top file.Note: You must wrap salt calls that use globbing in single-quotes to prevent the shell from expanding the globs before Salt is invoked.Match all minions:salt ’*’ test.pingMatch all minions in the example.net domain or any of the example domains:salt ’*.example.net’ test.pingsalt ’*.example.*’ test.pingMatch all the webN minions in the example.net domain (web1.example.net, web2.example.net . . . webN.example.net):salt ’web?.example.net’ test.pingMatch the web1 through web5 minions:salt ’web[1-5]’ test.pingMatch the web-x, web-y, and web-z minions:salt ’web-[x-z]’ test.ping5.1.2 Regular ExpressionsMinions can be matched using Perl-compatible regular expressions (which is globbing on steroids and a ton of caf-feine).Match both web1-prod and web1-devel minions:salt -E ’web1-(prod|devel)’ test.pingWhen using regular expressions in a State’s top file, you must specify the matcher as the first option. The following example executes the contents of webserver.sls on the above-mentioned minions.base: ’web1-(prod|devel)’: - match: pcre - webserver5.1.3 ListsAt the most basic level, you can specify a flat list of minion IDs:salt -L ’web1,web2,web3’ test.ping三、Grains我的理解:通过grains能得到系统底层的一些基本信息。是静态的。可以在master和minion的配置中写入key:value,但要注意优先级等区别。还是翻官网文档先:5.2 GrainsSalt comes with an interface to derive information about the underlying system. This is called the grains interface, because it presents salt with grains of information.Grains Static bits of information that a minion collects about the system when the minion first starts.The grains interface is made available to Salt modules and components so that the right salt minion commands are automatically available on the right systems.It is important to remember that grains are bits of information loaded when the salt minion starts, so this informationis static. This means that the information in grains is unchanging, therefore the nature of the data is static. So grainsinformation are things like the running kernel, or the operating system.Match all CentOS minions:salt -G ’os:CentOS’ test.pingMatch all minions with 64-bit CPUs and return number of available cores:salt -G ’cpuarch:x86_64’ grains.item num_cpusAdditionally, globs can be used in grain matches, and grains that are nested in a dictionary can be matched by adding a colon for each level that is traversed. For example, the following will match hosts that have a grain called ec2_tags,which itself is a dict with a key named environment, which has a value that contains the word production:salt -G ’ec2_tags:environment:*production*’5.2.1 Listing GrainsAvailable grains can be listed by using the ‘grains.ls’ module:salt ’*’ grains.lsGrains data can be listed by using the ‘grains.items’ module:salt ’*’ grains.items5.2.2 Grains in the Minion ConfigGrains can also be statically assigned within the minion configuration file. Just add the option grains and pass options to it:grains: roles: - webserver - memcache deployment: datacenter4 cabinet: 13 cab_u: 14-15 Then status data specific to your servers can be retrieved via Salt, or used inside of the State system for matching. It also makes targeting, in the case of the example above, simply based on specific data about your deployment.5.2.3 Grains in /etc/salt/grainsIf you do not want to place your custom static grains in the minion config file, you can also put them in /etc/salt/grains. They are configured in the same way as in the above example, only without a top-level grains: key:roles: - webserver - memcachedeployment: datacenter4cabinet: 13cab_u: 14-15Precedence of Custom Static GrainsBe careful when defining grains both in /etc/salt/grains and within the minion config file. If a grain is defined in both places, the value in the minion config file takes precedence, and will always be used over its counterpart in /etc/salt/grains.5.2.4 Writing GrainsGrains are easy to write. The grains interface is derived by executing all of the “public” functions found in the modules located in the grains package or the custom grains directory. The functions in the modules of the grains must return a Python dict, where the keys in the dict are the names of the grains and the values are the values.Custom grains should be placed in a _grains directory located under the file_roots specified by the mas-ter config file. They will be distributed to the minions when state.highstate is run, or by executing the saltutil.sync_grains or saltutil.sync_all functions.Before adding a grain to Salt, consider what the grain is and remember that grains need to be static data. If the data is something that is likely to change, consider using Pillar instead.Examples of GrainsThe core module in the grains package is where the main grains are loaded by the Salt minion and provides the principal example of how to write grains:https://github.com/saltstack/salt/blob/develop/salt/grains/core.pySyncing GrainsSyncing grains can be done a number of ways, they are automatically synced when state.highstate is called, or the grains can be synced and reloaded by calling the saltutil.sync_grains or saltutil.sync_all functions.四、Nodegroup在master的配置文件/etc/salt/master 中:有如下一段:##### Node Groups ################################################ Node groups allow for logical groupings of minion nodes. A group consists of a group# name and a compound target.#nodegroups:# group1: 'L@foo.domain.com,bar.domain.com,baz.domain.com and bl*.domain.com'# group2: 'G@os:Debian and foo.domain.com'咱们继续看文档:5.3 Node groupsNode group A predefined group of minions declared in the master configuration file nodegroups setting as a compound target.Nodegroups are declared using a compound target specification. The compound target documentation can be found here:Compound Matchers(参考下面一段)For example, in the master config file nodegroups setting:nodegroups:group1: ’L@foo.domain.com,bar.domain.com,baz.domain.com or bl*.domain.com’group2: ’G@os:Debian and foo.domain.com’Specify a nodegroup via the -N option at the command-line:salt -N group1 test.pingSpecify a nodegroup with - match: nodegroup in a top file:base: group1: - match: nodegroup - webserver实例:# vim /etc/salt/masternodegroups: cabinet01: 'E@test2(1[1-9]|3[1-2]).company.com' cabinet02: 'E@test(12|14[0-6]|18[3-5]).company.com' cabinet03: 'E@test10[1-5].company.com'# salt -N cabinet02 test.pingtest144.company.com: Truetest183.company.com: Truetest185.company.com: Truetest146.company.com: Truetest140.company.com: Truetest143.company.com: Truetest141.company.com: Truetest145.company.com: Truetest142.company.com: Truetest12.company.com: True 五、混合匹配5.4 Compound matchersCompound matcher A combination of many target definitions that can be combined with boolean operators.Compound matchers allow very granular minion targeting using any of the previously discussed matchers. The default matcher is a glob, as usual. For matching via anything other than glob, preface it with the letter denoting the match type. The currently implemented “letters” are:Letter Meaning ExampleG Grains glob match G@os:UbuntuE PCRE Minion id match E@web\d+\.(dev|qa|prod)\.locP Grains PCRE match P@os:(RedHat|Fedora|CentOS)L List of minions L@minion1.example.com,minion3.domain.com or bl*.domain.comI Pillar glob match I@pdata:foobarS Subnet/IP addr match S@192.168.1.0/24 or S@192.168.1.100R Range cluster match R@%foo.barD Minion Data match D@key:valueMatchers can be joined using boolean and, or, and not operators.For example, the following command matches all minions that have a hostname that begins with “webserv” and that are running Debian or it matches any minions that have a hostname that matches the regular expression web-dc1-srv.* :salt -C ’webserv* and G@os:Debian or E@web-dc1-srv.*’ test.pingThat same example expressed in a top file looks like the following:base: ’webserv* and G@os:Debian or E@web-dc1-srv.*’: - match: compound - webserver Note that you cannot have a leading not in a command. Instead you must do something like the following:salt -C ’* and not G@kernel:Darwin’ test.ping实例:[root@test200 ~]# salt -C 'E@test(12|14[0-6]|18[3-5]).company.com or dev0[1-2].office.com' test.ping test144.company.com: Truetest183.company.com: Truetest185.company.com: Truetest146.company.com: Truetest140.company.com: Truetest143.company.com: Truetest141.company.com: Truetest145.company.com: Truetest142.company.com: Truetest12.company.com: Truedev01.office.com: Truedev02.office.com: True