Converging Attributes before first chef-client run


#1

Hi all -

I’m trying to template files on central servers that’ll help bootstrap
new machines. I find myself needing to query Node Attributes that are
assembled from nested Roles on Nodes that may never have booted or run
chef-client.

The key I’m interested in is a deeply nested hash but is entirely
deterministic; i.e. unrelated to any runtime information or ohai
Attributes from the Node. The key has been designed so that, using the
current (0.9) Attribute merge functionality, the correct information
theoretically ends up on the Node object; it does, however, definitely
require deep merging to be done, and can be considered to be
arbitrarily deep.

I’m well aware that I can’t mock up a chef run 100% and expect the
correct result, but I’m taking care to isolate this one key such that
it will never change (FSVO “never”) and is utterly untouched by any
recipes - only Roles affect it. It contains things like the build
NIC’s MAC; the OS name; etc; that are useful in writing out a
dhcpd.conf, for example.

  • How can I coalesce the set of Attributes inherited by an as-yet
    inbuilt Node from its Roles and make it as equally accessible as those
    Attributes from already-running Nodes?

  • Is anyone else doing something like this? How? :slight_smile:

  • Is shef the way forward? (The wiki page seems to rule out loading Roles…)

  • Any suggestions?

[Chef-client & -server 0.9.12 on Ubuntu Lucid, FWIW]

TIA anyone!
Jonathan

Jonathan Matthews
London, UK
http://www.jpluscplusm.com/contact.html


#2

Well, answering my own question after a ludicrously short amount of
investigation (seriously, whoever wrote/hacked-on shef: the beers are
on me the next time you’re in town!):

On 22 December 2010 00:21, Jonathan Matthews contact@jpluscplusm.com wrote:
[snip]

  • How can I coalesce the set of Attributes inherited by an as-yet
    inbuilt Node from its Roles and make it as equally accessible as those
    Attributes from already-running Nodes?
    [snip]
  • Is shef the way forward? (The wiki page seems to rule out loading Roles…)

Yes, shef is (the|an) answer! The wiki page will be getting a few
updates tomorrow (mainly the display of the help text), but here’s
what I’ve just verified works:


jcm@austin:~/.chef$ NODE=my.host.fqdn.at.work ; echo “become_node
’”$NODE"’ ; node.save " | shef --client
loading configuration: /home/jcm/.chef/shef.rb
Session type: solo
Loading…done.

This is shef, the Chef shell.
Chef Version: 0.9.12
http://www.opscode.com/chef
http://wiki.opscode.com/display/chef/Home

run help' for help,exit’ or ^D to quit.

Ohai2u jcm@austin!
chef > become_node ‘my.host.fqdn.at.work’ ; node.save
Loading…done.

=> <Chef::Node:0x3fa3b09646bc @name=“my.host.fqdn.at.work”>

At that point, Roles nested to (in my case) the 4th level were
correctly saved back to the Node.

There’s obviously scripting to do around figuring out which hosts need
that bit of help pre-build, but I’m so impressed with shef - what a
brilliant tool!

Jonathan

Jonathan Matthews
London, UK
http://www.jpluscplusm.com/contact.html


#3

Don’t forget about ‘knife exec’, too, for similar crazy scripting antics.

Adam

On Tue, Dec 21, 2010 at 5:03 PM, Jonathan Matthews
contact@jpluscplusm.com wrote:

Well, answering my own question after a ludicrously short amount of
investigation (seriously, whoever wrote/hacked-on shef: the beers are
on me the next time you’re in town!):

On 22 December 2010 00:21, Jonathan Matthews contact@jpluscplusm.com wrote:
[snip]

  • How can I coalesce the set of Attributes inherited by an as-yet
    inbuilt Node from its Roles and make it as equally accessible as those
    Attributes from already-running Nodes?
    [snip]
  • Is shef the way forward? (The wiki page seems to rule out loading Roles…)

Yes, shef is (the|an) answer! The wiki page will be getting a few
updates tomorrow (mainly the display of the help text), but here’s
what I’ve just verified works:


jcm@austin:~/.chef$ NODE=my.host.fqdn.at.work ; echo “become_node
’”$NODE"’ ; node.save " | shef --client
loading configuration: /home/jcm/.chef/shef.rb
Session type: solo
Loading…done.

This is shef, the Chef shell.
Chef Version: 0.9.12
http://www.opscode.com/chef
http://wiki.opscode.com/display/chef/Home

