I wrote a simple chef script. There is a folder t1 in /DirectoryPath. Now I'm creating a new folder in /DirectoryPath/t5. But listDirectory() method gives same output, as if no new directory was created. I'm on MAC with chef 11.4
My script is like below
recipe.rb
def listDirectory()
ls = `ls ~/Delete`
return ls
end
log(listDirectory())
directory '/DirectoryPath/t5' do
action :create
end
log(listDirectory())
Output
Recipe: test::default
* log[t1] action write[2015-11-03T15:02:47-08:00] INFO: Processing log[t1] action write
[2015-11-03T15:02:47-08:00] INFO: t1
* directory[/DirectoryPath/t5] action create[2015-11-03T15:02:47-08:00] INFO: Processing directory[/DirectoryPath/t5] action create (test::default line 10)
[2015-11-03T15:02:47-08:00] INFO: directory[/DirectoryPath/t5] created directory /DirectoryPath/t5
- create new directory /DirectoryPath/t5
* log[t1] action write[2015-11-03T15:02:47-08:00] INFO: Processing log[t1] action write
[2015-11-03T15:02:47-08:00] INFO: t1
According to me 2nd log statement should show folders t1 and t5, but it is showing only t1. Can I know what could be the reason behind the command returning same result even if there was some change. And how do I avoid it?
This is because of the two phases of a chef-client run.
Your calls are executed during compile phase, before the directory
resource is executed (in the execution phase).
You should get yourself familiar with the other chef idioms, like libraries and definitions. You should not embed ruby code directly in the recipes.
I further suggest to post a new question with the problem that you want to achieve. Pretty sure there is a chef-style way to solve it.