I am desperately trying to figure out how to apply a few node variables in Chef-Solo. Otherwise I have to keep reprovisioning.
Step 1, I log into the provisioned box:
$ vagrant ssh
$ sudo chef-solo -o virtualenv::addsite -j /vagrant/resources/attr_test.json
Step 2, I have a JSON file passing data that im trying to run against in a recipe. I believe this has node.normal mode which is higher priority than default?
Here is what i have at the moment in attr_test.json
{
"username": "vagrant",
"params": {
"vhost": "fake"
},
"user": {
"name": "vagrant"
},
"overwrite_attributes": {
"username": "vagrant"
}
}
This is the recipe but I can't get the node.default data to populate.
virtualenv/recipes/addsite.rb
bash 'mkvirtualenv' do
cwd "/home/#{node.default['username']}/projects/"
user node.default['username']
environment ({
'HOME' => ::Dir.home(node.default['username']),
'USER' => node.default['username']
})
code <<-EOH
source /usr/local/bin/virtualenvwrapper.sh \
mkvirtualenv --no-site-packages --distribute #{node.default['params']['vhost']}
EOH
end
The Error
Chef::Exceptions::ValidationFailed
----------------------------------
Option user must be a kind of [String, Integer]! You passed {}.
Cookbook Trace:
---------------
/var/chef/cookbooks/virtualenv/recipes/addsite.rb:8:in `block in from_file'
/var/chef/cookbooks/virtualenv/recipes/addsite.rb:6:in `from_file'
Does anyone know how to do this? Do I need to pass in a Chef::Environment or Chef::Role in the JSON for json_type?
UPDATE
Here is another thing I've tried with Chef::Node, Chef::Environment, Chef::Role, etc. I still get "You Passed {}" and can't see the username or the other items :(
{
"json_class": "Chef::Node",
"chef_type": "node",
"name": "node_test",
"normal": {
"username": "vagrant",
"params": {
"vhost": "fakeit"
}
},
"normal_attributes": {
"username": "vagrant",
"params": {
"vhost": "fakeit"
}
},
"override_attributes": {
"username": "vagrant",
"params": {
"vhost": "fakeit"
}
},
"override": {
"username": "vagrant",
"params": {
"vhost": "fakeit"
}
},
"default": {
"username": "vagrant",
"params": {
"vhost": "fakeit"
}
},
"default_attributes": {
"username": "vagrant",
"params": {
"vhost": "fakeit"
}
},
"automatic": {},
"run_list": [
]
}
Solution
I ticked off the right answer. Here is my JSON so it helps anyone! The problem was the RECIPE:
{ "json_class": "Chef::Node", "chef_type": "node", "name": "node_test", "normal": { "username": "vagrant", "params": { "vhost": "fakeit" } }, "override": {}, "default": {}, "automatic": {}, "run_list": [] }
- To WRITE/SET a value I just use
node.default["value"]
- To READ a value I just use
node["value"]
You should use
node.default['whatever']
only when you set the default value for an attribute in a recipe.To use an atrtibute just use
node['whatever']
I think the error comes from the 'USER' environement variable getting an empty value.
This code should work as expected with your first json file.
I see no need for
#{}
aroundnode['params']['vhost']
as it is not in a string needing interpolation but I may be wrong with HereDoc syntax if it fails, readd it.Do you have set "node['params']['vhost']" attribute?
If that's not issue, try to set "default_attributes" instead of "overwrite_attributes", because there is no attributes to override.