I need to run some commands depending on a result of previous execution, which again depends on some package being installed.
On the first run, I'd expect something like:
install package "XXX"
get output from XXX
for each line in output:
if missing, use XXX to add
if unknown, use XXX to delete
The problem is that when I do the logic in top level of the recipe, then the get output
part will happen before package XXX
is installed. I'd really like to just order those parts by using a ruby block (enforcing execution when the resources are processed), but then it seems I cannot nest execute resources inside of a ruby block resource.
How can I implement this correctly?
The recipe goes something like this:
flavors_bag = data_bag_item('nova', 'flavors')
cmd = Chef::ShellOut.new("nova-manage flavor list")
flavors = cmd.run_command.stdout
flavors_bag['spec'].each do |flavor|
execute "nova-manage flavor create #{flavor['name']} #{flavor['ram']*1024} #{flavor['cpu']} #{flavor['disk']} #{flavor['id']}" do
action :run
not_if {flavors.include? flavor['name']}
end
end
['some', 'old', 'values'].each do |flavor_name|
execute "nova-manage flavor delete #{flavor_name}" do
action :run
only_if {flavors.include? flavor_name}
end
end
Best Answer
Can you share your recipe? Unfortunately I can't find a reference, but I remember reading on the Opscode site that Chef runs Resources of the same type as a batch. So, how you are calling for the install (and the subsequent add/delete) can matter.
You may want to look at the Execute Resource
Chef documenation: sometimes you want to make sure that a resource is configured before anything else.