Samba – Get value from hiera hash in Puppet

hierapuppetsamba

I have this structure in my hiera file that create some rules in my 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

I want to access the path of each share for create automatically the folder with the right persmissions.

I tried many solutions with hiera_hash() but I can not find the right setting. :

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',
  }
}

Can you help me please ?

Regards.

Best Answer

This is an application for the create_resources method.

In your case, you would not fetch the hash from inside the resource, but from outside:

# 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',
  }
}

The 2nd argument to hiera_hash is a default value. I prefer to always keep an empty Hash there in order to be able to load the resource on all machines, even if only certain machines have the values defined.