4

I'm trying to make a function which directory/file will be created only when the first directory exists, of not it must be skipped because of failed dependence.

I've tried this "onlyif" workaround, but unfortunately it doesn't work with my function.

$check_directory = file("/path/to/directory")
if($check_directory != '') {
    file{"/path/to/config":
        ensure  =>  directory,
        mode    =>  0755,
    }
    file{"/path/to/config/a.conf":
        ensure  =>  file,
        mode    =>  0755,
        content =>  template("config_template.conf"),
    }
}

I've got an error:

Error: Is a directory - /path/to/directory

Is there a way to do any other if statement? Or any parameter? Thank you.

skantana
  • 41
  • 1
  • 1
  • 5

3 Answers3

2

You should be able to simply use a require statement in your a.conf file resource:

file{"/path/to/directory":
    ensure  =>  directory,
    mode    =>  0755,
}

file{"/path/to/config":
    ensure  =>  directory,
    mode    =>  0755,
}
file{"/path/to/config/a.conf":
    ensure  =>  file,
    mode    =>  0755,
    content =>  template("config_template.conf"),
    require => File["/path/to/directory"],
}

This will make sure that the directory will be created before the file.

Sven
  • 98,649
  • 14
  • 180
  • 226
  • Thanks for the answer, but it's difference directory. I would like to check another path to configuration, not the one that will be created. if I do so, I will get .. Error: Could not find dependency – skantana Sep 16 '15 at 12:48
  • Then you have to add a file resource for that new directory and then use `require` with that resource. See my edited answer. – Sven Sep 16 '15 at 13:04
  • Directory/file will be created only when the first directory exists, of not it must be skipped because of failed dependence. – skantana Sep 16 '15 at 13:27
  • Sorry, now I got it. But the sample code you wrote doesn't use the `onlyif` trick you linked to. So I am still confused - what exactly did you try and what didn't work. Please read [How to ask better questions on Serverfault](http://meta.serverfault.com/questions/3608/how-can-i-ask-better-questions-on-server-fault) to avoid wasting your and our time. – Sven Sep 16 '15 at 14:52
0

Seems to work fine with find_file; hat tip to this blog post that led me in the right direction.

$check_directory = find_file("/path/to/directory/.")
if $check_directory {
    file{"/path/to/config":
        ensure  =>  directory,
        mode    =>  0755,
    }
    file{"/path/to/config/a.conf":
        ensure  =>  file,
        mode    =>  0755,
        content =>  template("config_template.conf"),
    }
}

$check_directory will be undef if the directory isn't present, so that evaluates to false.

Note the addition of the trailing /. which causes the check to fail (and the code not to be run) in the case that /path/to/directory exists but is a FILE rather than a directory.

Wildcard
  • 152
  • 2
  • 14
0

The ethos of @Sven's answer is absolutely correct: you set up resource dependencies in Puppet using before, require, notify, etc. The only thing I would change is I would use the defined() function to test:

file { '/tmp/foo':
  ensure => 'directory',
  mode   => '0755',
}
if defined(File['/tmp/foo']) {
  notice("/tmp/foo is defined! making /tmp/bar/baz")

  file { '/tmp/bar':
    ensure => 'directory',
    mode   => '0755',
  }

  file { '/tmp/bar/baz':
    ensure  => 'present',
    mode    => '0755',
    require => File['/tmp/bar'],
  }
}

Two other useful tidbits:

  • If you enable the "Future Parser" in Puppet 3.2+, you can access resource attributes like so: $a = File['/tmp/foo']['ensure']. This would store the value of the ensure attribute for the /tmp/foo file resource in the variable $a. If you decide to look into this, I would rewrite the above example to use if defined(File['/tmp/foo']) and File['/tmp/foo']['ensure'] == 'directory' (UNTESTED).

  • If you're passing these items as parameters to a class, there is a sneaky way to access the values of class parameters via in-puppet-how-can-i-access-a-variable-attribute-inside-a-defined-type.

thankyour
  • 126
  • 3