Arbitrary attributes for nodes


#1

Maybe I’m missing something but the wiki pages on attributes and their use
in roles, nodes and recipes is confusing me.

I’d simply like to be able to set arbitrary attributes on a node that can be
used in a recipe.

Meaning somewhere in the node’s json place something like, { “num” : “1” }

And be able to access this in any recipe the node is running like
node[:num].

Is this not possible? I tried placing key/values under default through a
knife edit of the node but my attempts to access the values in a recipe
returns nil.

Thanks,
Tristan


#2

node.set[:num] = 1

On Tue, Feb 15, 2011 at 10:03 AM, Tristan Sloughter
tristan.sloughter@gmail.com wrote:

Maybe I’m missing something but the wiki pages on attributes and their use
in roles, nodes and recipes is confusing me.
I’d simply like to be able to set arbitrary attributes on a node that can be
used in a recipe.
Meaning somewhere in the node’s json place something like, { “num” : “1” }
And be able to access this in any recipe the node is running like
node[:num].
Is this not possible? I tried placing key/values under default through a
knife edit of the node but my attempts to access the values in a recipe
returns nil.
Thanks,
Tristan


#3

But where is that done? I saw this in the training slides… I want it done
per node with different values so I was hoping I could simply place it in
the nodes attributes.

Tristan

On Tue, Feb 15, 2011 at 9:10 AM, Sean OMeara someara@gmail.com wrote:

node.set[:num] = 1

On Tue, Feb 15, 2011 at 10:03 AM, Tristan Sloughter
tristan.sloughter@gmail.com wrote:

Maybe I’m missing something but the wiki pages on attributes and their
use
in roles, nodes and recipes is confusing me.
I’d simply like to be able to set arbitrary attributes on a node that can
be
used in a recipe.
Meaning somewhere in the node’s json place something like, { “num” : “1”
}
And be able to access this in any recipe the node is running like
node[:num].
Is this not possible? I tried placing key/values under default through a
knife edit of the node but my attempts to access the values in a recipe
returns nil.
Thanks,
Tristan


#4

In override?

~/development/chef-repo$ knife node show db3

"override": {
“mysql”: {
“manage_users”: true
}
},
“run_list”: [
“role[mysql_master]”
]
}

~/development/chef-repo$ knife node show db3slave3

"override": {
},
“run_list”: [
“role[mysql_slave]”
]
}

~/development/chef-repo/site-cookbooks/users/attributes$ cat default.rb
default[:mysql][:manage_users] = false

On Tue, Feb 15, 2011 at 10:11 AM, Tristan Sloughter
tristan.sloughter@gmail.com wrote:

But where is that done? I saw this in the training slides… I want it done
per node with different values so I was hoping I could simply place it in
the nodes attributes.
Tristan

On Tue, Feb 15, 2011 at 9:10 AM, Sean OMeara someara@gmail.com wrote:

node.set[:num] = 1

On Tue, Feb 15, 2011 at 10:03 AM, Tristan Sloughter
tristan.sloughter@gmail.com wrote:

Maybe I’m missing something but the wiki pages on attributes and their
use
in roles, nodes and recipes is confusing me.
I’d simply like to be able to set arbitrary attributes on a node that
can be
used in a recipe.
Meaning somewhere in the node’s json place something like, { “num” : “1”
}
And be able to access this in any recipe the node is running like
node[:num].
Is this not possible? I tried placing key/values under default through a
knife edit of the node but my attempts to access the values in a recipe
returns nil.
Thanks,
Tristan


#5

Hmm, so yes adding the default.rb to attributes works for the default
values. But adding to override does not actual override. I’m still getting 0
(which I placed in default.rb instead of 1 which I placed in override).
Could there be a reason that its not updating the information on the client
when run?

Tristan

On Tue, Feb 15, 2011 at 9:40 AM, John E. Vincent (lusis) <lusis.org+
chef-list@gmail.com> wrote:

In override?

~/development/chef-repo$ knife node show db3

"override": {
“mysql”: {
“manage_users”: true
}
},
“run_list”: [
“role[mysql_master]”
]
}

~/development/chef-repo$ knife node show db3slave3

"override": {
},
“run_list”: [
“role[mysql_slave]”
]
}

~/development/chef-repo/site-cookbooks/users/attributes$ cat default.rb
default[:mysql][:manage_users] = false

On Tue, Feb 15, 2011 at 10:11 AM, Tristan Sloughter
tristan.sloughter@gmail.com wrote:

But where is that done? I saw this in the training slides… I want it
done
per node with different values so I was hoping I could simply place it in
the nodes attributes.
Tristan

On Tue, Feb 15, 2011 at 9:10 AM, Sean OMeara someara@gmail.com wrote:

node.set[:num] = 1

On Tue, Feb 15, 2011 at 10:03 AM, Tristan Sloughter
tristan.sloughter@gmail.com wrote:

Maybe I’m missing something but the wiki pages on attributes and their
use
in roles, nodes and recipes is confusing me.
I’d simply like to be able to set arbitrary attributes on a node that
can be
used in a recipe.
Meaning somewhere in the node’s json place something like, { “num” :
“1”

}
And be able to access this in any recipe the node is running like
node[:num].
Is this not possible? I tried placing key/values under default through
a

knife edit of the node but my attempts to access the values in a
recipe

returns nil.
Thanks,
Tristan


#6

On Tuesday, February 15, 2011 at 7:47 AM, Tristan Sloughter wrote:
Hmm, so yes adding the default.rb to attributes works for the default values. But adding to override does not actual override. I’m still getting 0 (which I placed in default.rb instead of 1 which I placed in override). Could there be a reason that its not updating the information on the client when run?

