Table of contents
1.
Introduction
2.
How to Run a Script in Bolt
2.1.
Example
3.
Wrapping Scripts in Plan
4.
Frequently Asked Questions
4.1.
Can we check the log messages in the Bolt plan?
4.2.
What is target refers to in Bolt automation?
4.3.
What type of file is created in the Bolt plan?
4.4.
Do we need to include environment variables in every script?
4.5.
What is a task in Bolts?
5.
Conclusion
Last Updated: Mar 27, 2024
Medium

Scripts in Bolt

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

Introduction

This blog will help to understand the scripts in Bolt. Bolt is an orchestration tool to automate the configuration of devices. You can install Bolt on any operating system and automate the servers and applications.

PUPPET BOLT LOGO

 Bolt can be used to deploy applications, debug servers, patch and upgrade systems, or halt and restart services. This blog will guide you on how to load a script in the Bolt and how we can manipulate the scripts in the Bolt.

How to Run a Script in Bolt

puppet bolt

The Bolt follows a procedure when you try to run the script in Bolt. First, the script will be copied from the Bolt controller to a temporary directory automatically by the Bolt to the target and runs the script. Also, once it is got executed script is deleted from the temporary directory. The target here is the machine or remote device we are targeting with the help of Bolt.

As long as the proper interpreter has been installed on the target operating system, you can run scripts in any language in Bolt.  Also, any scripting language the target is capable of using.

There are a few things you should know to run scripts in Bolt.

  • Correct script file reference in command.
  • Mention the necessary arguments for the script.
  • Available options in Botl CLI. ( you can check out the official documentation for this).
     

There are multiple ways in Bolt to refer to the scripts.

  1. You can use the scripts from particular directories in modules available on the modulepaths like <module>/scripts/scriptname.sh. This is the puppet file reference way. Instead of /scripts, you can also use /files to load the scripts like <module>/files/scriptname.sh, but it is not preferable.
  2. You can use the relative path from the root Bolt project folder.
  3. You can also use an absolute file path in Bolt.

Example

Let's demonstrate with the help of the script name image_upgrade.sh it is available in a module named docker_modules. This script will update the image for every Docker container available in the docker-project project.

#!/bin/sh
for x in `docker-projects ps --services |sort`; do
  echo "redoing $x"
  docker-projects rm -s -f $x
 docker-projects pull $x
done
docker-projects up -d --remove-orphans

Image_upgrade.sh contains the above content as a script. 

You can run the above script with the command: 

bolt script run dockers_modules/scripts/Image_upgrade.sh -t <TARGETS>



1. You can pass arguments as command line like in the below example:

#!/bin/sh
cd $1
for x in `docker-projects ps --services |sort`; do
  echo "redoing $x"
  docker-projects rm -s -f $x
 docker-projects pull $x
done
docker-projects up -d --remove-orphans

In the above script cd $1 is the argument we are passing.

Run the above script with the command:

bolt script run dockers_modules/scripts/Image_upgrade.sh ./docker -t <TARGETS>



2. You can pass arguments as environment variables. 

Environment variables are those variables that can affect the way a script is running with their dynamic nature.

#!/bin/sh
if [ -n $Docker_Directory ]
  cd $Docker_Directory 
fi
for x in `docker-projects ps --services |sort`; do
  echo "redoing $x"
  docker-projects rm -s -f $x
 docker-projects pull $x
done
docker-projects up -d --remove-orphans

The above script has Docker_Directory as an environment variable and will observe the changes in the Docker_Directory and change directories.

You can run the above script with the command: 

bolt script run dockers_modules/scripts/Image_upgrade.sh -t <TARGETS> --env-var Docker_Directory=./docker

Wrapping Scripts in Plan

You can warp your script in plan in Bolt. A plan is like a blueprint or a template you can assume in Bolt, which can help you to make the scripts available in the projects. By typing bolt plan show or Get-BoltPlan in PowerShell, you can make your scripts available to your teammates.

Follow the below instructions to create a plan for a script.

  1. Create a new directory name docker_modules to store your Bolt projects, and inside the directory, initialize your project with the command bolt project init in the command shell.
     
  2. After this, create a scripts/ directory in your project.
     
  3. Move the image_uprgrade.sh script we have created in the script/ folder.
     
  4. Now use the following command to make a plan for the image_upgrade script: 

a. bolt plan new docker_modules: image_uprgrade.sh --script docker_modules/scripts image_uprgrade.sh.
 

5. The above command creates a YAML plan file in Bolt which will have targets as parameters so the script can be run in targets and return the result.
 

6. Running the same command with the —pp or -Pp option will allow you to construct a Puppet language plan in place of YAML.
 

7. File plans/image_upgrade.YAML contains the Plan for the script, and you can run this plan with the command bolt plan run docker_modules::image_upgrade -t <TARGETS>.
 

Frequently Asked Questions

Can we check the log messages in the Bolt plan?

We can check the log messages before or after executing the script.

What is target refers to in Bolt automation?

Target in Bolt refers to the remote devices or machines we target to perform the automation.

What type of file is created in the Bolt plan?

A YAML file is created for Bolt Plan to execute the script.

Do we need to include environment variables in every script?

No, it is not compulsory to include the environment variable. You can use if it is required.

What is a task in Bolts?

The task in Bolt is scripts with an extra metadata file included.

Conclusion

In this blog, we learned about the scripts in Bolt and how we can load the scripts in the Bolt interface. We have also discussed the plan in Bolt and how to warp a script in Bolt step by step.

To learn more about Bolt, check out the following articles.

To learn more about DSA, competitive coding, and many more knowledgeable topics, please look into the guided paths on Coding Ninjas Studio. Also, you can enroll in our courses and check out the mock test and problems available to you. Please check out our interview experiences and interview bundle for placement preparations.

Happy Coding!

Live masterclass