tortuga
tortuga

Reputation: 757

Chef Attribute file

I'm trying to pass attributes to my recipe at run-time of chef client.

following is my default.rb attribute file:

if node['os'] == 'windows'
  if node.attribute?('temp')
    default['param']['name'] = node['temp']['tempname']
  else node.attribute?('base')
    default['param']['name'] = node['base']['nodename']
  end
end

I first ran the chef-client with only the base attribute defined in the node.json file. My node.json looked like :

{
  "base": {
    "nodename": "John"
  }
}

Chef-client run was successfull and the attribute was set accordingly. i.e.

default['param']['name'] = "John"

Than I ran chef-client with both base and temp attribute defined in the node.json. And here is my node.json file :

{
  "base": {
    "nodename": "John"
  },
  "temp": {
    "tempname": "Mike"
  }
}

Chef-client again ran successfully and the attribute was set accordingly.

default['param']['name'] = "Mike"

The problem is when I run the chef-client again and pass only base attribute from the node.json file(see below for the file), the code doesn't seem to enter the else loop. It just runs the `if' loop with old value for temp attribute(Mike).

{
  "base": {
    "nodename": "John-2"
  }
}

Any ideas to where I'm going wrong.

Upvotes: 2

Views: 4105

Answers (1)

cassianoleal
cassianoleal

Reputation: 2566

The second time you ran chef-client, it saved the node[:temp][:nodename] attribute to the node object on the Chef server.

When you ran chef-client again, it loaded the node's attributes back from the server before reaching the attributes/default.rb file, so node[:temp][:nodename] was set when it reached the conditions.

I'm not sure what you're trying to achieve, but it would probably be best to explicitly tell which attribute you want to use through another attribute (and maybe assume a default for it), such as:

default[:name_attribute] = 'temp'

then,

if node['os'] == 'windows'
  if node['name_attribute'] == 'temp'
    default['param']['name'] = node['temp']['tempname']
  else
    default['param']['name'] = node['base']['nodename']
  end
end

Upvotes: 3

Related Questions