Question regarding multi-OS NTP manifest

160 views
Skip to first unread message

Jesse

unread,
Apr 15, 2012, 7:44:16 PM4/15/12
to Puppet Users
Hi all,

I'm jumping into learning puppet with the modules on Puppetlabs
websites, but I'm running into a few issues and I could use some help.
I'm currently working on the second exercise here:

http://docs.puppetlabs.com/learning/variables.html

I can write a manifest that installs NTP for CentOS:

package { 'ntp':
ensure => installed,
}

file { '/etc/ntp.conf':
owner => 'root',
group => 'root',
mode => '640',
content => "server 0.rhel.pool.ntp.org",
notify => Service['ntpd'],
require => Package['ntp'],
}

service { 'ntpd':
ensure => running,
enable => true,
hasstatus => true,
hasrestart => true,
}

But when I try to use the $case variable to select the correct OS, I
get an error. Here's my manifest:

package { 'ntp':
ensure => installed,
}
file { '/etc/ntp.conf':
content => "server 0.rhel.pool.ntp.org",
owner => 'root',
mode => '640',
require => Package['ntp'],
}
case $operatingsystem {
centos, redhat: { $ntp = "ntp" }
debain, ubuntu: { $ntp = "ntpd" }
default: { fail("Unrecognized operating system for webserver") }
}

if $ntp == 'ntp' {
service { 'ntp':
name => $ntp,
ensure => running,
enable => true,
hasrestart => true,
hasstatus => true,
}
}
else {
service { 'ntpd':
name => $ntp,
ensure => running,
enable => true,
hasstatus => true,
hasrestart => true,
}
}

I've tried to reorder things a few different ways but I have a feeling
this is related to how I'm using the case variable and the if/else
statement. My error is this:

err: /Stage[main]//Service[ntp]/ensure: change from stopped to running
failed: Could not start Service[ntp]: Execution of '/sbin/service ntp
start' returned 1: at /root/learning-manifests/ntp.pp:24

Which confuses me, because the after the manifest is finished the /etc/
ntp.conf file is written and configured correctly, NTP is installed,
but it's calling '/sbin/service ntp start' as opposed to '/etc/init.d/
ntp start.'

When I run the vanilla script, everything configures correctly and
ntp(d) is started. Why is my second script using /sbin when it seems
that it should be using /etc/init.d/?

Thanks!

Jesse

Wil Cooley

unread,
Apr 17, 2012, 1:34:02 AM4/17/12
to Puppet Users
On Apr 15, 4:44 pm, Jesse <anonym...@gmail.com> wrote:

