Defining a simple module to configure time

Modules are collections of manifests and files that define how to install and configure various components. Manifests contain the instructions to apply to a system's configuration. In this recipe, we'll create a simple module to install and configure the NTP daemon.

Getting ready

Puppet has a strict way of organizing modules. Your modules should always be stored in /etc/puppet/modules. Every module is a directory within this directory, containing the necessary directories that in turn contain manifests, files, templates, and so on.

How to do it…

In this recipe, we'll create the necessary directory structure, manifests, and files to configure your system's time. Perform the following steps:

  1. Create ntp/manifests in /etc/puppet/modules via the following command:
    ~]# mkdir -p /etc/puppet/modules/ntp/manifests
    
  2. Create ntp/templates to house all the templates used by the puppet module through the following:
    ~]# mkdir -p /etc/puppet/modules/ntp/templates
    
  3. Now, create the install.pp file in /etc/puppet/modules/ntp/manifests with the following contents:
    class ntp::install inherits ntp {
      package { 'ntp':
        ensure => installed,
      }
    }
  4. Create the config.pp file in /etc/puppet/modules/ntp/manifests with the following contents:
    class ntp::config inherits ntp {
      file { '/etc/ntp.conf':
        ensure  => file,
        owner   => 'root',
        group   => 'root',
        mode    => 0644,
        content => template("ntp/ntp.conf.erb"),
      }
    }
  5. Next, create the ntp.conf.erb template file in /etc/puppet/modules/ntp/templates with the following contents:
    driftfile /var/lib/ntp/drift
    
    restrict default nomodify notrap nopeer noquery
    
    restrict 127.0.0.1
    restrict ::1
    
    server 0.be.pool.ntp.org iburst
    server 1.be.pool.ntp.org iburst
    server 2.be.pool.ntp.org iburst
    server 3.be.pool.ntp.org iburst
    
    includefile /etc/ntp/crypto/pw
    
    keys /etc/ntp/keys
    
    disable monitor
  6. Create the service.pp file in /etc/puppet/modules/ntp/manifests with the following contents:
    class ntp::service inherits ntp {
      service { 'ntp':
        ensure     => running,
        enable     => true,
        hasstatus  => true,
        hasrestart => true,
        require => Package['ntp'],
      }
    }
  7. Finally, create the init.pp file that binds them all together in /etc/puppet/modules/ntp/manifests with the following contents:
    class ntp {
        include ntp::install
        include ntp::config
        include ntp::service
    }

How it works...

When applying a module to a system, it applies the directives found in the module's init.pp manifest.

As you can see, we created a template file that is "automagically" distributed to the clients. Puppet automatically creates a file share for the templates and files directories.

As you can see in the config.pp file, the template references ntp/ntp.conf.erb. Puppet will automatically resolve this to the correct location (ntp/templates/ntp.conf.erb).

There's more...

I created four manifests to install and configure Puppet. This could be easily achieved by just creating one monolithic init.pp manifest with the contents of the other three files. When you start creating complex manifests, you'll be happy to have split them up.

If you want to have a single location for all the assets (templates and files) you use in your modules, you will have to define a separate file share for this location in the /etc/puppet/fileserver.conf file, as follows:

[mount_point]
    path /path/to/files
    allow *

See also

Read up on Puppet Modules through the link https://docs.puppetlabs.com/puppet/3.8/reference/modules_fundamentals.html.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset