puppetpuppet-enterprisepuppetlabs-apache

Platform Independent Manifest to install and run apache2 or httpd


I need to write a single manifest as install-apache.pp that will install

Below is the code; this works in CentOS but does not work in Ubuntu.

case $facts['os']['name'] {
  'Debian': {
    package { 'apache2':         
      ensure => installed,       
    }            
    service { 'apache2':     
      ensure => running,     
    }
  }
  'RedHat': {
    package { 'httpd' :
      ensure => installed,
    } 
    service { 'httpd':
      ensure => running,
    }
  }
}

So I made some changes as below, but am not sure why it is not working.

case $operatingsystem {
  'Debian': {
    package { 'apache2':         
      ensure => installed,       
    } ->             
    service { 'apache2':     
      ensure => running,     
      enable => true,        
    }
  }
  'RedHat': {
    package { 'httpd' :
      ensure => installed,
    } ->
    service { 'httpd':
      ensure => running,
      enable => true,    
    }
  }
}

Command used to execute:

puppet apply install-apache.pp --logdest /root/output.log


Solution

  • The problem here is that you are making use of the fact $facts['os']['name'] which is assigned the specific operating system of the distribution and not the family of the distribution. That fact will be assigned Ubuntu on Ubuntu and not Debian. The fact needs to be fixed to $facts['os']['family'], which will be assigned Debian on Ubuntu.

    You can also make use of selectors to improve this a bit more in addition to the fix. It is also recommended to construct a dependency of the service on the package in that manifest to ensure proper ordering. Refreshing would also be helpful.

    With those fixes and improvements in mind, your final manifest would look like:

    $web_service = $facts['os']['family'] ? {
      'RedHat' => 'httpd',
      'Debian' => 'apache2',
      default  => fail('Unsupported operating system.'),
    }
    
    package { $web_service:        
      ensure => installed,      
    }            
    ~> service { $web_service:    
      ensure => running,    
      enable => true,       
    }