Category Archives: Puppet

Puppet – Using virtual resources to prevent duplicated resource declaration

When your Puppet setup is getting larger, you must come across the duplicated resource declaration error.

We are not allowed to declare the same resource more than one time!

So Puppet introduces the virtual resource to solve this problem. A virtual resource will not be sent to the Puppet client until it is realized. Now, we could specify a virtual resource and mark it non-virtual as many times as you want in a configuration.

For example: I have a my_packages class which contains all the some package resources. It only contains virtual resources so any other class include this would do nothing.
Continue reading Puppet – Using virtual resources to prevent duplicated resource declaration

Advertisements

Puppet – Run exec as a specific user

I would like to Puppet to install the node modules which is specified in the Node.js project package.json. But the Puppet agent is run under the root account while the project is owned by another Linux user. The Puppet exec type allows you the specific the running user but it would not include the environmental variables like the $HOME. Those variables have to be set when calling the exec type just like follow.

exec { "install node modules of a nodejs project":
  cwd         => "/home/ykyuen/nodejs-project",
  command     => "npm install",
  user        => "ykyuen",
  environment => ["HOME=/home/ykyuen"],
}

 

If you need to set more than one environmental variable, just add them in an array format.

  environment => ["HOME=/home/ykyuen", "FOO=foo"],

 

Done =)

Reference: Puppet Docs: Type Reference – Exec

Puppet – RPM Package ensure => installed failure

In Puppet, the source attribute in package type allow us to install a .rpm package by providing the file path located in the agent machine. But in that case, the package name should be the same as the .rpm filename. Otherwise, you will get following error.

package oracle-instantclient11.2-basic-11.2.0.1.0-1.x86_64 is already installed

 

Here is an example of the Puppet manifest.

#package { "oracle-instantclient": <-- this will cause error in every daemon run
package { "oracle-instantclient11.2-basic-11.2.0.1.0-1.x86_64":
  ensure   => installed,
  provider => 'rpm',
  source   => '/tmp/oracle-instantclient11.2-basic-11.2.0.1.0-1.x86_64.rpm',
  require  => File["/tmp/oracle-instantclient11.2-basic-11.2.0.1.0-1.x86_64.rpm"],
}

 

Done =)

Reference: Ensure package installed fails if it is installed

Housekeeping Puppet Master

Yesterday i used a Puppet master as an example to talk about purging old files on Linux.

Of course we can setup a cron job and run the shell command daily for housekeeping. But as we are using Puppet, we don’t we do that thru Puppet manifest?

tidy { 'puppet::reports':
  path => '/var/lib/puppet/reports',
  matches => '*.yaml',
  age => '14d',
  backup => false,
  recurse => true,
  rmdirs => false,
  type => 'ctime',
}

 

The above tidy type will delete those .yaml which is older than 14 days.

Done =)

Reference: Google Groups – Purge puppet’s reports

Puppet – Create a Custom Resource Type

We can define our own Puppet Resource Type. Here is an simple example which will takes create new folders from a existing folder.

class eureka::ykyuen ($new_folder) {
  define copy_folder {
    file { "/home/ykyuen/$name":
      source => "/home/ykyuen/src_folder",
      recurse => "true",
      group => "ykyuen",
      owner => "ykyuen",
      ensure  => directory,
      replace => "false", # Create new folder only if it doesn't exist
    }
  }

  copy_folder { ["new_folder_1", "new_folder_2"]: }
}

Continue reading Puppet – Create a Custom Resource Type

Puppet – Automate New Relic Server Monitor Agent Installation

This is an example to install the newrelic-sysmond package on the Puppet Agent.

class system::newrelic-sysmond ($licensekey) {
  package { "newrelic-sysmond": ensure => installed, }

  exec { 'Insert newrelic license key':
    command => "nrsysmond-config --set license_key=$licensekey",
    require => Package['newrelic-sysmond'],
    creates => '/etc/newrelic/key_added',
  }

  exec { 'Restart newrelic system monitor':
    command => "/etc/init.d/newrelic-sysmond start",
    require => Exec['Insert newrelic license key'],
    creates => '/etc/newrelic/key_added',
  }

  exec { 'Create the key_added file':
    command => "echo key_added > /etc/newrelic/key_added",
    require => Exec['Restart newrelic system monitor'],
    creates => '/etc/newrelic/key_added',
  }
}

 

Done =)

Puppet – Update resource which is already declared

If you want to update the declared resource, you need to make use of the resource reference which is denoted by the Capital letter of the type reference. The following example is found in the Puppet 3 Reference Menu.

file {'/etc/passwd':
  ensure => file,
}

File['/etc/passwd'] {
  owner => 'root',
  group => 'root',
  mode  => 0640,
}

 

Done =)

Reference: Puppet 3 Reference Menu – Amending Attributes With a Reference