> case $operatingsystem {
>     centos, redhat: { $ntp = "ntp" }
>     debain, ubuntu: { $ntp = "ntpd" }

This is backwards; centos/redhat should be 'ntpd' and debian/ubuntu
should be 'ntp'.

> if $ntp == 'ntp' {
>   service { 'ntp':
>     name       => $ntp,
>     ensure     => running,
>     enable     => true,
>     hasrestart => true,
>     hasstatus  => true,
>   }}
>
> else {
>     service { 'ntpd':
>       name       => $ntp,
>       ensure     => running,
>       enable     => true,
>       hasstatus  => true,
>       hasrestart => true,
>     }
>   }

This conditional is redundant. I think it's better to use one resource
name and change the "name" attribute with the variable as you have
done (although I would a more explicit variable name like
'$ntp_service'):

service { 'ntp':
name => $ntp_service,
...
}


> err: /Stage[main]//Service[ntp]/ensure: change from stopped to running
> failed: Could not start Service[ntp]: Execution of '/sbin/service ntp
> start' returned 1:  at /root/learning-manifests/ntp.pp:24

This is because of the reversal I mentioned above; it should be 'ntpd'
on CentOS, not 'ntp'.

> When I run the vanilla script, everything configures correctly and
> ntp(d) is started. Why is my second script using /sbin when it seems
> that it should be using /etc/init.d/?

Have you tried 'man service'? '/sbin/service' is a way of running init
scripts with a sanitized environment and is the preferred way of
running init scripts rather than doing so directly.

http://linux.die.net/man/8/service

Jesse

unread,
Apr 17, 2012, 4:00:18 PM4/17/12
to puppet...@googlegroups.com

On Tuesday, April 17, 2012 1:34:02 AM UTC-4, Wil Cooley wrote:
On Apr 15, 4:44 pm, Jesse <anonym...@gmail.com> wrote:

> case $operatingsystem {
>     centos, redhat: { $ntp = "ntp" }
>     debain, ubuntu: { $ntp = "ntpd" }

This is backwards; centos/redhat should be 'ntpd' and debian/ubuntu
should be 'ntp'.


After spending over an hour trying to figure out my mistake, of course I notice the errant "d" after I post to the group. Thanks for confirming!

 
> if $ntp == 'ntp' {
>   service { 'ntp':
>     name       => $ntp,
>     ensure     => running,
>     enable     => true,
>     hasrestart => true,
>     hasstatus  => true,
>   }}
>
> else {
>     service { 'ntpd':
>       name       => $ntp,
>       ensure     => running,
>       enable     => true,
>       hasstatus  => true,
>       hasrestart => true,
>     }
>   }

This conditional is redundant. I think it's better to use one resource
name and change the "name" attribute with the variable as you have
done (although I would a more explicit variable name like
'$ntp_service'):

service { 'ntp':
  name => $ntp_service,
  ...
}

 
Thank you, I"ll use that idea in my next manifest. 

 
> err: /Stage[main]//Service[ntp]/ensure: change from stopped to running
> failed: Could not start Service[ntp]: Execution of '/sbin/service ntp
> start' returned 1:  at /root/learning-manifests/ntp.pp:24

This is because of the reversal I mentioned above; it should be 'ntpd'
on CentOS, not 'ntp'.

> When I run the vanilla script, everything configures correctly and
> ntp(d) is started. Why is my second script using /sbin when it seems
> that it should be using /etc/init.d/?

Have you tried 'man service'? '/sbin/service' is a way of running init
scripts with a sanitized environment and is the preferred way of
running init scripts rather than doing so directly.

http://linux.die.net/man/8/service

I'll read up on this as well. I've created another manifest based on the lesson plan, this time for an Apache install that is OS sensitive, with a custom 404 and a generic landing page. I think this part is wrong:

file { '$webservice_name.conf':
      path    => "/etc/${webservice_name}/conf/${webservice_name}.conf",

Since in Debian/Ubuntu it would be

file { '$webservice_name.conf':
      path    => "/etc/${webservice_name}/${webservice_name}.conf",

but I'm not sure how to parse that out. Here's it in entirety (Debian side commented out as per the Puppetlabs instructions to test only for EL/CentOS or fail):

#init.pp
class httpd {
    case $operatingsystem {
      centos, redhat: {
        $webservice_name = 'httpd'
        $conf_file    = '/etc/httpd/conf/httpd.conf'
        $docu_root    = '/var/www/html'
        $404page      = '404page.redhat.centos.html'
      }
      #      debian, ubuntu {
      # $webservice_name = 'apache2'
      # $conf_file    = '/etc/apache2/apache2.conf'
      # $docu_root    = /var/www/'
      # $404page      = '404page.debian.ubuntu.html'
      #}
      default: { fail("Uncrecognized operatingsystem for webserver")}
      }
    package { '$webservice_name'
      ensure => installed,
    }
    file { '$webservice_name.conf':
      path    => "/etc/${webservice_name}/conf/${webservice_name}.conf",
      source  => "puppet:///modules/apache2/${conf_file}",
      ensure  => file,
      require => Package['$service_name'],
    }
    service { 'ntp':
      name      => $service_name,
      ensure    => running,
      enable    => true,
      subscribe => File['${webservice_name.conf}'],
    }
    file { '404page.html':
      path    => '$docu_root/404page.html
      source  => "puppet:///modules/apache2/${404page}",
      ensure  => file,
      require => Package["$webservice_name"],
    }
}

I figured I could start another topic, but I didn't want to clutter the board.

Cheers!


Jesse

Jesse

unread,
Apr 17, 2012, 4:06:03 PM4/17/12
to puppet...@googlegroups.com
Ooops, forgot the default page:

 file { 'default.html':
      path   => "${docu_root}/default.html",
      source => "puppet:///modules/apache2/default.html",
Reply all
Reply to author
Forward
0 new messages