A collection of task oriented solutions in Puppet

 

You want to create a directory

Challenge

You want to create a new directory

Solution

class directories {

  # create a directory      
  file { '/etc/site-conf':
    ensure => 'directory',
  }

  # a fuller example, including permissions and ownership
  file { '/var/log/admin-app-log':
    ensure => 'directory',
    owner  => 'root',
    group  => 'wheel',
    mode   => '0750',
  }

  # this example is incorrect and creates a file
  file { '/etc/site-conf/':
    ensure => 'present',
  }
}

Explanation

Creating a directory is another task that is handled by the powerful and flexible file type. In this case specifying a directory is done by setting ensure to (unsurprisingly) directory. The third example is a (semi-)common mistake made by people coming from certain other tools. Adding a / to the end of a filename isn't enough to tell puppet to make a directory and so it creates a file instead, and silently drops the / off the end of the name.

You can use a number of the parameters from the file type to control the directories properties, such as the owner, group and permissions.

It's also worth noting that puppet will not create missing parent directories. There is a long standing bug regarding this feature request "Directory creation fails if parent directory does not exist" and while there are a couple of workarounds there's no official fix.

See also