1

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.

1

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 %>
6
  • Is $classes defined in your manifest? Is it actually hiera_array("classes")? – Felix Frank Oct 2 '14 at 19:37
  • $classes is defined in my manifest. – mYzk Oct 10 '14 at 6:26
  • Okay, then this is probably a safe thing to do. Do use @classes rather than classes in your templates. – Felix Frank Oct 10 '14 at 8:28
  • I use classes in my templates – mYzk Oct 13 '14 at 14:38
  • I can see that. You should prefix Puppet variables with an @ in your templates, though. Use without @ is deprecated and will no longer work in Puppet 4.x. – Felix Frank Oct 13 '14 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.