Changing the order of 'run_list'

Ohai!

To add one recipe before other, I can use:

knife node run_list add lasvegas1 ‘role[base]’ --before ‘role[web]’

But I can’t do this using recipe:

if not node.run_list.roles.include?(‘base’)
ruby_block “Add base role before web” do
block { node.run_list.add(“role[base]”).before(“role[web]”) }
end
end

How can I do this?

Thanks!


– Tiago Cruz

On Friday, June 5, 2015 at 8:01 AM, Tiago Cruz wrote:

Ohai!

To add one recipe before other, I can use:

knife node run_list add lasvegas1 'role[base]' --before 'role[web]'

But I can't do this using recipe:

if not node.run_list.roles.include?('base')
ruby_block "Add base role before web" do
block { node.run_list.add("role[base]").before("role[web]") }
end
end

How can I do this?

Thanks!

--
-- Tiago Cruz

The run list is just a ruby array, use ruby’s Array methods for this.

--
Daniel DeLeo

Hello Daniel, thanks for your time!

Could you please help with some example? I think that I don't get yet :frowning:

  • Try 1:

node.run_list.add("role[base]", --before, "role[web]")

[2015-06-05T17:51:29+00:00] ERROR: ruby_block[Add base before web]
(test::web line 7) had an error: NoMethodError: undefined method `before'
for Chef::Resource::RubyBlock

  • Try 2:

node.run_list.add("role[base]", before, "role[web]")

[2015-06-05T17:53:01+00:00] ERROR: ruby_block[Add base before web]
(test::web line 7) had an error: NoMethodError: undefined method `before'
for Chef::Resource::RubyBlock

  • Try 3:

node.run_list.add("role[base]", "before", "role[web]")

[2015-06-05T17:47:04+00:00] ERROR: ruby_block[Add base before web]
(test::web line 7) had an error: ArgumentError: wrong number of arguments
(3 for 1)

Thanks!

On Fri, Jun 5, 2015 at 12:19 PM, Daniel DeLeo dan@kallistec.com wrote:

On Friday, June 5, 2015 at 8:01 AM, Tiago Cruz wrote:

Ohai!

To add one recipe before other, I can use:

knife node run_list add lasvegas1 'role[base]' --before 'role[web]'

But I can't do this using recipe:

if not node.run_list.roles.include?('base')
ruby_block "Add base role before web" do
block { node.run_list.add("role[base]").before("role[web]") }
end
end

How can I do this?

Thanks!

--
-- Tiago Cruz

The run list is just a ruby array, use ruby’s Array methods for this.

Class: Array (Ruby 2.2.0)

--
Daniel DeLeo

--
-- Tiago Cruz

This is not something you should generally do as changing the run list from inside recipe code probably won't have the effect you expect. Notably it won't have any effect until the next run assuming the current one succeeds.

As to your specific question: node.run_list.run_list_items.insert(node.run_list.find('role[base']), 'role[web]'). You can find more information in the Ruby documentation for Array#insert and Enumerable#find.

--Noah

On Jun 5, 2015, at 11:09 AM, Tiago Cruz tiago.tuxkiller@gmail.com wrote:

Hello Daniel, thanks for your time!

Could you please help with some example? I think that I don't get yet :frowning:

  • Try 1:

node.run_list.add("role[base]", --before, "role[web]")

[2015-06-05T17:51:29+00:00] ERROR: ruby_block[Add base before web] (test::web line 7) had an error: NoMethodError: undefined method `before' for Chef::Resource::RubyBlock

  • Try 2:

node.run_list.add("role[base]", before, "role[web]")

[2015-06-05T17:53:01+00:00] ERROR: ruby_block[Add base before web] (test::web line 7) had an error: NoMethodError: undefined method `before' for Chef::Resource::RubyBlock

  • Try 3:

node.run_list.add("role[base]", "before", "role[web]")

[2015-06-05T17:47:04+00:00] ERROR: ruby_block[Add base before web] (test::web line 7) had an error: ArgumentError: wrong number of arguments (3 for 1)

Thanks!

On Fri, Jun 5, 2015 at 12:19 PM, Daniel DeLeo dan@kallistec.com wrote:

On Friday, June 5, 2015 at 8:01 AM, Tiago Cruz wrote:

Ohai!

To add one recipe before other, I can use:

knife node run_list add lasvegas1 'role[base]' --before 'role[web]'

But I can't do this using recipe:

if not node.run_list.roles.include?('base')
ruby_block "Add base role before web" do
block { node.run_list.add("role[base]").before("role[web]") }
end
end

How can I do this?

Thanks!

--
-- Tiago Cruz

The run list is just a ruby array, use ruby’s Array methods for this.

Class: Array (Ruby 2.2.0)

--
Daniel DeLeo

--
-- Tiago Cruz

Thanks Noah,

I was unable to config this as you said, but I'll post my ugly solution
here to help someone in the future:

add role 'base' before the main one after first run

ruby_block "Add base before web" do
block {
node.run_list.remove("role[web]")
node.run_list.add("role[base]")
node.run_list.add("role[web]")
}
not_if { node.run_list.roles.include?('base') }
end

On Fri, Jun 5, 2015 at 3:17 PM, Noah Kantrowitz noah@coderanger.net wrote:

This is not something you should generally do as changing the run list
from inside recipe code probably won't have the effect you expect. Notably
it won't have any effect until the next run assuming the current one
succeeds.

As to your specific question:
node.run_list.run_list_items.insert(node.run_list.find('role[base']),
'role[web]'). You can find more information in the Ruby documentation for
Array#insert and Enumerable#find.

--Noah

On Jun 5, 2015, at 11:09 AM, Tiago Cruz tiago.tuxkiller@gmail.com wrote:

Hello Daniel, thanks for your time!

Could you please help with some example? I think that I don't get yet :frowning:

  • Try 1:

node.run_list.add("role[base]", --before, "role[web]")

[2015-06-05T17:51:29+00:00] ERROR: ruby_block[Add base before web]
(test::web line 7) had an error: NoMethodError: undefined method `before'
for Chef::Resource::RubyBlock

  • Try 2:

node.run_list.add("role[base]", before, "role[web]")

[2015-06-05T17:53:01+00:00] ERROR: ruby_block[Add base before web]
(test::web line 7) had an error: NoMethodError: undefined method `before'
for Chef::Resource::RubyBlock

  • Try 3:

node.run_list.add("role[base]", "before", "role[web]")

[2015-06-05T17:47:04+00:00] ERROR: ruby_block[Add base before web]
(test::web line 7) had an error: ArgumentError: wrong number of arguments
(3 for 1)

Thanks!

On Fri, Jun 5, 2015 at 12:19 PM, Daniel DeLeo dan@kallistec.com wrote:

On Friday, June 5, 2015 at 8:01 AM, Tiago Cruz wrote:

Ohai!

To add one recipe before other, I can use:

knife node run_list add lasvegas1 'role[base]' --before 'role[web]'

But I can't do this using recipe:

if not node.run_list.roles.include?('base')
ruby_block "Add base role before web" do
block { node.run_list.add("role[base]").before("role[web]") }
end
end

How can I do this?

Thanks!

--
-- Tiago Cruz

The run list is just a ruby array, use ruby’s Array methods for this.

Class: Array (Ruby 2.2.0)

--
Daniel DeLeo

--
-- Tiago Cruz

--
-- Tiago Cruz