Learning Kit

Intro to Bolt

Using Bolt: Running scripts

Time: 15 minutes
Materials needed: none

Introduction to scripts

Bolt scripts are the next step up from Bolt commands in terms of power and complexity. You can use a single invocation of Bolt to run an entire script, written in any language supported by your target nodes.

There are big advantages to using Bolt to run a script instead of individual commands. Scripts can include conditional logic, loops, and other powerful programming concepts. You can store scripts in a version control system like Git, gaining all the advantages version control provides. And it’s quicker and less error-prone to run a sequence of commands by typing bolt script run once than by typing bolt command run for each command.

To demonstrate the power of Bolt scripts, imagine that you want to gather information about a target node called node1 using the commands hostname, date, and ps. Using what you learned in the last section of this course, you could run bolt command run three times:

bolt command run "hostname" --targets node1
bolt command run "date" --targets node1
bolt command run "ps" --targets node1

Or you could run a single Bolt script:

bolt script run "get_info.sh" --targets node1

As long as the file get_info.sh contains the hostname, date, and ps commands, the end result will be exactly the same.

In case you’re wondering about the mechanics, Bolt uses a three-step process to run scripts:

1). Use SSH or WinRM to copy the script to a temporary folder on the target node. 2). Run the script on the target node, sending any output back to the host node. 3). Delete the script from the target node.

Writing scripts

Writing a Bolt script is trivial: use your favorite scripting language to write a program, and you’re done! Use Bash, PowerShell, Python, Ruby, or any other language that has an interpreter installed on the target nodes.

For example, if your target node runs Linux or macOS, you could write a Bash script containing the three commands from above:

#!/usr/bin/env bash

Or if you prefer Python and have a Python interpreter installed on your target nodes, you could put this content into a script called get_info.py:

#!/usr/bin/env python
import os

If Ruby’s your thing, you could put this content in get_info.rb:

#!/usr/bin/env ruby

Since the three commands you’ve been working with happen to be valid PowerShell commands, you could put them as-is into a PowerShell script called get_info.ps1.


Note: because of differences in how Linux, macOS, and Windows text editors save files, scripts written on Linux or macOS computers can be run on any target nodes, but scripts written on Windows computers can only be run on Windows target nodes.

Running scripts

You’ve already seen a Bolt script in action. You might have noticed that the syntax for running scripts is almost the same as it is for running commands:

bolt script run "<SCRIPT_NAME>" --targets <NODE_IDENTIFER>

Use this syntax to run scripts on target nodes with any operating system. Typically you would only run PowerShell scripts on Windows target nodes, since Linux and macOS don’t support PowerShell. Similarly, Bash scripts generally only work on Linux or macOS target nodes. But Bolt can run scripts written in cross-platform languages like Python or Ruby on any target node with an appropriate interpreter.

Puppet sites use proprietary and third-party cookies. By using our sites, you agree to our cookie policy.