Puppet-使用文件创建命令传递变量

Puppet-使用文件创建命令传递变量

我需要一种方法来将给定变量(例如 thearch)传递给给定类中的几个不同文件。我需要能够为每个文件单独声明此变量的内容。

我尝试了以下方法:

file { "xxx":
  thearch => "i386",
  path    => "/xxx/yyyy",
  owner   => root,
  group   => root,
  mode    => 644,
  content => template("module/test.erb"),
}

这没有传递这个变量,所以我可以像我预期的那样在 erb 文件中将它与 <%=thearch%> 语句一起使用。

我在这里做错了什么?

答案1

您需要将文件包装在接受该参数的定义中,以便在调用模板时可以使用该参数,然后调用该定义。如果许多参数通常相同,则将它们设置为默认值,以保持代码整洁。

define thearch_file($thearch, $path, $owner = root, $group = root, $mode = 0644, $template = '/module/test.erb') {
  file { $name:
    path    => $path,
    owner   => $owner,
    group   => $group,
    mode    => $mode,
    content => template($template),
  }
}

thearch_file {
  "xxx":
    thearch => 'i386',
    path    => "/xxx/yyy";
  "yyy":
    thearch => 'x86_64',
    path    => "/xxx/zzz";
}

答案2

您无法为文件资源定义任意元参数,例如“thearch”。唯一可用的元参数是这里。您可以使用节点中现有的架构事实,这可能会为您提供所需的功能。

<%= architecture %>

也许

<% if architecture == 'i386' then -%>
  do some stuff
<% end-%>

答案3

您不能这样做。资源的参数不是任意值。不过,您可以这样做:

thearch = "i386"
file { "xxx":
  path    => "/xxx/yyyy",
  owner   => root,
  group   => root,
  mode    => 644,
  content => template("module/test.erb"),
}

相关内容