How to write Puppet modules with example

simple Puppet module that installs and configures Nginx on a CentOS/RHEL system:

  1. Create the module directory and files:
sudo mkdir -p /etc/puppet/modules/nginx/manifests
sudo touch /etc/puppet/modules/nginx/manifests/init.pp
sudo touch /etc/puppet/modules/nginx/files/nginx.conf
  1. Edit the init.pp file to define the class and its parameters:
class nginx (
  $listen_port = 80,
  $server_name = 'localhost',
) {

  # Install Nginx package
  package { 'nginx':
    ensure => 'installed',
  }

  # Copy Nginx configuration file
  file { '/etc/nginx/nginx.conf':
    mode    => '0644',
    source  => 'puppet:///modules/nginx/nginx.conf',
    require => Package['nginx'],
  }

  # Start Nginx service
  service { 'nginx':
    ensure => 'running',
    enable => true,
    require => File['/etc/nginx/nginx.conf'],
  }

  # Create default virtual host
  file { '/etc/nginx/conf.d/default.conf':
    mode    => '0644',
    content => template('nginx/default.conf.erb'),
    require => Package['nginx'],
  }
}

In this code, the nginx class installs the Nginx package, copies a custom configuration file from the Puppet server to the client, starts the Nginx service, and creates a default virtual host.

  1. Edit the nginx.conf file to include your desired Nginx configuration:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    server {
        listen       <%= @listen_port %>;
        server_name  <%= @server_name %>;

        # Add your custom server configuration here
    }

    include /etc/nginx/conf.d/*.conf;
}

In this example, the nginx.conf file includes the default Nginx configuration, as well as a template for the default virtual host that will be created by the Puppet module.

  1. Edit the default.conf.erb file to define the default virtual host:
server {
    listen <%= @listen_port %> default_server;
    server_name _;

    root /usr/share/nginx/html;
    index index.html;

    # Add your custom virtual host configuration here
}

In this code, the default.conf.erb template creates a default virtual host that listens on the specified port and serves static files from the /usr/share/nginx/html directory.

  1. Apply the Puppet module to the target node:
sudo puppet apply -e 'include nginx'

This will install and configure Nginx on the target node using the Puppet module we just created.

Note that this is a simple example and there are many more features and options available when writing Puppet modules. However, this should give you a basic understanding of how to create and apply a Puppet module to manage infrastructure as code.

Leave a Reply

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