Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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
share|improve this question

1 Answer 1

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.

share|improve this answer
    
Updated the question with the code I'm using (fails on the first run because of missing nova-manage command) - the package is listed to install before, but the Chef::ShellOut runs before all resources. –  viraptor Jan 4 '12 at 15:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.