How to Create Puppet Bolt Tasks Part 2: Creating and Installing Tasks

In How to Create Puppet Bolt Tasks Part 1: Understanding Modules and Tasks, I explained what Puppet Bolt tasks are and how they are related to modules. If you haven’t read that article, I suggest you do before following the instructions below. In addition, it’s worth reading the other articles in the series to understand how to work with Linux and Puppet Bolt:

Puppet Bolt Agentless Automation for Linux and Windows Server
How To Run Commands on Remote Windows Servers Using Puppet Bolt
Use Puppet Bolt Tasks to Manage Windows Server
Remote Management Using Puppet Bolt and Windows Subsystem for Linux

Create a Puppet Module and Task

Before creating a task, we need a module for it to live in. All that’s required is a directory for the module and a subfolder called tasks. In the example below, I’m creating a folder structure for a module called ‘petriusers’ in the default modules directory. The -p switch creates any parent directories of the tasks folder if they don’t already exist. Note that you should swap out /russell/ for the name of your home directory.

I’m going to use a PowerShell script for my task. I need to create the script and then move it to the tasks directory. I’m using vi to create a script file. vi is a text editor for Linux that is installed by default in most distributions. I’ll just copy the contents of a pre-existing script to the file and then save it.

To modify a file in vi, you need to press the INSERT key. To break out of INSERT mode, press ESC. To save a file, type a colon (SHIFT + ;), then w followed by q on the command line. ‘W’ stands for write and ‘q’ for quit.

How to Create Puppet Bolt Tasks (Image Credit: Russell Smith)
How to Create Puppet Bolt Tasks (Image Credit: Russell Smith)

Once the script file is saved, move it from the working directory to the tasks folder, not forgetting to swap out /russell/ for your home directory name.

Install a Module Using PuppetFile

Now that we have a module and task created, it needs to be installed so that it can be used with Puppet Bolt. Modules are installed using a file called Puppetfile. You specify the modules you want to install in the file and then run a command to install the module(s). My Puppetfile will contain one line that looks like this:

How to Create Puppet Bolt Tasks (Image Credit: Russell Smith)
How to Create Puppet Bolt Tasks (Image Credit: Russell Smith)

You can see the module name, petriusers, and the local flag is set to true to designate that it shouldn’t be downloaded from Puppet Forge. Again, I’ll create the file using vi and paste the line of code above into the file and save it.

Now let’s move the file to the bolt directory:

Finally, install the tasks listed in Puppetfile:

We can check the module was installed by listing the available tasks:

How to Create Puppet Bolt Tasks (Image Credit: Russell Smith)
How to Create Puppet Bolt Tasks (Image Credit: Russell Smith)

And that is it! You can now run the task like you would any other. You can see below that the task is referred to by module_name::task_name.

If your script accepts arguments, just add them as shown below. My script accepts an argument called ‘days’, setting the number of days for which I want to retrieve user login history information from the event log.

In a forthcoming article, I’ll show you how to orchestrate Puppet Bolt tasks using plans.

Related Topics:

  • Cloud Computing

    Don't have a login but want to join the conversation? Sign up for a Petri Account

    IT consultant, contributing editor @PetriFeed , and trainer @Pluralsight . All about Microsoft, Office 365, Azure, and Windows Server.