Puppet template if defined syntax

puppetunix

I am having trouble finding a solution in Puppet documents. I am trying to define a variable in Puppet template. For example:

If class graylog2 and apache are defined in node class section then the template puts these variables in the config file:

# Apache logging
local5.* @<%= @server_gl -%>:<%= @service_port_gl -%> <- This part has to inserted if defined graylog2 class and apache class in node file
local5.info ~
local5.err ~

# Nginx logging
local4.* @<%= @server_gl -%>:<%= @service_port_gl -%> <- This part has to be inserted if defined graylog2 class and apache class in node file
local4.info ~
local4.err ~

Example:

if defined (Class['apache', 'graylog2'])
    # Nginx logging
    local4.* @<%= @server_gl -%>:<%= @service_port_gl -%>
    local4.info ~
    local4.err 
else
    # Nginx logging
    local4.info ~
    local4.err

I am not 100% sure, but I guess the example version would not work.
Also how could I get @server_gl from graylog2 module so it does not have to be defined in syslog module.

Best Answer

Solution:

# Apache logging
<% if classes.include?("httpd") %>
local5.* @<%= scope.function_hiera(["ls_gelf_host"]) %>:<%= @service_port -%> 
local5.info ~
local5.err ~
<% else %>
local5.info ~
local5.err ~
<% end %>

# Nginx logging
<% if classes.include?("nginx") %>
local4.* @<%= scope.function_hiera(["ls_gelf_host"]) %>:<%= @service_port -%> 
local4.info ~
local4.err ~
<% else %>
local4.info ~
local4.err ~
<% end %>