Tuesday, January 6, 2015

Puppet create a multi-level directory structure

Lets assume that you currently have your only puppet agent and the puppet master.
In this case you can write your code segment in /etc/puppet/manifests/site.pp file itself under default node definition (This will be applied to all nodes if NO matching node definition is found for the given puppet agent hostname).

node default {

        # create a directory
        $dirpath = '/tmp/dbscripts/mysql'
        exec { "create_dir_structure":
                path        => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
                command => "mkdir -p ${dirpath}",
        }

        # create the file
        file {  "/tmp/dbscripts/mysql/hellopuppet.txt":
                ensure  => file,
                content => 'master says hello to puppet agent',
        }


}

This will successfully create the directory structure even if parent directory does not exist.

Note:
If you attempt to create the directory structure as follows,
# create a directory
file { "/tmp/dbscripts/mysql/":
                ensure => "directory",
      }

You will get the following error.
Error: Cannot create /tmp/dbscripts/mysql; parent directory /tmp/dbscripts does not exist

There has been a suggested feature [1] logged in Puppet Labs yet NOT implemented as of puppet version 3.4.3 master/agent. May be there are smart alternatives as above rather than adding an additional feature... :)

[1] http://projects.puppetlabs.com/issues/86
[2] https://github.com/ghoneycutt/puppet-module-common/blob/master/manifests/mkdir_p.pp

No comments:

Post a Comment