Puppet: System Administration Automated

Support

Finding out the state of resources in Ruby. This is used by Julian Simpson to work out if packages are available for tests.

#!/usr/bin/ruby
require 'puppet'
package_name = 'postfix'
# new type instance
package =  Puppet::Type.type(:package).newpackage(:name => package_name, :ensure => :installed ) 

# what changes would Puppet like to make?
current = package.evaluate

# this is an evil way to find out if the above package is installed.
if  current.length == 0
  puts "#{package_name} installed "
else
  puts "#{package_name} isn't installed "
end

Controlling a service example

#!/usr/bin/ruby

service_name = 'postfix'
action = 'start' # e.g., (start|stop|restart|status)

require 'puppet'
service = Puppet::Type.type(:service).newservice(:name => service_name)

service.provider.send(action)