Hi,
I’m new to Chef and am trying to migrate my existing infrastructure. Sorry
if this is explained in the docs or elsewhere, but I’ve not been able to
find it.
I have two Chef environments, dev and prod, and want to specify file paths
in a cookbook that have a different parent directory based on the
environment. When I try to access the environment attribute in my cookbook,
however, all I get is ‘nil’.
For example, in my “dev” environment’s JSON file, I have
…
“default_attributes”: {
“my_attr”: “/my/path”
},
…
And I can verify my_attr is defined in
https://manage.opscode.com/environments/dev. If I do a knife node show dev_all
it shows the proper environment, role, run list, and recipe. In
that recipe’s attributes/default.rb, I have:
Chef::Log.info(’---------------’)
Chef::Log.info(node.chef_environment)
Chef::Log.info(node[:my_attr])
Chef::Log.info(Chef::Environment.load(node.chef_environment).default_attributes[‘my_attr’])
Chef::Log.info(’---------------’)
When I run a vagrant provision, it prints:
$ rvmsudo vagrant provision
[default] Running provisioner: Vagrant::Provisioners::ChefClient…
[default] Creating folder to hold client key…
[default] Uploading chef client validation key…
[default] Generating chef JSON and uploading…
[default] Running chef-client…
stdin: is not a tty
[2012-12-26T06:52:26+00:00] INFO: *** Chef 10.16.2 ***
[2012-12-26T06:52:27+00:00] INFO: Run List is [role[web]]
[2012-12-26T06:52:27+00:00] INFO: Run List expands to [vine_web]
[2012-12-26T06:52:28+00:00] INFO: Starting Chef Run for dev_all
[2012-12-26T06:52:28+00:00] INFO: Running start handlers
[2012-12-26T06:52:28+00:00] INFO: Start handlers complete.
[2012-12-26T06:52:29+00:00] INFO: Loading cookbooks [build-essential,
nginx, ohai, vine_web]
[2012-12-26T06:52:29+00:00] INFO: ---------------
[2012-12-26T06:52:29+00:00] INFO: dev
[2012-12-26T06:52:29+00:00] INFO: nil
[2012-12-26T06:52:29+00:00] INFO: /my/path
[2012-12-26T06:52:29+00:00] INFO: ---------------
[2012-12-26T06:52:31+00:00] INFO: Chef Run complete in 3.026496384 seconds
[2012-12-26T06:52:31+00:00] INFO: Running report handlers
[2012-12-26T06:52:31+00:00] INFO: Report handlers complete
I’m confused because my node defintiely has the right environment, and the
environment definitely has the right attribute, but the attribute hasn’t
been assigned to the node by the time my cookbook runs.
Is this the expected behavior, perhaps due to the order in which things
happen? If no, what have I done wrong?
Is my workaround the right thing to be doing? It took quite a bit of
googling before I stumbled into the Chef docs and thought to try that. If
yes, is there somewhere in the docs I can make a note of this?
Thanks in advance, and happy holidays!
/~s