0

I create a cookbook for copying some folders to others. In template, I wrote linux commands like:

cp -r [some path] [some path]
cp -r [some path] [some path]
cp -r [some path] [some path]
...

But the last command copy files with specific extension .html. So I wrote command like this:

cp [some path]/*.html [some path]

Everything works fine but I have to wrote some if statement to the last command because there are cases, when there are no html files in this directory. How to write it in my template?

pulpet112
  • 43
  • 2
  • 10

1 Answers1

-1

You can use if statement.

Code wrapped in <% %> or <% -%> is a statement that is evaluated.

So you can write something like this:

<% if File.exist?('filename') -%>
  # do something
<% end -%>
Farkhat Mikhalko
  • 3,565
  • 3
  • 23
  • 37
  • @pulpet112 can you specify which extension of your template? – Farkhat Mikhalko Jul 20 '16 at 08:45
  • Sure. It's .erb file – pulpet112 Jul 20 '16 at 08:48
  • @pulpet112 yes you can use this constructions in your file `<% %>` – Farkhat Mikhalko Jul 20 '16 at 08:49
  • Do not do this in a Chef template! It's a _template_! – StephenKing Jul 20 '16 at 08:51
  • @farhatmihalko Great! And the last question: my path to this directory is wrote in attributes. For instance, i have in attribute file this line: default['temp']['path'] = "/folder1/folder2" And there, I'm looking for html files. So how can I replace 'filename' in your example? It could be like that?? <% if File.exist?(node['temp']['path']/*.html) -%> cp <%= node['temp']['path']%>/*.html node[some path] <% end -%> – pulpet112 Jul 20 '16 at 08:54
  • @StephenKing - why? – pulpet112 Jul 20 '16 at 08:54
  • @StephenKing ok, i think you're right. I wrote this if statement in recipe, not in template. It almost works. In the if condition, i put sth like this: "#{node[......]}/*.html" but the value of this condition is always false. What is wrong?? – pulpet112 Jul 20 '16 at 09:52
  • Are the files put there during the chef run? Then this is because the `if` condition is evaluated earlier. Read more about that here in [@coderanger's blog](https://coderanger.net/two-pass/). – StephenKing Jul 20 '16 at 11:46
  • Bad idea in a template because separation of concerns. No logic in templates. – StephenKing Jul 20 '16 at 11:46
  • @StephenKing of course this files are there, even before running the whole chef cookbok. It's interesting, that when i put a real filename instead of " * ", for example "#{node[..]}/aaa.html, everything is fine and the if condition have true state when this aaa.html file exists and false if not. So the problem is only with " * " – pulpet112 Jul 20 '16 at 12:27
  • @pulpet112 try (untested) `if !Dir.glob("#{node['temp']['path']}/*.html").empty?` instead of `File.exists?` File as it name implies works with 1 file... But really you're doing something weird with those cp calls within a template, if you say a word about what you're trying to achieve we may probably offer better alternatives. – Tensibai Jul 20 '16 at 15:45