大网数据广告活动
服务器运维问答

Copy entire template folder from puppet module

· 52CE 我爱测

I'm looking for the syntax to copy an entire templates directory inside a puppet module. (Eg: templates/webconfig/file1.erb, templates/webconfig/config/file2.erb) I tried to copy in below syntax: file {"$http_home_dir/webconfig":…

问题原文

原帖链接 · Satheesh · CC BY-SA 3.0

I'm looking for the syntax to copy an entire templates directory inside a puppet module. (Eg: templates/webconfig/file1.erb, templates/webconfig/config/file2.erb)

I tried to copy in below syntax:

file {"$http_home_dir/webconfig":
                        ensure => "directory",
                        owner => "$http_user",
                        group => "$http_group",
                        content => template("webconfig"),
                        recurse => true,
                        require => File["$http_home_dir"];
                }

It doesn't worked. When i tried to use wildcard like below also, it didn't worked.

content => template("webconfig/*.erb"),

Is there anything specific I'm missing

已采纳解答

原帖链接 · Craig Watson · CC BY-SA 3.0

You can only copy files wholesale using the source parameter, which copies files as-is. The only way you can copy multiple templates is to use multiple file resources.

A way of shortening the amount of code you need is using a define resource type. For example, using Puppet 4 strict typing and the splat operator:

define myclass::webconfig (
  String $file_path,
  Hash   $attributes,
  String $template_path,
) {
  file { $file_path:
    ensure  => file,
    content => template("${template_path}/${name}.erb"),
    *       => $attributes,
  }
}

Which can be used as:

file { $http_home_dir:
  ensure => directory,
  owner  => $http_user,
  group  => $http_group,
}

myclass::webconfig { 'myfile':
  template_path => 'webconfig',
  file_path     => "${http_home_dir}/webconfig",
  attributes    => {
    owner   => $http_user,
    group   => $http_group,
    require => File[$http_home_dir],
  }
}

Which will place a file at $http_dir/webconfig/myfile with the contents of template webconfig/myfile.erb.

You can also pass an array of file names, like so:

$my_files = [
  'myfile',
  'myotherfile',
  'mythirdfile',
  'foobarfozbaz'
]

file { $http_home_dir:
  ensure => directory,
  owner  => $http_user,
  group  => $http_group,
}

myclass::webconfig { $my_files:
  template_path => 'webconfig',
  file_path     => "${http_dir}/webconfig",
  attributes    => {
    owner   => $http_user,
    group   => $http_group,
    require => File[$http_home_dir],
  }
}

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-05-12)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。