Ask Your Question
0

puppet if statement base on exec - can variables set in exec environment be accessed elsewhere in the same class

asked 2017-07-28 02:21:57 -0600

Scott gravatar image

I'm trying to configure a logging figuration with log paths based on a check to see if that log file exists. However, the if statements always return true even though I know /var/log/hermes/hermes.log doesn't exist. I have tested the exec {"servicelogcheck" onlyif is working so it's something to do with the if statement. I had thought about adding within each exec something like environment => ["FOO=true" and then changing the if statement to if $FOO == true but it appears that variables set within exec environment are only accessible within the exec not the whole class. It's driving me crazy!!! We are using puppet v3.7

class hermes::fluentd($service_name='hermes', $logs='/var/log/test/test.log') {

exec {"service_log_check":
  command => '/bin/true',
  onlyif => '/usr/bin/test -e /var/log/hermes/hermes.log',
}

exec {"service_log_json_check":
  command => '/bin/true',
  onlyif => '/usr/bin/test -e /var/log/hermes/hermes-json.log',
}

exec {"request_log_check":
  command => '/bin/true',
  onlyif => '/usr/bin/test -e /var/log/hermes/request.log',
}

if service_log_check {
    $service_logs = "/var/log/${service_name}/${service_name}.log"
}
else { $service_logs = []
}

if service_log_json_check {
    $service_json_logs = "/var/log/${service_name}/${service_name}-json.log"
}
else { $service_json_logs = []
}

if request_log_check {
    $service_request_logs = "/var/log/${service_name}/request.log"
}
else
{ $service_request_logs = []
}

$logs_to_tail = [$logs, $service_logs, $service_json_logs, $service_request_logs]


file { "/etc/td-agent/config.d/${service_name}_service_logs.conf":
    ensure => present,
    content => template('service_logs.conf.erb'),
    owner  => td-agent,
    group  => td-agent,
    mode   => '0775'
}

}

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

answered 2017-07-29 07:08:44 -0600

if service_log_check {
[…]
if service_log_json_check {
[…]
if request_log_check {

You can't use exec resources like this. Puppet is not a procedural programming language. It is a declarative language. During catalog compilation, everything is evaluated – in advance – nothing gets executed at that stage. Then the finished catalog is sent to the client, which then “executes” it. So the puppet master – the place at which your Puppet code is being evaluated – cannot tell, whether a file exists on the client.

You have to approach your situation differently. Since I don't know, how and where $logs_to_tail is used, I can't really give you any advise with that.

PS: Also update your Puppet version. This might ease up further assistance.

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: 2017-07-28 02:21:57 -0600

Seen: 139 times

Last updated: Jul 29