chef-solo passing custom json

2019-08-26 13:25发布

问题:

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"]

回答1:

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.

bash 'mkvirtualenv' do
    cwd "/home/#{node['username']}/projects/"
    user node['username']
    environment ({
        'HOME' => ::Dir.home(node['username']),
        'USER' => node['username']
    })
    code <<-EOH
        source /usr/local/bin/virtualenvwrapper.sh \
        mkvirtualenv --no-site-packages --distribute node['params']['vhost']
    EOH
end

I see no need for #{} around node['params']['vhost'] as it is not in a string needing interpolation but I may be wrong with HereDoc syntax if it fails, readd it.



回答2:

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.