Skip to content
Advertisement

Puppet exec with command mkdir -p does not fail but does not create folder

I’m running this simple puppet code on Ubuntu 14.04 machines, to create folders according to a comma-separated list of paths:

define local_dirs ($comma_separated_dirs) {
    $folders = split($comma_separated_dirs, ',')

    exec { $folders:
       command => "mkdir -p ${name} && chmod 0777 ${name}"
    }
}

This resource definition is used as follows (in this case – just a single folder, no commas, other modules do need this “split” logic):

local_dirs { 'create-dir': 
   comma_separated_dirs => "/data/disk1"
}

When I run it, I see the following log line, indicating that the exec command was actually executed:

Notice: /Stage[main]/My_module/Local_dirs[create-dir]/Exec[/data/disk1]/returns: executed successfully

However, the folder was never created, running ls -la /data shows there’s no such folder.

Some more facts worth noting:

  • Puppet runs as root so it’s not likely a permission issue
  • Puppet version is 3.7.2

Please advise on what could be the issue here, or – if there are other ways to create an arbitrary number of arbitrarily-deep folder structures using Puppet – I’ll gladly replace the exec command with anything equivalent. I’m not using puppet’s file resource due to it’s inability to automatically create the necessary parent directories (see here)

EDIT: I’m specifically interested in a way that would spare me any coding of each folder as an array of folders following the hierarchy from root (e.g. ['/data', '/data/disk1', '/data/disk1/user1']).

Advertisement

Answer

You have the wrong expectation for the value of the $name variable. You are assuming that when you use it in a resource declaration …

exec { $folders:
   command => "mkdir -p ${name} && chmod 0777 ${name}"
}

… that it represents the name of the resource being declared. This is not the case. It means the same thing inside that declaration as it does outside: the name / title of the instance of the defined type in which the declaration is being evaluated (the local_dirs instance, whose name is apparently 'create-dirs'). The mkdir is therefore creating a directory of that name in whatever Puppet’s working directory happens to be.

You could solve the problem by interposing another level of defined type to wrap your Exec. If you’re willing to enable the future parser, however, then the forward-looking direction would be to use the new each() function instead of an array title to declare your multiple Exec resources.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement