我的 hiera 文件中有这种结构,它在我的 smb.conf 中创建了一些规则。
samba::shares:
PDF:
- comment = "PDF"
- path = /home/smb/pdf
- browseable = yes
- hide dot files = yes
- read only = no
- public = yes
- writable = yes
- create mode = 0775
- printable = no
Partage:
- comment= "Partage"
- path = /home/smb/Partage
- browseable = yes
- hide dot files = yes
- read only = no
- public = yes
- writable = yes
- create mode = 0775
- printable = no
我想访问每个共享的路径,以便自动创建具有正确权限的文件夹。
我尝试了许多使用 hiera_hash() 的解决方案,但找不到正确的设置。:
define create_folder{
# I want to loop on PDF, Partage, etc. and extract path
# for each one (/home/smb/pdf, /home/smb/Partage, etc.).
$path = hiera_hash('path')
file{"$path":
path => $path,
ensure => diretory,
owner => "smb",
group => "smb",
require => File["/home/smb/"],
mode => '775',
}
}
你能帮我吗 ?
问候。
答案1
这是一个应用程序create_resources
方法。
就您而言,您不会从资源内部获取哈希,而是从外部获取:
# puppet/site.pp
create_resources(create_folder, hiera_hash('samba::shares', {}))
# puppet/modules/...
define create_folder(
$comment,
$path,
$browseable,
$hide_dot_files,
$read_only,
$public,
$writable,
$create mode,
$printable,
){
file{"$path":
path => $path,
ensure => diretory,
owner => "smb",
group => "smb",
require => File["/home/smb/"],
mode => '775',
}
}
第二个参数是hiera_hash
默认值。我更喜欢始终保留一个空的哈希,以便能够在所有机器上加载资源,即使只有某些机器定义了值。
答案2
最后,我添加了一个每次 Puppet 运行时都会运行的脚本:
#!/bin/bash
folders=($(cat /etc/samba/smb.conf | grep path | cut -d" " -f3))
for folder in "${folders[@]}" ; do
if [ ! -d "$folder" ]; then
mkdir -p "$dossier"
chown -R smb:smb "$folder"
chmod -R 775 "$folder"
fi
done
问候。