Puppet is a configuration management tool used to automate the management and configuration of your server infrastructure. Below are a few examples of Puppet manifests that illustrate how you can manage different aspects of your systems using Puppet's declarative language.

1. Installing a Package

This basic Puppet manifest ensures that a specific package, in this case, httpd (Apache web server), is installed on your system.

node 'server.example.com' {
  package { 'httpd':
    ensure => installed,
  }
}

2. Managing Service

The following manifest ensures that a service (e.g., httpd) is enabled and running. It also ensures that the service is subscribed to changes in a specific configuration file, meaning the service will restart if the file changes.

node 'server.example.com' {
  service { 'httpd':
    ensure    => running,
    enable    => true,
    subscribe => File['/etc/httpd/conf/httpd.conf'],
  }
}

3. File Resource Management

This manifest ensures a specific configuration file (/etc/httpd/conf/httpd.conf) has the correct permissions, owner, and group. It also provides the content of the file directly within the manifest.

node 'server.example.com' {
  file { '/etc/httpd/conf/httpd.conf':
    ensure  => file,
    owner   => 'root',
    group   => 'root',
    mode    => '0644',
    content => template('httpd/httpd.conf.erb'),
  }
}

In this example, the content attribute uses the template function, which fills the content of the file with the result rendered from a template file located at modules/httpd/templates/httpd.conf.erb.

4. Executing Commands

This example demonstrates running a command to clean up temporary files. It ensures the command runs only if the directory is not empty.

node 'server.example.com' {
  exec { 'cleanup-temp-files':
    command => 'rm -rf /tmp/*',
    onlyif  => 'ls /tmp',
  }
}

5. Creating a User

This manifest creates a user with specified properties such as home directory, managehome to manage the home directory, and shell.

node 'server.example.com' {
  user { 'johndoe':
    ensure     => present,
    uid        => '1001',
    gid        => '1001',
    home       => '/home/johndoe',
    managehome => true,
    shell      => '/bin/bash',
    password   => '$1$xyz$3Baxxxxxxeo/',
  }
}

6. Managing Cron Jobs

This example sets up a cron job for a user. It ensures that a script runs at a specified time.

node 'server.example.com' {
  cron { 'logrotate':
    ensure  => present,
    command => '/usr/sbin/logrotate',
    user    => 'root',
    hour    => '2',
    minute  => '30',
  }
}