Tristan
Nodes can’t have their own persistent override attributes. However, “normal” level attributes will win over default level attributes, so you should use these to get the behavior you want.

The general pattern for attributes precedence is that cookbooks and roles should be setting defaults. If you need to change the values for a specific node, use, the normal attributes on the node. Overrides are there so roles can force a certain value even if the node already has a value there. There are certainly other ways to use it, but that is the pattern it was designed for.


Dan DeLeo


#7

I’ve also found it cleaner to just use roles for everything… I’m
sure this isn’t how the community cookbooks work, but consider:

knife ec2 server create “role[dbmaster]” -N "dbmaster.example.com"
knife ec2 server create “role[dbslave]” -N “dbslave1.example.com

~/$ cat roles/dbmaster.rb
run_list [ “recipe[mysql::master]” ]

~/$ cat roles/dbslave.rb
run_list [ “recipe[mysql::slave]” ]

~/$ cat cookbooks/mysql/master.rb | snip
node.set[:mysql][:master] = true
mysql_slaves = search(:node, “mysql_slaves:true”)

do master stuff

~/$ cat cookbooks/mysql/slave.rb | snip
node.set[:mysql][:slave] = true
mysql_masters = search(:node, “mysql_master:true”)

do slave stuff stuff

YMMV, TMTOWTDI, WTFBBQ, no hits no runs no errors.

-s

On Tue, Feb 15, 2011 at 10:47 AM, Tristan Sloughter
tristan.sloughter@gmail.com wrote:

Hmm, so yes adding the default.rb to attributes works for the default
values. But adding to override does not actual override. I’m still getting 0
(which I placed in default.rb instead of 1 which I placed in override).
Could there be a reason that its not updating the information on the client
when run?
Tristan
On Tue, Feb 15, 2011 at 9:40 AM, John E. Vincent (lusis)
lusis.org+chef-list@gmail.com wrote:

In override?

~/development/chef-repo$ knife node show db3

"override": {
“mysql”: {
“manage_users”: true
}
},
“run_list”: [
“role[mysql_master]”
]
}

~/development/chef-repo$ knife node show db3slave3

"override": {
},
“run_list”: [
“role[mysql_slave]”
]
}

~/development/chef-repo/site-cookbooks/users/attributes$ cat default.rb
default[:mysql][:manage_users] = false

On Tue, Feb 15, 2011 at 10:11 AM, Tristan Sloughter
tristan.sloughter@gmail.com wrote:

But where is that done? I saw this in the training slides… I want it
done
per node with different values so I was hoping I could simply place it
in
the nodes attributes.
Tristan

On Tue, Feb 15, 2011 at 9:10 AM, Sean OMeara someara@gmail.com wrote:

node.set[:num] = 1

On Tue, Feb 15, 2011 at 10:03 AM, Tristan Sloughter
tristan.sloughter@gmail.com wrote:

Maybe I’m missing something but the wiki pages on attributes and
their
use
in roles, nodes and recipes is confusing me.
I’d simply like to be able to set arbitrary attributes on a node that
can be
used in a recipe.
Meaning somewhere in the node’s json place something like, { “num” :
“1”
}
And be able to access this in any recipe the node is running like
node[:num].
Is this not possible? I tried placing key/values under default
through a
knife edit of the node but my attempts to access the values in a
recipe
returns nil.
Thanks,
Tristan


#8

Great, thanks! This worked perfectly.

On Tue, Feb 15, 2011 at 10:05 AM, Daniel DeLeo dan@kallistec.com wrote:

On Tuesday, February 15, 2011 at 7:47 AM, Tristan Sloughter wrote:

Hmm, so yes adding the default.rb to attributes works for the default
values. But adding to override does not actual override. I’m still getting 0
(which I placed in default.rb instead of 1 which I placed in override).
Could there be a reason that its not updating the information on the client
when run?

Tristan

Nodes can’t have their own persistent override attributes. However,
“normal” level attributes will win over default level attributes, so you
should use these to get the behavior you want.

The general pattern for attributes precedence is that cookbooks and roles
should be setting defaults. If you need to change the values for a specific
node, use, the normal attributes on the node. Overrides are there so roles
can force a certain value even if the node already has a value there. There
are certainly other ways to use it, but that is the pattern it was designed
for.


Dan DeLeo


#9

+1 for a nice simple articulation…added to the wiki:
http://wiki.opscode.com/display/chef/Attributes#Attributes-UsageBestPractices

Seth


Opscode, Inc.
Seth Chisamore, Technical Evangelist
T: (404) 348-0505 E: schisamo@opscode.com
Twitter, IRC, Github: schisamo

On Tue, Feb 15, 2011 at 11:05 AM, Daniel DeLeo dan@kallistec.com wrote:

On Tuesday, February 15, 2011 at 7:47 AM, Tristan Sloughter wrote:

Hmm, so yes adding the default.rb to attributes works for the default
values. But adding to override does not actual override. I’m still getting 0
(which I placed in default.rb instead of 1 which I placed in override).
Could there be a reason that its not updating the information on the client
when run?

Tristan

Nodes can’t have their own persistent override attributes. However,
“normal” level attributes will win over default level attributes, so you
should use these to get the behavior you want.

The general pattern for attributes precedence is that cookbooks and roles
should be setting defaults. If you need to change the values for a specific
node, use, the normal attributes on the node. Overrides are there so roles
can force a certain value even if the node already has a value there. There
are certainly other ways to use it, but that is the pattern it was designed
for.


Dan DeLeo