Puppet logs for Windows agent

Puppet logs

To use Puppet on a Windows machine, you will need to install the Puppet agent software on the machine. This can be done manually or by using a package manager like Chocolatey.

Once the Puppet agent is installed, you will need to configure it to connect to a Puppet Master server, which will hold the configuration information for your infrastructure. You can then use Puppet to manage the configuration of your Windows machines.

The Puppet agent logs contain information about the actions taken by the Puppet agent on a managed machine. It can be used to troubleshoot issues that arise during the configuration management process.

Create Puppet log

Here is a code snippet that You can use for ensuring that Puppet logs for Windows agent are stored in dedicated Event Log.

exec { 'Create Puppet Log':
  path => 'C:\windows\system32',
  command => 'cmd.exe /c reg add
              HKLM\System\CurrentControlSet\Services\EventLog\Puppet\Puppet
              /v EventMessageFile /t REG_EXPAND_SZ
              /d "C:\Program Files\Puppet Labs\Puppet\puppet\bin\puppetres.dll"
              && shutdown /r /t 0',
  unless => 'cmd.exe /c reg query
             HKLM\System\CurrentControlSet\Services\EventLog\Puppet\Puppet',
}

By default the Puppet agent installed on Windows will run as a Windows service. The logs from Puppet runs are recorded in Application Event Log. It is very easy to change that behavior, which I would strongly advise as one of the first things to be done after agent installation. Puppet docs clearly define the options to be chosen. In this post, however I would like to present how easily it is to automate this task by putting this code snippet inside basic Puppet module.

When the Puppet log gets created

Unless clause of the exec command is being used to ensure that Puppet log is being added just once. Whenever reg query does not return any result the command clause will be executed. The logs will be created. On the other hand, if reg query produces any results, command execution will be skipped.

exec { 'Create Puppet Log':
  path => 'C:\windows\system32',
  command => 'cmd.exe /c reg add
              HKLM\System\CurrentControlSet\Services\EventLog\Puppet\Puppet
              /v EventMessageFile /t REG_EXPAND_SZ
              /d "C:\Program Files\Puppet Labs\Puppet\puppet\bin\puppetres.dll"
              && shutdown /r /t 0',
  unless => 'cmd.exe /c reg query
             HKLM\System\CurrentControlSet\Services\EventLog\Puppet\Puppet',
}

Creation of the Puppet log

Command part 1 has been taken directly from Puppet docs. It will create separate Puppet log in the Windows Event Log.

exec { 'Create Puppet Log':
  path => 'C:\windows\system32',
  command => 'cmd.exe /c reg add
              HKLM\System\CurrentControlSet\Services\EventLog\Puppet\Puppet
              /v EventMessageFile /t REG_EXPAND_SZ
              /d "C:\Program Files\Puppet Labs\Puppet\puppet\bin\puppetres.dll"
              && shutdown /r /t 0',
  unless => 'cmd.exe /c reg query
             HKLM\System\CurrentControlSet\Services\EventLog\Puppet\Puppet',
}

Reboot is required

Command part 2 is basically about making sure that command part 1 has been realized. In order to achieve that the Windows server must be rebooted. Next time when Puppet runs on the newly started machine the logs from Puppet runs will be recorded in the Puppet Event Log.

exec { 'Create Puppet Log':
  path => 'C:\windows\system32',
  command => 'cmd.exe /c reg add
              HKLM\System\CurrentControlSet\Services\EventLog\Puppet\Puppet
              /v EventMessageFile /t REG_EXPAND_SZ
              /d "C:\Program Files\Puppet Labs\Puppet\puppet\bin\puppetres.dll"
              && shutdown /r /t 0',
  unless => 'cmd.exe /c reg query
             HKLM\System\CurrentControlSet\Services\EventLog\Puppet\Puppet',
}

References:

Leave a Reply

Your email address will not be published. Required fields are marked *