2

I want to check "if dir exists" condition with puppet how it done here How to do a file/dir exists conditional in Puppet? but then if i write

  exec { 'check_presence':
    command => "true",
    path    =>  ["/usr/bin","/usr/sbin", "/bin"],
    onlyif  => "test -d /test"
  }
  file { '/test/123':
    ensure  => directory,
    mode    => '0644',
    require => Exec['check_presence']
  }

from exec i get alwyas true, and puppet always try to create /test/123

Debug: Exec[check_presence](provider=posix): Executing check 'test -e /test'
Debug: Executing 'test -e /test'

Error: Cannot create /test/123; parent directory /test does not exist
Error: /Stage[main]/tst::test/File[/test]/ensure: change from absent to directory failed: Cannot create /test/123; parent directory /test does not exist

Why he do it?! And how i must check parent directory exists?!

My puppet is 3.8

UPD I don't want to create /test ! I want to create /test/123 ONLY if /test already exists!

2
1

puppet doesn't have one native function for this, the only workaround:

mkdir -p your_module_path/lib/puppet/parser/functions/ now you need to create this file directory_exists.rb with this code:

require 'puppet'

module Puppet::Parser::Functions
  newfunction(:directory_exists, :type => :rvalue) do |args|
    if File.directory?(args[0])
      return true
    else
      return false
    end
  end
end

Now in your puppet code, you can use the function:

if directory_exists('/test') {
  file { '/test/123':
    ensure  => directory,
    mode    => '0644',
    owner   => 'root',
    group   => 'root',
  }
}
7
  • Could you please, where's i can find my_module_path? :) – user423253 Jul 3 '17 at 15:44
  • that was an example, We have a module called profile and the path in my puppet server is "/etc/puppetlabs/code/environments/production/modules/profiles/lib/puppet/parser/functions/directory_exists.rb" – c4f4t0r Jul 3 '17 at 15:51
  • Thanx you too! But in my case, i must return boolean (true/false) from func, or initialize var $var = directory_exists('/test') and then compare it if $var == 1 { ... } – user423253 Jul 4 '17 at 5:22
  • @user423253 have you solved your problem? – c4f4t0r Jul 4 '17 at 7:46
  • 2
    Functions run on the master, so this would only work if the code was being run in apply mode, not in agent mode – Peter Souter Jul 4 '17 at 13:38
1

If you don't want to create /test itself I believe you'll have to do both things in an exec:

exec { '/usr/bin/install /test/123':
  onlyif  => "/usr/bin/test -d /test"
}

Using install rather than mkdir enables setting properties like mode in the same command.


The easiest way to create parent directories is to chain files:

file { '/test':
  ensure  => directory,
  mode    => '0644',
} ->
file { '/test/123':
  ensure  => directory,
  mode    => '0644',
}

The semantics of your code simply requires the exec to "be instantiated" (that is, run) before the child directory is created. There is no optionality implied by this - true will run if /test exists, otherwise true does not run.

4
  • But i don't want to create /test ! I want create /test/123 ONLY if /test already exists! – user423253 Jul 3 '17 at 14:46
  • in this case, where i must set owner, group, etc? – user423253 Jul 3 '17 at 14:51
  • Ok, it's work, many thanx you! But question is actual: Why puppet always return true in my case ? – user423253 Jul 3 '17 at 15:07
  • @user423253 I missed owner in the resource definition, anyway, you can put what you want – c4f4t0r Jul 4 '17 at 15:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.