[chef] Re: Include recipe if file exists.


Chronological Thread 
  • From: Fabien Delpierre < >
  • To: chef < >
  • Subject: [chef] Re: Include recipe if file exists.
  • Date: Thu, 8 Oct 2015 13:11:34 -0400

I was certain the syntax should work since I believe I've used it before, but I tested it anyway before sending this. Here's what I did:

1. Set up a blank cookbook
2. Loaded it up in Vagrant (still empty recipe)
3. Added a dependency on the git cookbook and reprovisioned the VM (still with an empty recipe) -- nothing happened, as expected
4. Added the following bit to the recipe:

if ::File.exists?('/tmp/gitexists')
  include_recipe "git"
end


Then re-ran Vagrant -- again, nothing happened as expected.
5. Logged in to the VM and did a touch /tmp/gitexists
6. Logged out, reprovisioned the VM, this time git did get installed.

So that seems to work.

On Thu, Oct 8, 2015 at 12:39 PM, Fabien Delpierre < " target="_blank"> > wrote:
Perhaps you misspoke, but you said you want to run a recipe only if a file exists, right?
Why use unless, then? Of course those three lines wouldn't work since it's doing "IF /etc/foo/bar NOT exist THEN include_recipe".

On Thu, Oct 8, 2015 at 12:17 PM, Doug Garstang < " target="_blank"> > wrote:
All,

Trying to do a conditional include_recipe based on a file existing. Doesn't work due to compile/converge stage stuff.

A workaround was suggested here by the folks that should know:

However, it doesn't work, just as someone else responded to the ticket with. They did not get a reply.

So, just to be clear, as suggested, this does not work:

unless ::File.exists?("/etc/foo/bar")
  include_recipe "foo::bar"
end

Doug





Archive powered by MHonArc 2.6.16.

§