Cloud Computing

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.

Sponsored Content

Passwords Haven’t Disappeared Yet

123456. Qwerty. Iloveyou. No, these are not exercises for people who are brand new to typing. Shockingly, they are among the most common passwords that end users choose in 2021. Research has found that the average business user must manually type out, or copy/paste, the credentials to 154 websites per month. We repeatedly got one question that surprised us: “Why would I ever trust a third party with control of my network?


mkdir -p /home/russell/.puppetlabs/bolt/modules/petriusers/tasks

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.
vi history.ps1

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.

mv history.ps1 /home/russell/.puppetlabs/bolt/modules/petriusers/tasks

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:

mod 'petriusers', local: true

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.

vi Puppetfile

Now let’s move the file to the bolt directory:
mv Puppetfile /home/russell/.puppetlabs/bolt/

Finally, install the tasks listed in Puppetfile:
bolt puppetfile install

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

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.

bolt task run petriusers::history -n winrm://server1 -u administrator -p --no-ssl

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.
bolt task run petriusers::history days=3 -n winrm:// server1 -u administrator -p --no-ssl

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

Related Topics:

BECOME A PETRI MEMBER:

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

Register
Comments (0)

Leave a Reply

IT consultant, Contributing Editor @PetriFeed, and trainer @Pluralsight. All about Microsoft, Office 365, Azure, and Windows Server.
13 Email Threat Types to Know About Right Now

As email threats evolve and multiply, keeping track of them all—and staying protected against the many different types—becomes a complex challenge. Today, that requires more than just the traditional email gateway solution that used to be good enough.

In this eBook you will learn:

  • What are the most common and challenging email attacks for organizations?
  • How to defend against sophisticated email threats, such as spoofing, social engineering, and fraud
  • How to protect employees at the inbox level with the right technologies and security-awareness training
  • How to use a multilayered protection strategy to reduce susceptibility to email attacks and better defend your business and employees

Sponsored by: