Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Hello Coders! This blog will discuss running bolt commands. We will then discuss how to specify the targets and connection credentials. Followed by the discussion to upload and download a file or directory. We hope that you are excited to learn about running bolt.
So, what are we waiting for? Let's dig deep into the brief discussion.
What is Bolt Command
Bolt command combines different targets and performs various actions on them. These actions may include running a simple command or a group of commands.
Bolt is used for running a command on a given list of targets. So, you need to specify the command and list of targets on which it will run. All the special shell characters are enclosed in single quotation marks. The syntax for the command is as follows:
bolt <command> run 'pwd' --targets <target_server>
This will run a command remotely on specified targets.
Reading Targets
From File
While running bolt commands, if you want to read a target file, use the '@' symbol followed by the file name. And pass this to the '--targets' option.
The command will look like this:
bolt command run 'pwd' --targets '@target.txt'
We must wrap the file name in single quotes.
From STDIN(Standard Input and Output)
The second option to read the target is using standard input and output. For reading the list of targets from stdin, pass '-' (a single dash symbol) to the '--targets' option.
The command will look like this:
bolt command run 'pwd' --targets -
Specify the Targets
Using URLs
To specify the targets while running bolt, we can use the '--targets' keyword followed by a list of targets separated by commas.
For example,
bolt command run 'pwd' --targets target1_url, target2_url
Using Inventory Files
Sometimes specifying a URL(Universal Resource Identifier) is not possible. In that case, you can give the name of the inventory files if present instead of the target URL. For example, if for target1, the file name is file1, and for target2, the file name is file2, then the command will be as follow:
bolt command run 'pwd' --targets file1, file2
Using Meta Characters
In some cases, many different targets have similar names. For this, bolt supports glob matching for specifying the target. For example, to run a command on the targets that begin with the word 'coding' followed by a single character, the command will be as follows:
bolt command run 'pwd' --targets 'Example?'
Similar to this, there exist a few other meta-characters like
Example* = Used to match any number of targets starting with the word 'Example'.
Example? = Used to match any number of targets starting with the word 'Example' and followed by one more character.
Example[1-N] = Matches all the targets containing every single character from the set. Like 'Example1', 'Example2', till 'ExampleN'.
Example[^N] = Matches all the targets containing every single character from the set till N but not N itself. Like 'Example1', 'Example2', till 'ExampleN-1'.
{A, B} = Matches all the targets matching the given set. Like for {A, B}1, both A1 and B1 will be matched.
Using Previous Commands
After executing every command, its results are stored in the " .rerun.json " file. The rerun accepts any of the three values:
Success: Specifies the list of targets for which the command get succeeds.
Failure: Specifies the list of targets for which the command fails.
All: Specifies all the targets on which the command runs.
The syntax for this is:
bolt task run restart_server --rerun success/failure/all
If you don't want to save data for specific commands, you can disable them using the ' --no-save-rerun' option. For this, you need to add this option at the end of the above command as follows:
bolt task run restart_server --rerun success/failure/all --no-save-rerun
This is all about how to specify the targets for running bolts. Let's now discuss how to read these targets from different sources.
Specify Connection Credentials
For running bolt to establish a connection with a remote target, you need to specify the credentials of that connection. The easiest way to achieve this is to pass the username and password to the command.
The syntax for passing the credentials is as follows:
bolt command run 'pwd' --targets <target_source> --user <user_name> --password <password>
You need to replace the <user_name> and <password> with your credentials.
Also, to protect your password from not appearing in the process list, use the "--password-prompt" option.
bolt command run 'pwd' --targets <target_source> --user <user_name> --password-prompt
Hit enter and type your password and again hit enter.
The bolt uses SSH(Secure Shell) transport medium to establish the connection.
Upload and Download a File or Directory
To upload a file or directory, you must provide a source, destination path, and a list of targets. Both accept relative and absolute paths. The command for this will be:
Replace the <source_path> and <destination_path> with the URL's. If the destination directory does not exist, the bolt will create it.
Frequently Asked Questions
What is bolt command?
Bolt is used for running a command on a given list of targets. Bolt command combines different targets and performs various actions on them.
Which transport is used by the bolt for connection?
The bolt uses SSH(Secure Shell) transport medium to establish the connection.
How to make your password secure in running bolt?
To protect your password from not appearing in the process list, use the "--password-prompt" option. The command used is -> bolt command run 'pwd' --targets servers --user <user_name> --password-prompt
What are the different metacharacters used in the bolt?
Various metacharacters used in the bolt are : *,? , [SET], [^SET] , {A,B} and \(Escapes).
What parameters are accepted by the rerun bolt command?
The rerun command accepts three parameters(any one). These are ‘success’, ‘failure’, and ‘all’.
Conclusion
In this blog, we discussed running bolt commands and how to specify and read targets. Followed by how to specify credentials for the connection and transport used for making the connection. We discussed how to upload and download files or directories. We hope that now you understand the topic!
If you found this one interesting, explore our other related blogs as well: