<code>一、探讨一下,如何針對指定的minion </code><code>id</code><code>來執行</code>
<code>先了解官網文檔的targeting這一節的内容:</code>
<code>Targeting</code>
<code>Salt allows </code><code>for</code> <code>minions to be targeted based on a wide range of criteria. The default targeting system uses globular expressions to match minions, hence </code><code>if</code> <code>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.</code>
<code>Many other targeting systems can be used other than globs, these systems include:</code>
<code>Regular Expressions</code>
<code>Target using PCRE-compliant regular expressions</code>
<code>Grains</code>
<code>Target based on grains data: Targeting with Grains</code>
<code>Pillar</code>
<code>Target based on pillar data: Targeting with Pillar</code>
<code>IP</code>
<code>Target based on IP address</code><code>/subnet/range</code>
<code>Compound</code>
<code>Create logic to target based on multiple targets: Targeting with Compound</code>
<code>Nodegroup</code>
<code>Target with nodegroups: Targeting with Nodegroup</code>
<code>二、通配符和正則</code>
<code>5.1 Matching the minion </code><code>id</code>
<code>5.1.1 Globbing</code>
<code>The default matching that Salt utilizes is shell-style globbing around the minion </code><code>id</code><code>. This also works </code><code>for</code> <code>states </code><code>in</code> <code>the </code><code>top</code> <code>file</code><code>.</code>
<code>Note: You must wrap salt calls that use globbing </code><code>in</code> <code>single-quotes to prevent the shell from expanding the globs before Salt is invoked.</code>
<code>Match all minions:</code>
<code>salt ’*’ </code><code>test</code><code>.</code><code>ping</code>
<code>Match all minions </code><code>in</code> <code>the example.net domain or any of the example domains:</code>
<code>salt ’*.example.net’ </code><code>test</code><code>.</code><code>ping</code>
<code>salt ’*.example.*’ </code><code>test</code><code>.</code><code>ping</code>
<code>Match all the webN minions </code><code>in</code> <code>the example.net domain (web1.example.net, web2.example.net . . . webN.example.net):</code>
<code>salt ’web?.example.net’ </code><code>test</code><code>.</code><code>ping</code>
<code>Match the web1 through web5 minions:</code>
<code>salt ’web[1-5]’ </code><code>test</code><code>.</code><code>ping</code>
<code>Match the web-x, web-y, and web-z minions:</code>
<code>salt ’web-[x-z]’ </code><code>test</code><code>.</code><code>ping</code>
<code>5.1.2 Regular Expressions</code>
<code>Minions can be matched using Perl-compatible regular expressions (</code><code>which</code> <code>is globbing on steroids and a ton of caf-feine).</code>
<code>Match both web1-prod and web1-devel minions:</code>
<code>salt -E ’web1-(prod|devel)’ </code><code>test</code><code>.</code><code>ping</code>
<code>When using regular expressions </code><code>in</code> <code>a State’s </code><code>top</code> <code>file</code><code>, you must specify the matcher as the first option. The following example executes the contents of webserver.sls on the above-mentioned minions.</code>
<code>base:</code>
<code> </code><code>’web1-(prod|devel)’:</code>
<code> </code><code>- match: pcre</code>
<code> </code><code>- webserver</code>
<code>5.1.3 Lists</code>
<code>At the most basic level, you can specify a flat list of minion IDs:</code>
<code>salt -L ’web1,web2,web3’ </code><code>test</code><code>.</code><code>ping</code>
<code>三、Grains</code>
<code>我的了解:通過grains能得到系統底層的一些基本資訊。是靜态的。可以在master和minion的配置中寫入key:value,但要注意優先級等差別。</code>
<code>還是翻官網文檔先:</code>
<code>5.2 Grains</code>
<code>Salt 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.</code>
<code>Grains Static bits of information that a minion collects about the system when the minion first starts.</code>
<code>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.</code>
<code>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 </code><code>in</code> <code>grains is unchanging, therefore the nature of the data is static. So grainsinformation are things like the running kernel, or the operating system.</code>
<code>Match all CentOS minions:</code>
<code>salt -G ’os:CentOS’ </code><code>test</code><code>.</code><code>ping</code>
<code>Match all minions with 64-bit CPUs and </code><code>return</code> <code>number of available cores:</code>
<code>salt -G ’cpuarch:x86_64’ grains.item num_cpus</code>
<code>Additionally, globs can be used </code><code>in</code> <code>grain matches, and grains that are nested </code><code>in</code> <code>a dictionary can be matched by adding a colon </code><code>for</code> <code>each level that is traversed. For example, the following will match hosts that have a grain called ec2_tags,</code><code>which</code> <code>itself is a dict with a key named environment, </code><code>which</code> <code>has a value that contains the word production:</code>
<code>salt -G ’ec2_tags:environment:*production*’</code>
<code>5.2.1 Listing Grains</code>
<code>Available grains can be listed by using the ‘grains.</code><code>ls</code><code>’ module:</code>
<code>salt ’*’ grains.</code><code>ls</code>
<code>Grains data can be listed by using the ‘grains.items’ module:</code>
<code>salt ’*’ grains.items</code>
<code>5.2.2 Grains </code><code>in</code> <code>the Minion Config</code>
<code>Grains can also be statically assigned within the minion configuration </code><code>file</code><code>. Just add the option grains and pass options to it:</code>
<code>grains:</code>
<code> </code><code>roles:</code>
<code> </code><code>- memcache</code>
<code> </code><code>deployment: datacenter4</code>
<code> </code><code>cabinet: 13</code>
<code> </code><code>cab_u: 14-15</code>
<code> </code>
<code>Then status data specific to your servers can be retrieved via Salt, or used inside of the State system </code><code>for</code> <code>matching. It also makes targeting, </code><code>in</code> <code>the </code><code>case</code> <code>of the example above, simply based on specific data about your deployment.</code>
<code>5.2.3 Grains </code><code>in</code> <code>/etc/salt/grains</code>
<code>If you </code><code>do</code> <code>not want to place your custom static grains </code><code>in</code> <code>the minion config </code><code>file</code><code>, you can also put them </code><code>in</code> <code>/etc/salt/grains</code><code>. They are configured </code><code>in</code> <code>the same way as </code><code>in</code> <code>the above example, only without a </code><code>top</code><code>-level grains: key:</code>
<code>roles:</code>
<code> </code><code>- webserver</code>
<code> </code><code>- memcache</code>
<code>deployment: datacenter4</code>
<code>cabinet: 13</code>
<code>cab_u: 14-15</code>
<code>Precedence of Custom Static Grains</code>
<code>Be careful when defining grains both </code><code>in</code> <code>/etc/salt/grains</code> <code>and within the minion config </code><code>file</code><code>. If a grain is defined </code><code>in</code> <code>both places, the value </code><code>in</code> <code>the minion config </code><code>file</code> <code>takes precedence, and will always be used over its counterpart </code><code>in</code> <code>/etc/salt/grains</code><code>.</code>
<code>5.2.4 Writing Grains</code>
<code>Grains are easy to write. The grains interface is derived by executing all of the “public” functions found </code><code>in</code> <code>the modules located </code><code>in</code> <code>the grains package or the custom grains directory. The functions </code><code>in</code> <code>the modules of the grains must </code><code>return</code> <code>a Python dict, where the keys </code><code>in</code> <code>the dict are the names of the grains and the values are the values.</code>
<code>Custom grains should be placed </code><code>in</code> <code>a _grains directory located under the file_roots specified by the mas-ter config </code><code>file</code><code>. They will be distributed to the minions when state.highstate is run, or by executing the </code>
<code>saltutil.sync_grains or saltutil.sync_all functions.</code>
<code>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.</code>
<code>Examples of Grains</code>
<code>The core module </code><code>in</code> <code>the grains package is where the main grains are loaded by the Salt minion and provides the principal example of how to write grains:</code>
<code>https:</code><code>//github</code><code>.com</code><code>/saltstack/salt/blob/develop/salt/grains/core</code><code>.py</code>
<code>Syncing Grains</code>
<code>Syncing grains can be </code><code>done</code> <code>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.</code>
<code>四、Nodegroup</code>
<code>在master的配置檔案</code><code>/etc/salt/master</code> <code>中:</code>
<code>有如下一段:</code>
<code>##### Node Groups #####</code>
<code>##########################################</code>
<code># Node groups allow for logical groupings of minion nodes. A group consists of a group</code>
<code># name and a compound target.</code>
<code>#nodegroups:</code>
<code># group1: '[email protected],bar.domain.com,baz.domain.com and bl*.domain.com'</code>
<code># group2: 'G@os:Debian and foo.domain.com'</code>
<code>咱們繼續看文檔:</code>
<code>5.3 Node </code><code>groups</code>
<code>Node group A predefined group of minions declared </code><code>in</code> <code>the master configuration </code><code>file</code> <code>nodegroups setting as a compound target.</code>
<code>Nodegroups are declared using a compound target specification. The compound target documentation can be found here:</code>
<code>Compound Matchers(參考下面一段)</code>
<code>For example, </code><code>in</code> <code>the master config </code><code>file</code> <code>nodegroups setting:</code>
<code>nodegroups:</code>
<code>group1: ’[email protected],bar.domain.com,baz.domain.com or bl*.domain.com’</code>
<code>group2: ’G@os:Debian and foo.domain.com’</code>
<code>Specify a nodegroup via the -N option at the </code><code>command</code><code>-line:</code>
<code>salt -N group1 </code><code>test</code><code>.</code><code>ping</code>
<code>Specify a nodegroup with - match: nodegroup </code><code>in</code> <code>a </code><code>top</code> <code>file</code><code>:</code>
<code> </code><code>group1:</code>
<code> </code><code>- match: nodegroup</code>
<code>執行個體:</code>
<code># vim /etc/salt/master</code>
<code> </code><code>cabinet01: </code><code>'E@test2(1[1-9]|3[1-2]).company.com'</code>
<code> </code><code>cabinet02: </code><code>'E@test(12|14[0-6]|18[3-5]).company.com'</code>
<code> </code><code>cabinet03: </code><code>'E@test10[1-5].company.com'</code>
<code># salt -N cabinet02 test.ping</code>
<code>test144.company.com:</code>
<code> </code><code>True</code>
<code>test183.company.com:</code>
<code>test185.company.com:</code>
<code>test146.company.com:</code>
<code>test140.company.com:</code>
<code>test143.company.com:</code>
<code>test141.company.com:</code>
<code>test145.company.com:</code>
<code>test142.company.com:</code>
<code>test12.company.com:</code>
<code> </code>
<code>五、混合比對</code>
<code>5.4 Compound matchers</code>
<code>Compound matcher A combination of many target definitions that can be combined with boolean operators.</code>
<code>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 </code><code>type</code><code>. The currently implemented “letters” are:</code>
<code>Letter Meaning Example</code>
<code>G Grains glob match G@os:Ubuntu</code>
<code>E PCRE Minion </code><code>id</code> <code>match E@web\d+\.(dev|qa|prod)\.loc</code>
<code>P Grains PCRE match P@os:(RedHat|Fedora|CentOS)</code>
<code>L List of minions [email protected],minion3.domain.com or bl*.domain.com</code>
<code>I Pillar glob match I@pdata:foobar</code>
<code>S Subnet</code><code>/IP</code> <code>addr match [email protected]</code><code>/24</code> <code>or [email protected]</code>
<code>R Range cluster match R@%foo.bar</code>
<code>D Minion Data match D@key:value</code>
<code>Matchers can be joined using boolean and, or, and not operators.</code>
<code>For example, the following </code><code>command</code> <code>matches all minions that have a </code><code>hostname</code> <code>that begins with “webserv” and that are running Debian or it matches any minions that have a </code><code>hostname</code> <code>that matches the regular expression web-dc1-srv.</code>
<code>* :</code>
<code>salt -C ’webserv* and G@os:Debian or E@web-dc1-srv.*’ </code><code>test</code><code>.</code><code>ping</code>
<code>That same example expressed </code><code>in</code> <code>a </code><code>top</code> <code>file</code> <code>looks like the following:</code>
<code> </code><code>’webserv* and G@os:Debian or E@web-dc1-srv.*’:</code>
<code> </code><code>- match: compound</code>
<code>Note that you cannot have a leading not </code><code>in</code> <code>a </code><code>command</code><code>. Instead you must </code><code>do</code> <code>something like the following:</code>
<code>salt -C ’* and not G@kernel:Darwin’ </code><code>test</code><code>.</code><code>ping</code>
<code>[root@test200 ~]</code><code># salt -C 'E@test(12|14[0-6]|18[3-5]).company.com or dev0[1-2].office.com' test.ping </code>
<code>dev01.office.com:</code>
<code>dev02.office.com:</code>
本文轉自 pcnk 51CTO部落格,原文連結:http://blog.51cto.com/nosmoking/1636508,如需轉載請自行聯系原作者