This message was deleted.
# puppet
s
This message was deleted.
y
I want to say, "Add `recurse => true`" to your file resource
p
Perhaps they’ve added recurse since then
I just tried this (with a hard coded path):
Copy code
file { '/some/long/path/with/parents/that/do/not/exist/on/disk/yet':
      ensure => 'directory',
      owner   => 'tomcat',
      group   => 'tomcat',
      recurse => true,
    }
And the Puppet agent comes back with:
Copy code
Info: Applying configuration version 'rhs-dev-cf877fee152'
Error: Cannot create /some/long/path/with/parents/that/do/not/exist/on/disk/yet; parent directory /some/long/path/with/parents/that/do/not/exist/on/disk does not exist
Error: /Stage[main]/Dspace_modern/File[/some/long/path/with/parents/that/do/not/exist/on/disk/yet]/ensure: change from 'absent' to 'directory' failed: Cannot create /some/long/path/with/parents/that/do/not/exist/on/disk/yet; parent directory /some/long/path/with/parents/that/do/not/exist/on/disk does not exist
s
We have a defined type
parent_dirs
that uses
reduce
, looking up parameters from Hiera so we can make sure that any common path elements get the same ownership/permissions regardless of where
parent_dirs
is called from.
Copy code
define parent_dirs (
  Hash $params = {
    'ensure' => 'directory',
    'owner'  => 'root',
    'group'  => 'root',
    'mode'   => '0755',
  },
) {
  $parent_dirs_params = lookup('parent_dirs::params',Hash[String,Hash],undef,{})
  $title.split('/').reduce([]) | $sub_path,$d | {
    $new_path = $sub_path + $d
    $new_path_join = $new_path.join('/')
    if $new_path_join =~ /^\// {
      # Use the params hash as defaults if any values aren't set in parent_dirs::params for a particular path
      ensure_resource('file',$new_path_join,($params + pick($parent_dirs_params[$new_path_join],{})))
    }
    $new_path
  }
}
p
Thanks @Skylar Thompson - I take it that’s not in Puppet core or a module etc. That’s something you’ve defined for your site ?
s
Yeah, it's custom for us
p
As an aside, I’m not sure that “recurse => true” actually creates missing parents. It doesn’t seem to in my quick test
y
Basically, loop through the directory tree elements, split by
/
and
ensure => directory
on each element.
p
Hmm…..
Thanks all for your help - does make it difficult though. Wonder why they haven’t implemented the equivalent of “mkdir -p”
s
It does cut a bit against the roles&profiles grain but OTOH is handy for a sysadmin
p
Right - yeah, very much approaching things from a sysadmin point of view - so there is likely things I’m not aware of 🙂
p
Thanks @kenyon - Looks useful, will look into this
b
I use https://forge.puppet.com/modules/pltraining/dirtree to create my parent directories.