0
votes

I'm new to puppet so any help would be appreciated. I'm trying to pass static data into variable apache_version inside Puppet class depending on OS release version. This variable will be later used in .erb template file to configure apache according to version. For now my class looks like this:

class some_class_name () {
  case $::operatingsystem {
    'RedHat' : {
      case $::operatingsystemrelease {
        /^7\..*/ : { }
        /^6\..*/ : { }
      }
    }
  }
}

How can I define inside {} brackets this variable? As for .erb file it should be used like this:

<%- if @apache_version >= '2.4' -%>
    set something
<%- else -%>
     set something else
<%- end -%>

I was aiming for something like this:

class some_class_name () {
  case $::operatingsystem {
    'RedHat' : {
      case $::operatingsystemrelease {
        /^7\..*/ : { $apache_version = '2.4' }
        /^6\..*/ : { $apache_version = '2.2' }
      }
    }
  }
}

but it doesn't work.

2
Works fine for me. What does facter operatingsystem and facter operatingsystemrelease output exactly? Are you including some_class_name? How are you checking the value of $apache_version? Add notify { "Version: ${apache_version}": } perhaps. - Dominic Cleal
facter operatingsystem gives RedHat facter operatingsystemrelease gives 7.2 (at least on system I'm working right now) some_class_name is included in init.pp file correctly. As for checking - I'm not checking it anywhere. Should I? Shouldn't it work like this, that once I assign version to variable it should be used in .rb file? - NRG
The variable can be used in the ERB template if the template() call is in the same scope, but the manifest shown doesn't have a template() call so it's hard to say whether this is correct. - Dominic Cleal
FYI there is no reason to end a regular expression with .* - Matt Schuchard

2 Answers

1
votes

Ok, I've managed to solve the issue. Now I'm using:

if $::operatingsystemrelease =~ /^7\..*/ {
    $apache_version = '2.4'
  } else {
    $apache_version = '2.2'
  }

in params.pp file. With this everything is being configured correctly.

0
votes

It is better to use the $::operatingsystemmajrelease fact here:

  if $::operatingsystemmajrelease == 7 {
    $apache_version = '2.4'
  } else {
    $apache_version = '2.2'
  }

I also think this is a good use-case for Puppet's selector:

$apache_version = $::operatingsystemmajrelease ? { 7 => '2.4', default => '2.2' }