Modifying node run list from within a recipe?

Folks,

So, I’ve done some searching through the documents, and searching through the archives, and feeding various terms into Google, and I’m still coming up empty.

In short, I know that I could do a “knife node run_list add” from within a recipe, but I figure there has to be a better way to do this from within Ruby itself, and which doesn’t involve dependency on a tool that shouldn’t have to be installed on every single node that might run some random application.

I must be missing something obvious, but I don’t know what it is. Any and all assistance or pointers you can provide will be greatly appreciated.

Thanks!


Brad Knowles brad@shub-internet.org
LinkedIn Profile: http://tinyurl.com/y8kpxu

Why not just key off an attribute to decide whether or not to do recipe_includes? Here’s an example:

You could wrap that in any sort of conditional Ruby logic you want.

Thanks,
Matt Ray
Cloud Integrations Product Lead :: Opscode
512.731.2218 :: matt@opscode.com
mattray :: GitHub :: IRC :: Twitter


From: Brad Knowles brad@shub-internet.org
Sent: Tuesday, August 27, 2013 9:05 AM
To: chef@lists.opscode.com
Cc: Brad Knowles
Subject: [chef] Modifying node run list from within a recipe?

Folks,

So, I’ve done some searching through the documents, and searching through the archives, and feeding various terms into Google, and I’m still coming up empty.

In short, I know that I could do a “knife node run_list add” from within a recipe, but I figure there has to be a better way to do this from within Ruby itself, and which doesn’t involve dependency on a tool that shouldn’t have to be installed on every single node that might run some random application.

I must be missing something obvious, but I don’t know what it is. Any and all assistance or pointers you can provide will be greatly appreciated.

Thanks!


Brad Knowles brad@shub-internet.org
LinkedIn Profile: http://tinyurl.com/y8kpxu

On Aug 27, 2013, at 9:17 AM, Matt Ray matt@opscode.com wrote:

Why not just key off an attribute to decide whether or not to do recipe_includes? Here's an example:
https://github.com/opscode-cookbooks/java/blob/master/recipes/default.rb#L21

You could wrap that in any sort of conditional Ruby logic you want.

In the particular case I have, I'm converting an installation script from bash to Chef, and part of what the bash script does is adding a role to the run_list for the node it's running on. So, the Chef recipe(s) in question would be running as a command-line over-ride for the node, possibly as part of a chef-solo run, and not part of a normal chef-client run.

So, unless there's something obvious that I'm missing, I don't have a way to modify the existing Chef recipe(s) for the node to do the kind of thing that you're suggesting.

Is there a better way to solve this problem?

--
Brad Knowles brad@shub-internet.org
LinkedIn Profile: http://tinyurl.com/y8kpxu

On Aug 27, 2013, at 10:06 AM, Brad Knowles brad@shub-internet.org wrote:

In the particular case I have, I'm converting an installation script from bash to Chef, and part of what the bash script does is adding a role to the run_list for the node it's running on. So, the Chef recipe(s) in question would be running as a command-line over-ride for the node, possibly as part of a chef-solo run, and not part of a normal chef-client run.

Ahh, it just took a bit more searching. The end result is shown at [0], but the key elements were discovering that the class is Chef::RunList::RunListItem (I found good examples at [1] and [2]), but the key link is shown at [3]:

but node.run_list is a Chef::RunList

The final result being shown at [0] is:

node = Chef::Node.load('n')
node.run_list << "role[foo]"
node.save

Thanks, everyone!

[0] http://stackoverflow.com/a/11749480
[1] http://rubydoc.info/gems/chef/11.4.4/Chef/RunList/RunListItem
[2] https://github.com/opscode/chef/blob/master/spec/unit/run_list/run_list_item_spec.rb
[3] http://lists.opscode.com/sympa/arc/chef/2013-02/msg00271.html

--
Brad Knowles brad@shub-internet.org
LinkedIn Profile: http://tinyurl.com/y8kpxu

1 Like