如何根据人偶数组内容生成文件?

How to generate files based on puppet array contents?

我希望能够根据数组的内容创建文件。这是我尝试过的:

class make_files (
    $servers = ['server-1','server-2','server-3'],
) {
    file { $servers :
        path    => '/tmp',
        content => $servers,
    }
}

...但是当我这样执行时...

# puppet apply --modulepath=/root/modules -e 'include make_files'

...我得到这个输出:

Notice: Compiled catalog for host-001 in environment production in 0.13 seconds
Error: Cannot alias File[server-2] to ["/tmp"] at /root/modules/make_files/manifests/init.pp:9; resource ["File", "/tmp"] already declared at /root/modules/make_files/manifests/init.pp:9

我希望有三个文件...

# ls /tmp/server-?
/tmp/server-1  /tmp/server-2  /tmp/server-3
# cat /tmp/server-?
server-1
server-2
server-3

如果有人能告诉我如何获得要命名的文件,那就太好了:server-1.cfg、server-2.cfg 和 server-3.cfg。 感谢任何帮助!

首先,您已经在 init.pp 文件中定义了资源 File['/tmp']。 这是您遇到问题的主要原因:

Error: Cannot alias File[server-2] to ["/tmp"] at /root/modules/make_files/manifests/init.pp:9; resource ["File", "/tmp"] already declared at /root/modules/make_files/manifests/init.pp:9

请检查 path 属性的含义。也许这会澄清问题。 创建

$files_to_create = ['/path/server-1','/path/server-2','/path/server-3']

然后使用它:

    file { $files_to_create :
        content => $servers,
    }

另请确保 /path 目录已存在于文件系统中。到目前为止,人偶无法创建中间目录。