5

I am trying to write the module where it creates the file if directory exists or else it shouldn't do anything.

class puppetmodule{
  exec { 'chk_dir_exists':
    command => 'test -d /usr/dir1',
    path    =>  ["/usr/bin","/usr/sbin", "/bin"],
  } ->

  file {'usr/dir1/test.txt':
    ensure => 'file',
    owner  => 'root',
    group  => 'root',
    mode   => '0750',
  }
}

Below is the error it is throwing. Please advice me on this.

Error: test -d /usr/dir1 returned 1 instead of one of [0]

  • Why not create the directory and then create the file, instead of checking if it exists first? – gWaldo May 6 '16 at 16:46
  • This directory is not present in all the servers. – joe May 6 '16 at 18:58
3

Something like this will work:

  $dir = "/usr/dir1"

  exec { "chk_${dir}_exist":
    command => "true",
    path    =>  ["/usr/bin","/usr/sbin", "/bin"],
    onlyif  => "test -d ${dir}"
  }

  file {"${dir}/test.txt":
    ensure => file,
    owner  => 'root',
    group  => 'root',
    mode   => '0750',
    require => Exec["chk_${dir}_exist"],
  }

Explanation:

onlyif => "test -d ${dir}"

means that the Exec resource is only created if the output of test -d is true.

require => Exec["chk_${dir}_exist"]

means the File resource is created only if the Exec resource exists.

If the directory does not exist, the puppet run will generate an error indicating that it cannot create File resource because the Exec resource does not exist. This is expected and can be safely ignored as the rest of the puppet catalog still gets applied.

If the directory exists, the File resource is created and applied.

  • This does not work for me using puppet 3.8 – Dejan Jun 14 at 19:49
  • The require attribute is no longer present. – Dejan Jun 17 at 18:35
0

Puppet is about end state. You can ensure a file exists with the state you specify or is absent. If you need to do some branching (if) logic, Puppet supports that as well. See conditionals in the documentation - https://docs.puppet.com/puppet/latest/reference/lang_conditional.html

$directory_exists = <insert logic here> 

if $directory_exists {
  file {'usr/dir1/test.txt':
    ensure => 'file',
    owner  => 'root',
    group  => 'root',
    mode   => '0750',
  }
}
  • 1
    Hello ferventcoder, Thanks for your response. Please correct me if I am wrong. I tried below code and it Doesn't throw any errors but it doesn't create the file even if directory present. $directory_exists = "(test -d /usr/dir1)" if $directory_exists =='true'{ file {'usr/dir1/test.txt': ensure => 'file', owner => 'root', group => 'root', mode => '0750', } } – joe May 6 '16 at 19:19
  • I think you should use a notify resource to tell you what the value of $directory_exists comes back as. You could be running into possible permissions issues. – ferventcoder May 6 '16 at 19:47

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.