3

I have an attribute file titled default.rb with a key in it default['current'] that stores a dir in string format. I wanted to use this for the following command to use when it changes directories:

# Run database migrations
execute 'database migrations' do
  user    'ubuntu'
  cwd     "#{default['current']}"
  command 'sudo php artisan down && sudo php artisan migrate --force && sudo php artisan up'
end

Instead when i ran the recipe i got the following error.

NameError
---------
No resource, method, or local variable named `default' for `Chef::Recipe "deploy"'

Is there a proper way to do this that I'm missing? I'd rather not hard code in the working directory.

codeforester
  • 39,467
  • 16
  • 112
  • 140
WhyAyala
  • 647
  • 7
  • 29

2 Answers2

3

When attributes are referenced in a recipe, we need to prefix them with node, like this:

cwd     node['current']

See also: Overriding attributes in the recipe

Community
  • 1
  • 1
codeforester
  • 39,467
  • 16
  • 112
  • 140
  • Yep, this worked for me. I had thought that default was just short hand for node.default. – WhyAyala Feb 06 '17 at 21:06
  • 1
    In a recipe, node["variable"] doesn't necessarily get the default – Jon Malachowski Feb 06 '17 at 21:14
  • 1
    @JonMalachowski would you care to elaborate? – WhyAyala Feb 06 '17 at 21:22
  • @coderanger - thanks for chiming in. OP said `node.default['current']` did work. Can you please update the answer to clarify that? – codeforester Feb 07 '17 at 04:50
  • 1
    That refers specifically to the default level, which is one of 10 levels that attribute data can be set at. – coderanger Feb 07 '17 at 05:10
  • 1
    Also you don't need quotes for a single value – coderanger Feb 07 '17 at 05:10
  • 1
    Thanks. I think enclosing in single quotes is just a matter of style - having them regardless of whether the string has a space or not, I feel, is a good thing. Do you agree? – codeforester Feb 07 '17 at 05:11
  • It's a matter of style to the degree that it's not a functional error but if you think that's a good style then all I can say is all the automated lint tools we have will disagree with you. Your call if you want to listen :) – coderanger Feb 07 '17 at 08:01
  • @coderanger What would be your opinion on using symbols as keys instead? – WhyAyala Feb 07 '17 at 17:53
  • 1
    We recommend strings because they are clearer to people not familiar with Ruby. – coderanger Feb 07 '17 at 18:28
  • 2
    There is literally no difference between symbols and strings in chef- just be consistent for readability purposes. Symbols are one less character :-) – Jon Malachowski Feb 09 '17 at 22:50
  • 1
    @JonMalachowski - +1 on your preference for symbols. That one less character makes a difference when there are nested attributes. I believer Chef has a dot notation for nested attributes but they are not recommending it. – codeforester Feb 09 '17 at 23:18
  • @JonMalachowski I agree with your preference to symbols in that it is one less character to type, but I do not think it is safe to say that there is no difference between symbols and strings. They are fundamentally different objects treated very differently in memory. http://rubylearning.com/satishtalim/ruby_symbols.html – WhyAyala Feb 10 '17 at 15:12
0

This appears to be answered. But I didn't see a link to the core of the documentation for this(IMO) It is all here: https://docs.chef.io/attributes.html

Jon Malachowski
  • 256
  • 1
  • 7
  • I appreciate this but it also brings me back to a question i asked in the confirmed answer. I had thought that `default['attribute']` was the same as `node.default['attribute']` and according to the docs, `node` is implied if not included, so why did I need to include `node` in order for me to reference that attribute? For the record, I was able to accomplish the reference with node.default['attribute'] which was the given answer before the it was update to `node['attribute']`. – WhyAyala Feb 10 '17 at 16:08
  • when using a variable in a recipe (reading it), you should use 'node["attribute"]', this allows the variable to be over written in the environment/role/cli etc When the attribute is defined in the environment/variable/recipe defaults you choose what level you are defining it at. At run time the final node attribute hashmap is computed by loading them in successive order [listed in that link] – Jon Malachowski Feb 16 '17 at 16:45