How can I call include_recipe only on true condition?


#1

i have a bash code wherein it executes if few linux commands and also have if… else loop
After completion of bash resource I am calling include_recipe.

But now I want include_recipe to get called only for if loop in bash resource and skip if it goes to else loop

How can I achieve that?


#2

are you using the execute resource ?

have a look at using guards in your recipes.

https://docs.chef.io/resource_common.html#guards

And example/snippet of you chef recipe is always useful


#3

Hi,
No. I am not using execute resource. I am using bash resource in which I have if… else loop
after completing bash resource, I am running include_recipe

Now I want to run include_recipe <receipe 2> only if “if” part in bash resource is true and not for else loop

How can I do that?

snippet is herewith

bash ‘Host registration with server’ do
cwd node[‘local’][‘download_dir’]
sensitive true
user "root"
code <<-EOH
# Login to server
<<<< steps to login to to server and check IP address >>

if [ $? != 0 ]
then
  
	<<< check if host IP is not present, then do registration>>

else

no action as IP is already present
fi

# Logout from server
./vmssc logout server	

EOH
end

Invoke addGPtoHost recipe if guard point exists

if node[‘guardPointsList’].strip != ""
include_recipe 'gde_client::addGPtoHost’
end