robot with a square head and yellow eyes

Ensuring your Puppet Agent is installed on all machines in your environment can be a challenging task.  Today I will share a tip on how you can ensure that the puppet agent us automatically deployed to all of your new virtual machines.

Explanation

When you deploy a VMWare template, you have the option to create a customization specification.  You can add a command to this customization specification to automatically deploy the puppet agent, after the template has finished deploying.

One of the options in your customization specification is a run once command. Or really, it is a list of run once commands. There are three things you need the run once script to do:

  • Download the Puppet Agent
  • Install the Puppet Agent
  •  Trigger an immediate puppet run

In the following sections, I will show the commands you need to run to accomplish the three objectives above.

Download the Puppet Agent

First, you want to download the puppet agent by using this command:

powershell "new-item C:\install -type directory; (new-object System.Net.WebClient).DownloadFile('https://downloads.puppetlabs.com/windows/puppet-agent-x64-latest.msi','C:\install\puppet-agent-x64-latest.msi’)"

The run once commands in VMWare are using the regular windows command shell, so we have to call powershell to get access to the powershell cmdlets.  Next, we pass the powershell commands into powershell.exe to actually download the puppet agent.  The above command is actually two different powershell commands separated by semi-colons.

The first command is ensuring the C:\install folder exists.

#Create C:\install folder
new-item C:\install -type directory

The second command downloads the latest version of the puppet agent, and saves it to the C:\install folder:

(new-object System.Net.WebClient).DownloadFile('https://downloads.puppetlabs.com/windows/puppet-agent-x64-latest.msi','C:\install\puppet-agent-x64-latest.msi’)"

Powershell can’t natively download a file from a web URL, so we have to use the new-object cmdlet and import the system.net.webclient library from the .net framework.  We are then able to call the downloadfile function.  The downloadfile function takes two inputs, the source of the file, and where to save the file to.  In our case, the souce of the file is: https://downloads.puppetlabs.com/windows/puppet-agent-x64-latest.msi and the destination file is: C:\install\puppet-agent-x64-latest.msi

Install the Puppet Agent

Now that the appropriate installer is downloaded, we need to  silently install the puppet agent with this command (Be sure to replace mypuppetserver.mydomain.com with your actual puppet server FQDN):

msiexec /qn /i C:\install\puppet-agent-x64-latest.msi PUPPET_MASTER_SERVER=“mypuppetserver.mydomain.com”

This command is calling the msiexec command with the /qn option to make it run silently.  We then use the PUPPET_MASTER_SERVER argument to set the puppet master we want to use for this server.

Using Chocolatey

As an alternative to downloading and installing the puppet agent as outlined above.  If you have chocolatey installed, you can use the choco commands to install the puppet agent.  The command to do it using chocolatey is:

choco install puppet-agent -installArgs ‘”PUPPET_MASTER_SERVER=mypuppetserver.mydomain.com”‘

Be sure you replace “mypuppetserver.mydomain.com” with the proper FQDN of your puppet server.

If you don’t have chocolatey installed, you can checkout our article on how to install chocolatey.  As you can see, installing this with Chocolatey is a little easier than the alternative.

Force Puppet agent to run

Now that we have installed the puippet agent, we need to trigger a puppet agent run so your machine will checkin with puppet, and start applying whatever is in the node definition.  Since puppet was just installed, the puppet command may not yet be available in the existing command shell.  For this reason, we need to open a new command shell by using the cmd /c command.  Here is an example of how you would force a run of the puppet agent right now:

cmd.exe /c "puppet agent -t"

puppet agent -t is the command to test the puppet agent. It also happens to be the easiest way to force the puppet agent to check-in immediately.

Summary

To summarize, you need to add the three following commands to the run once section of your VMWare customization specification:

powershell "new-item C:\install -type directory; (new-object System.Net.WebClient).DownloadFile('https://downloads.puppetlabs.com/windows/puppet-agent-x64-latest.msi','C:\install\puppet-agent-x64-latest.msi’)"
msiexec /qn /i C:\install\puppet-agent-x64-latest.msi PUPPET_MASTER_SERVER=“mypuppetserver.mydomain.com”
cmd.exe /c "puppet agent -t"

**Note Be sure to fill in the proper address for your puppet server on the second line

If you add the three above commands to the run once section of your customization template, your puppet agent should automatically install the first time someone logs into the server.