Ask Your Question
0

puppet package resource with conditional

asked 2016-11-17 15:58:23 -0500

kumaresang gravatar image

Wanted to write a puppet package resource for checking rsyslog5 package or rsyslog package if both were not installed then install rsyslog. Any idea?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2016-11-18 04:46:42 -0500

this is not really a good thing to do - puppet describes the end state of a set of things - shouldnt really be dependent on existing state - however an interesting exercise!

as ever there are multiple ways to do this (some with bad examples of exec) - but lets try and avoid that.. one way I'd do it is by creating a custom fact for your module - so in modulehome/lib/facter create a rsyslog.rb file, also I'm being lazy and assuming this is an rpm based system, change as appropriate:

require 'facter'

result = %x{/bin/rpm -q --quiet --queryformat "%{NAME}" rsyslog5 }

Facter.add('rsyslog') do
    setcode do
        result
    end
end

the above fact just checks if you have rsyslog5 installed, exits silently if not; can test on command line like:

export FACTERLIB=modulehome/lib/facter:$FACTERLIB
facter rsyslog

next we can alter your manifest to install rsyslog only if rsyslog5 isnt installed (if rsyslog is installed it wont reinstall it)

  if $::rsyslog {
    notify{'rsyslog5 installed':}
  }
 else {
   package { 'rsyslog':
                ensure => installed,
           }

enjoy!

Stuart

edit flag offensive delete link more
0

answered 2016-11-20 07:12:45 -0500

stivesso gravatar image

I've had a similar need in the past and also came up with a set of Custom Facts , I've uploaded these Custom Facts on GitHub and you can find them here on Github (feel free to modify and reshare...) To use these Custom facts, proceed as described by @sahumphries (basically copy under modulehome/lib/facter and trigger a convergence). Below is examples of what you're getting on a Node that has rsyslogd version 5.8.10 (note that the syslogdaemoninstalled facts is a hash because we may have multiple syslog -rsyslogd, syslog-ng...- packages installed on the same system. But the running one is only the current running daemon on the system.
The second example shows what we've on another node with a syslog-ng Package installed and running version 2.0.9

# facter -p syslog_running
rsyslogd
# facter -p syslog_running_version
5.8.10
# facter -p syslog_daemon_installed
{
  syslog-ng => "0",
  rsyslogd => "1",
  syslogd => "0"
}

Second Example which shows syslog-ng Package installed and running version 2.0.9:

# facter -p syslog_running  
syslog-ng
# facter -p syslog_running_version
2.0.9
# facter -p syslog_daemon_installed
{
  syslog-ng => "1",
  rsyslogd => "0",
  syslogd => "0"
}
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2016-11-17 15:58:23 -0500

Seen: 201 times

Last updated: Nov 20 '16