Category Archives: puppet

Creating a Puppet Module Folder Structure with New-PuppetModule

Recently I’ve been writing a lot of Puppet modules. Usually this starts for me by creating a folder structure which typically looks something along the lines of this:

dir | tree

New-PuppetModule01

Creating this each time via Explorer soon became pretty tedious, so I put together the following function New-PuppetModule. This is along the lines of the puppet command to generate a module structue:

puppet module generate name

By default New-PuppetModule will create the top level module folder, a manifests folder and a blank init.pp. Optionally you can add in say files and templates folders

New-PuppetModule -Path C:\Code\Puppet\modules -Name module2 -Files -Templates

Or you can go the whole way and create all folders as defined in this document. (I have not enough experience with Puppet to know whether anyone actually would do this)

New-PuppetModule -Path C:\Code\Puppet\modules -Name module2 -Full

Here’s the function:

Function New-PuppetModule
{
<#
 .SYNOPSIS
 Function to create the folder structure for a new Puppet class.

.DESCRIPTION
 Function to create the folder structure for a new Puppet class.

.PARAMETER Path
 Path to the Puppet modules folder

.PARAMETER Name
 Mane of the Puppet module

.PARAMETER Files
 Create a 'files' folder in the Module

.PARAMETER Templates
 Create a 'templates' folder in the Module

.PARAMETER Lib
 Create a 'lib' folder in the Module

.PARAMETER Tests
 Create a 'tests' folder in the Module

.PARAMETER Spec
 Create a 'spec' folder in the Module

.PARAMETER Full
 Create all standard module folders

.INPUTS
 IO.DirectoryInfo

.OUTPUTS
 None

.EXAMPLE
 PS> New-PuppetModule -Path c:\Users\Documents\Fred\puppet\modules -Name newmodule -Files -Templates

.EXAMPLE
 PS> New-PuppetModule -Path c:\Users\Documents\Fred\puppet\modules -Name newmodule -Full

 .NOTES
 Version: 1.0 - First draft
 Date: 31/10/2013
 Tag: puppet,module
 Module layout determined from http://docs.puppetlabs.com/puppet/3/reference/modules_fundamentals.html
#>
 [CmdletBinding(DefaultParameterSetName='Individual')]

Param
 (
 [Parameter(Mandatory=$true)]
 [ValidateNotNullOrEmpty()]
 [IO.DirectoryInfo]$Path,

[Parameter(Mandatory=$true)]
 [ValidateNotNullOrEmpty()]
 [String]$Name,

[Parameter(Mandatory=$false,ParameterSetName='Individual')]
 [Switch]$Files,

[Parameter(Mandatory=$false,ParameterSetName='Individual')]
 [Switch]$Templates,

[Parameter(Mandatory=$false,ParameterSetName='Individual')]
 [Switch]$Lib,

[Parameter(Mandatory=$false,ParameterSetName='Individual')]
 [Switch]$Tests,

[Parameter(Mandatory=$false,ParameterSetName='Individual')]
 [Switch]$Spec,

[Parameter(Mandatory=$false,ParameterSetName='Full')]
 [Switch]$Full
 )

try {

# --- Check that the path to the Puppet modules folder exists
 if (!($Path.Exists)){

throw "Please supply the correct path to the Puppet modules folder"
 }

# --- Ensure module name is lower case and no spaces
 $Name = ($Name -replace " ","_").ToLower()

# --- Check if the module already exists
 $NewModulePath = Join-Path -Path $Path -ChildPath $Name

 if (Test-Path $NewModulePath){

throw "Puppet module $Name already exists"
 }

 # --- Create module folder, default manifests folder and init.pp file
 New-Item -Path $NewModulePath -ItemType Directory | Out-Null

$ManifestsPath = (Join-Path -Path $NewModulePath -ChildPath "manifests")

New-Item -Path $ManifestsPath -ItemType Directory | Out-Null
 New-Item -Path (Join-Path -Path $ManifestsPath -ChildPath "init.pp") -ItemType File | Out-Null

# --- Create the module subfolders depending on which have been requested
 switch ($PsCmdlet.ParameterSetName)
 {
 "Individual" {

 if ($PSBoundParameters.ContainsKey('Files')){

New-Item -Path (Join-Path -Path $NewModulePath -ChildPath "files") -ItemType Directory | Out-Null
 }

if ($PSBoundParameters.ContainsKey('Templates')){

New-Item -Path (Join-Path -Path $NewModulePath -ChildPath "templates") -ItemType Directory | Out-Null
 }

if ($PSBoundParameters.ContainsKey('Lib')){

New-Item -Path (Join-Path -Path $NewModulePath -ChildPath "lib") -ItemType Directory | Out-Null
 }

if ($PSBoundParameters.ContainsKey('Tests')){

New-Item -Path (Join-Path -Path $NewModulePath -ChildPath "tests") -ItemType Directory | Out-Null
 }

if ($PSBoundParameters.ContainsKey('Spec')){

New-Item -Path (Join-Path -Path $NewModulePath -ChildPath "spec") -ItemType Directory | Out-Null
 }

break
 }

 "Full" {

 New-Item -Path (Join-Path -Path $NewModulePath -ChildPath "files") -ItemType Directory | Out-Null
 New-Item -Path (Join-Path -Path $NewModulePath -ChildPath "templates") -ItemType Directory | Out-Null
 New-Item -Path (Join-Path -Path $NewModulePath -ChildPath "lib") -ItemType Directory | Out-Null
 New-Item -Path (Join-Path -Path $NewModulePath -ChildPath "tests") -ItemType Directory | Out-Null
 New-Item -Path (Join-Path -Path $NewModulePath -ChildPath "spec") -ItemType Directory | Out-Null

break
 }
 }
 }

catch [Exception]{

throw "Unable to create new Puppet module"
 }
}

Configuring puppet.conf To Support A Proxy Server

PuppetForge

Needed to configure a new Puppet Enterprise server to use a proxy server so that it could search and install modules from Puppet Forge since initial attempts to download were failing with:

[[email protected] /]# puppet module search motd
Notice: Searching https://forge.puppetlabs.com …
Error: Could not connect to https://forge.puppetlabs.com
There was a network communications problem
The error we caught said ‘getaddrinfo: Name or service not known’
Check your network connection and try again

I found the setting http_proxy_host and also some incorrect examples which suggested something like the following should be added to puppet.conf

[main]
certname = test01.local
vardir = /var/opt/lib/pe-puppet
logdir = /var/log/pe-puppet
rundir = /var/run/pe-puppet
modulepath = /etc/puppetlabs/puppet/modules:/opt/puppet/share/puppet/modules
server = test01.local
user = pe-puppet
group = pe-puppet
archive_files = true
archive_file_server = test01.local
http_proxy_host = proxy.server01.local:8080

turns out you can’t supply the port number on this line, an additional parameter http_proxy_port is needed to make it work

[main]
certname = test01.local
vardir = /var/opt/lib/pe-puppet
logdir = /var/log/pe-puppet
rundir = /var/run/pe-puppet
modulepath = /etc/puppetlabs/puppet/modules:/opt/puppet/share/puppet/modules
server = test01.local
user = pe-puppet
group = pe-puppet
archive_files = true
archive_file_server = test01.local
http_proxy_host = proxy.server01.local
http_proxy_port = 8080

Now things are happier 🙂

Update: The above only fixed  puppet module search motd for me, not  puppet module install puppetlab-motd 

Looks like you might also need to set the following environment variables too:

export http_proxy=http://proxy.server01.local:8080

export https_proxy=http://proxy.server01.local:8080

http://projects.puppetlabs.com/issues/15453