Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Salt is based on the idea of a Master who commands one or more Minions. The automation framework Salt is very potent. The Salt architecture is based on the concept of remotely executing commands.
In this article, we will learn how to target minions, and various targeting techniques, along with examples.
Targeting Minions
Targeting minions specify which minions should execute a command or run a state by matching against hostnames, system information, defined groups, or even combinations of these.
This command displays the status of all of our minions, and while we don't have many of them, we do have enough to demonstrate targeting. Let's first use an asterisk to target all of our minions. Type:
salt * test.ping
To restart the Apache HTTPd server, for example, the command salt web1 apache.signal restart tells us that the machine web1 is the target, and the command will only be executed on that single minion.
But what if all we want to do is shut down the SQL server? Let's use the minion's ID to find it. Go ahead and take off:
salt sql.devopslibrary.com system.shutdown
That's great; the SQL server has been shut down. Pretty simple. Let's pretend something terrible has happened next. Our website has stopped responding, and we hope that restarting IIS will fix it.
Matching Minion Id
Each minion requires a distinct identifier. When a minion launches for the first time, it uses its FQDN as the identifier. The minion's id can be overridden using the id configuration setting. The minion id given is used to generate the minion's public and private keys, and if it changes, the master must accept the new key as if the minion were a new host.
Globbing
But what if we have a ton of web servers from the above example? How are we going to choose them all? It's time to start using glob targeting! Go ahead and run:
salt iis* cmd.run iisreset
Globbing is simply a method of using wildcards to match minion names, so in our example, the iisreset would target any minion whose ID begins with IIS.
Match every minion:
salt '*' test.version
Choose any of the example domains, or codingninjas.com, to match all minions:
test.version salt '*.codingninjas.com'
test.version salt '*.codingninjas.*'
Match each of the webN minions located within the codingninjas.com domain (web1.codingninjas.com, web2.codingninjas.com, etc.):
salt 'web?.codingninjas.com' version test
Match the minions from web1 to web5:
salt 'web[1-5]' test.version
Match the minions of web-x, web-y, and web-z:
salt 'web-[x-z]' test.version
Regular Expressions
Regular expressions in Perl can be used to match minions (which is globbing on steroids and a ton of caffeine).
Match both web1-prod and web1-devel minions:
salt -E 'web1-(prod|devel)' test.version
The matcher must be specified as the first option when using regular expressions in a State's top file.
Well, it’s time to step it up a bit! If you enjoy writing regex, you can do so by passing -E*. This time let’s try targeting each minion except SQL since we shut it down previously. Run:
salt -E '(?!sql)' test.ping
Lists
The simplest form of the specification is a flat list of minion IDs:
salt -L 'web1,web2,web3' test.version
Now it's time to try out List matching, which is exactly what it sounds like. Let's try a ping test against one of the IIS servers as well as the MySQL server. Type as follows:
salt -L 'iis.codingninjas.com,mysql.codingninjas.com' test.ping
Targeting with Grains
Salt includes the Grains interface, which allows minions to be targeted by system properties. So minions running on a specific operating system can be summoned to carry out a function or a kernel.
Calling via a grain is achieved by passing the -G option to salt, stating a grain, and a glob expression to match the grain's value. The target syntax is the grain key accompanied by a glob expression: "os:Arch*."
salt -G 'os:Ubuntu' test.version
All of the Ubuntu minions will respond with True.
Execute the grains.item salt function to find out what grains are available and their values.
salt '*' grains.items
Compound Targeting
Multiple target interfaces can be used in conjunction to determine the command targets. These objectives can then be combined with and or statements. An example exemplifies this well:
salt -C 'G@os:Ubuntu and webser* or E@ub.*' test.version
In this example, any minion whose id begins with ‘webser’ and is running in ‘ubuntu’ will be matched, or any minion whose id begins with ‘ub’.
Although glob is the default type of matcher, it can be overridden by using the corresponding letter followed by the @ symbol. In the preceding example, a grain is used with G@, and a regular expression is used with E@. Because it is a glob, the webser* target does not require a target type specifier.
Frequently Asked Questions
How do you include salt in a list of minions?
"salt-key -L" will display a list of all minions whose public keys you have accepted on your master.
What are the different methods to target minions?
Globbing, regular expressions and lists are the three main methods to target minions.
What exactly are minions in salt?
A Salt Minion is a managed system that receives and executes commands and configurations sent by a user via Salt Master. Salt can be run in masterless mode, with only the Minion. The masterless mode is used to manage a single machine without a Master.
Where can you get a Salt Minion ID?
The salt-minion configuration will be saved by default in /etc/salt/minion.
What exactly is the Salt command?
A wide range of operations can be carried out using salt commands and targeting individual minions and groups of minions.
Conclusion
In this article, we learned how to target minions, and various targeting techniques, along with examples. For the time being, you've done an excellent job, and hopefully, you'll feel much more comfortable with targeting.
Go through the articles below to get a good grasp of this domain: