Puppet 模板 if 定义语法

Puppet 模板 if 定义语法

我在 Puppet 文档中找不到解决方案。我试图在 Puppet 模板中定义一个变量。例如:

如果class graylog2apache在节点类部分定义,则模板会将这些变量放入配置文件中:

# 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 ~

例子:

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

我不确定,但我猜示例版本不会起作用。另外我如何才能@server_gl从中获取graylog2 module它,这样它就不必在中定义syslog module

答案1

解决方案:

# 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 %>

相关内容