On Friday, June 21, 2013 1:04:36 AM UTC-5, Roman Alekseev wrote:
>
> Is the module below correct?
>
> class nginx {
>
> if defined(Package['nginx']) {
>         file { "/tmp/puppet-nginx.sh":
>                         ensure          => "present",
>                         owner           => "root",
>                         group           => "root",
>                         mode            => 0755,
>                         source          => 
> "puppet://$puppetserver/modules/nginx/puppet-nginx.sh",
>                         require         => Package['nginx'],
>                 }
>
> }



Do not use defined().  Ever.

There are two possible approaches here.  The better is to determine based 
on node facts and/or external data whether nginx is *supposed* to be 
installed, and then to include class 'nginx' only if so.  In that case, the 
class might look like this:

class nginx {

  # ensure the package installed
  package { 'nginx':
    ensure => 'installed'
  }

  # ensure the target file in place
  file { "/tmp/puppet-nginx.sh":
    ensure => 'file',
    owner => 'root',
    group => 'root',
    mode => 0755,
    source => 'puppet:///modules/nginx/puppet-nginx.sh'
  }
}

Note that if the file were supposed to replace one that the package 
provides (e.g. a config file) then you would also want to declare a 
relationship between the two to ensure that the package was managed before 
the file.  One way to declare that would be:

Package['nginx'] => File['/tmp/puppet-nginx.sh']

Note also that a fully-featured nginx module would probably manage the 
nginx configuration file and service as well.


The alternative is what you are attempting: to detect whether nginx is 
already installed, and to manage the target file only in that case.  For 
that, you do indeed want a custom fact to communicate to the master whether 
nginx is installed.  If you do it that way, then as Matthew said, the 
defined() doesn't do anything useful for you.  In fact, it is more likely 
harmful than useful.  Instead, you might write your class like this:

class nginx {
  if $::nginx_is_installed == 'true' {
    # ensure the target file in place
    file { "/tmp/puppet-nginx.sh":
      ensure => 'file',
      owner => 'root',
      group => 'root',
      mode => 0755,
      source => 'puppet:///modules/nginx/puppet-nginx.sh'
    }
  }
}

You can apply that to all nodes.  Do note that all facts are strings.  You 
custom fact must be written that way, and any test you perform on the fact 
value must accommodate it.


John

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to puppet-users+unsubscr...@googlegroups.com.
To post to this group, send email to puppet-users@googlegroups.com.
Visit this group at http://groups.google.com/group/puppet-users.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to