run help' for help,exit’ or ^D to quit.

Ohai2u jcm@austin!
chef > become_node ‘my.host.fqdn.at.work’ ; node.save
Loading…done.

=> <Chef::Node:0x3fa3b09646bc @name=“my.host.fqdn.at.work”>

At that point, Roles nested to (in my case) the 4th level were
correctly saved back to the Node.

There’s obviously scripting to do around figuring out which hosts need
that bit of help pre-build, but I’m so impressed with shef - what a
brilliant tool!

Jonathan

Jonathan Matthews
London, UK
http://www.jpluscplusm.com/contact.html


Opscode, Inc.
Adam Jacob, CTO
T: (206) 508-7449 E: adam@opscode.com


#4

On 22 Dec 2010 01:34, “Adam Jacob” adam@opscode.com wrote:

Don’t forget about ‘knife exec’, too, for similar crazy scripting antics.

Looks interesting, but doesn’t appear to expose “become_node”. TBH, the
(non-existent) docs didn’t help me poke at it too much :slight_smile:


#5

On Fri, Dec 24, 2010 at 2:28 AM, Jonathan Matthews
contact@jpluscplusm.com wrote:

On 22 Dec 2010 01:34, “Adam Jacob” adam@opscode.com wrote:

Don’t forget about ‘knife exec’, too, for similar crazy scripting antics.

Looks interesting, but doesn’t appear to expose “become_node”. TBH, the
(non-existent) docs didn’t help me poke at it too much :slight_smile:

Everybody is a critic. :slight_smile:

Adam


Opscode, Inc.
Adam Jacob, CTO
T: (206) 508-7449 E: adam@opscode.com


#6

On Fri, Dec 24, 2010 at 3:28 AM, Jonathan Matthews
contact@jpluscplusm.com wrote:

On 22 Dec 2010 01:34, “Adam Jacob” adam@opscode.com wrote:

Don’t forget about ‘knife exec’, too, for similar crazy scripting antics.

Looks interesting, but doesn’t appear to expose “become_node”. TBH, the
(non-existent) docs didn’t help me poke at it too much :slight_smile:

knife exec gives you everything that’s available in the "main mode"
of shef, minus commands that apply primarily to running recipes. We
should definitely add some help to the manpage, but for the moment you
can use the interactive help inside shef (e.g., help :nodes).

You can probably achieve your goal of getting the attributes to be
applied using something like this:

knife exec -E ‘n = nodes.show(“i-ec308f81”); n.expand!; n.save’

Cheers,
Dan DeLeo


#7

On 26 December 2010 03:05, Daniel DeLeo dan@kallistec.com wrote:

knife exec gives you everything that’s available in the "main mode"
of shef, minus commands that apply primarily to running recipes. We
should definitely add some help to the manpage, but for the moment you
can use the interactive help inside shef (e.g., help :nodes).

You can probably achieve your goal of getting the attributes to be
applied using something like this:

knife exec -E ‘n = nodes.show(“i-ec308f81”); n.expand!; n.save’

That’s brilliant - a great (and fast!) one-liner to achieve almost
exactly what I need :slight_smile:

Poking at it for few minutes, I notice an interesting departure from
shef’s “become_node; node.save” behaviour: the knife call doesn’t
/remove/ attributes that’ve been deleted from included (in my case,
nested) roles. Is this by design?

Jonathan

Jonathan Matthews
London, UK
http://www.jpluscplusm.com/contact.html


#8

You can probably achieve your goal of getting the attributes to be
applied using something like this:

knife exec -E ‘n = nodes.show(“i-ec308f81”); n.expand!; n.save’

That’s brilliant - a great (and fast!) one-liner to achieve almost
exactly what I need :slight_smile:

Poking at it for few minutes, I notice an interesting departure from
shef’s “become_node; node.save” behaviour: the knife call doesn’t
/remove/ attributes that’ve been deleted from included (in my case,
nested) roles. Is this by design?

You need to add a call to Node#reset_defaults_and_overrides

Note also that you can write your knife exec scripts as a file and run
them with knife exec path/to/script.rb. There’s not any elegant way
to pass arguments to these scripts but you can use environment
variables, so you could do NODE_NAME=foo knife exec myscript.rb and
get the node name in the script via ENV[‘NODE_NAME’]`

Jonathan

Dan DeLeo