One Policyfile.rb across different nodes


#1

I have Policyfile.rb like this one

name ‘policy’

run_list(
‘recipe[cookbook::default]’
)

default[‘cookbook’] = {
‘deviceid’ => 12345,
}

So the case is to use this policyfile, say, accross 20 nodes. The key difference bettween each node is just ‘deviceid’ attribute.

So my question is: how to stick with only one Policyfile instead of use 20 Policyfiles with just different ‘deviceid’ attribute.


#2

@jugatsu what you want to do is use hoisting. This will allow you to specify different attributes for different machine deployments. Example can be found is this great presentation https://yolover.poise.io/.

What i have done is create a rb file per machine environment and define just the different attributes. Everything else gets defined at the global level in the Policyfile. I have also defined the environment attributes in different files to make it easier to manage depending on the environment changes the files can get large

Example of what you might define is

default[‘env1’][‘cookbook’][‘deviceid’] = 12
default[‘env2’][‘cookbook’][‘deviceid’] = 13
default[‘env3’][‘cookbook’][‘deviceid’] = 14

then in you client.rb file you would specify policy_group ‘env1’

You will also need to add the cookbook “poise-hoist” to your run list to make this easier.

Let me know if you need more info.


#3

@stonesbg thanks for reply. But can you give a little bit more info. You mean using instance_eval(IO.read(‘Policyfile.rb’)) in each node1,node2,node3…node20.rb file?


#4

So what you would do is you would have your main policyfile for example master_policy.rb

Within master_policy.rb you would define your run_list, named_run_list and the default attributes for all instances.

As part of the run_list and named_run_list that you define you need to add the “poise_hoist” cookbook at the very start of the run_list definition.

Then create a folder called env_attributes and in this folder I will create “env1_attributes.rb”. This will be the ruby file that defines all of the default attributes that need to be different for the “env1” environment. You need to make sure that you always specify [‘env1’] as the first attribute for any attributes that you want to override. For example
default['env1']['cookbook']['deviceid'] = 12

Now in order to load this file as part of the "chef update " you need to put is these lines:

env1_path = File.expand_path('../env_attributes/env1_attributes.rb', __FILE__) instance_eval(IO.read(env1_path))

This will inject the contents of the env1_attributes.rb into the master_policy.rb.

Next you just need to make sure you client.rb has the policy_group defined correctly. Essentially what the poise-hoist cookbook does is it looks at the policy_group your running with “env1” and remove that level of the hierarchy and remaps all values. Therefore you end up with the default[‘env1’][‘cookbook’][‘deviceid’] => default[‘cookbook’][‘deviceid’]

Hope that helps


#5

Great. Appreciate. I got the idea, will try.