Puppet Module for NTP - KeegMitch/Operations-Engineering-group-c GitHub Wiki
Module setup
- Create a module structure within the
/etc/puppetlabs/code/modulesdirectory on themgmtserver. Usesudo mkdirwhen 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
- Create an init.pp file in the
ntp_service/manifestsdirectory using:sudo vim init.pp
Module manifest
- Inside the
init.ppfile, 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,
}
}
Template files
- Retrieve the ntp.conf.erb file from the OE2 GitHub repository
git clone https://github.com/OlayinkaOP/OE2.git
- 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/ - Check that the
ntp.conf.erbfile is there
Applying the module
- Add one line to the end of the
site.ppfile,which is located in this path:/etc/puppetlabs/code/environments/production/manifests/site.pp,include ntp_service
- 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:
And test puppet agent:
Notify the User
- Inside the
init.ppfile, add this to thentp_serviceclass:
class { 'ntp_service::notify': }
Should look like this:
- Add this code to the end of the
init.ppfile:
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'],
}
}
- Run
test_puppet_agent( This should run without errors)
Current output: The NTP service is running