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.
Read the Best Personal and Business Tech without Ads
Staying updated on what is happening in the technology sector is important to your career and your personal life but ads can make reading news, distracting. With Thurrott Premium, you can enjoy the best coverage in tech without the annoying ads.
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.
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.
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
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:
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
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.