Puppet Module for NTP - KeegMitch/Operations-Engineering-group-c GitHub Wiki

Module setup

  1. Create a module structure within the /etc/puppetlabs/code/modules directory on the mgmt server. Use sudo mkdir when creating the directories.

Note: To access the puppet modules directory, I have now added an alias called puppet_modules , which directs us to the directory where we configure our own modules

ntp_service
ntp_service/manifests
ntp_service/files
ntp_service/templates

image

  1. Create an init.pp file in the ntp_service/manifests directory using: sudo vim init.pp

Module manifest

  1. Inside the init.pp file, put in the following code:
class ntp_service {
  class { 'ntp_service::install': }
  class { 'ntp_service::config': }
  class { 'ntp_service::service': }
}

class ntp_service::install {
  package { 'ntp':
    ensure => present,
  }
}

class ntp_service::config {
  if $hostname == 'mgmt-c' {
    $restrict = "restrict 10.25.0.0 mask 255.255.0.0 nomodify notrap"
    $server = "server 127.127.1.0"
    $fudge = "127.127.1.0 stratum 10"
  } else {
    $restrict = ''
    $server = "server mgmt-c.foo.org.nz prefer"
    $fudge = ''
  }

  file { '/etc/ntp.conf':
    ensure => present,
    owner => 'root',
    group => 'root',
    mode => '0444',
    content => template("ntp_service/ntp.conf.erb"),
  }
}

class ntp_service::service {
  service { 'ntp':
    ensure => running,
    enable => true,
  }
}


image

Template files

  1. Retrieve the ntp.conf.erb file from the OE2 GitHub repository

git clone https://github.com/OlayinkaOP/OE2.git

image

  1. Copy it into the templates subdirectory of the ntp module. Do this command inside the repo directory: sudo cp ntp.conf.erb /etc/puppetlabs/code/modules/ntp_service/templates/
  2. Check that the ntp.conf.erb file is there

image

Applying the module

  1. Add one line to the end of the site.pp file,which is located in this path: /etc/puppetlabs/code/environments/production/manifests/site.pp , include ntp_service

image

  1. Run the puppet agent (lab says we should alias it): sudo /opt/puppetlabs/puppet/bin/puppet agent --server=mgmt-c --no-daemonize --verbose --onetime

Note: The alias to this is connect_puppet_agent, the alias test_puppet_agent is for this command: sudo /opt/puppetlabs/puppet/bin/puppet agent --test. The aliases have been configured and documented on this page: Creating Aliases

This is what the output should be when you run connect puppet agent:

image

And test puppet agent:

image

Notify the User

  1. Inside the init.pp file, add this to the ntp_service class:

class { 'ntp_service::notify': }

Should look like this:

image

  1. Add this code to the end of the init.pp file:
class ntp_service::notify {
  exec { 'check_ntp_status':
    command     => '/bin/systemctl is-active ntp',
    refreshonly => true,
    logoutput   => true,
    notify      => Notify['ntp_status'],
  }

  exec { 'notify_ntp_status':
    command     => '/bin/systemctl is-active ntp',
    refreshonly => true,
    logoutput   => true,
    subscribe   => Exec['check_ntp_status'],
    notify      => Notify['ntp_status'],
  }

  notify { 'ntp_status':
    message  => 'NTP service is running',
    withpath => false,
    subscribe => Exec['notify_ntp_status'],
  }
}

  1. Run test_puppet_agent ( This should run without errors)

Current output: The NTP service is running

image