Table of contents
1.
Introduction
2.
What is Bolt Command
3.
Reading Targets
3.1.
From File
3.2.
From STDIN(Standard Input and Output)
4.
Specify the Targets
4.1.
Using URLs
4.2.
Using Inventory Files
4.3.
Using Meta Characters
4.4.
Using Previous Commands
5.
Specify Connection Credentials
6.
Upload and Download a File or Directory
7.
Frequently Asked Questions 
7.1.
What is bolt command?
7.2.
Which transport is used by the bolt for connection?
7.3.
How to make your password secure in running bolt?
7.4.
What are the different metacharacters used in the bolt?
7.5.
What parameters are accepted by the rerun bolt command?
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

Running Bolt

Author Ayushi Goyal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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.

introduction

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. 

about bolt

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

reading target

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'
output from file

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 -
output from stdin

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
output using rerun success

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
output from 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.

output

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. 

output

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:

bolt file upload <source_path> <destination_path> --targets servers

upload and download

Similarly, for downloading a file or directory, the command will be as follows:

bolt file download <source_path> <destination_path> --targets servers


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:

Check out the guided paths on Coding Ninjas Website, online coding coursespractice on Coding Ninjas StudioCoding Ninjas Studio Interview BundleCoding Ninjas Studio Interview ExperiencesCoding Ninjas LibraryCoding Ninjas Studio Contests, and Coding Ninjas Studio Test Series for more excellent content. Do upvote our blog to assist other ninjas in their development. 

Keep learning, and Keep Growing!

Happy Coding!

Live masterclass