Chef-solo传递自定义json [英] chef-solo passing custom json

查看:87
本文介绍了Chef-solo传递自定义json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我拼命地试图找出如何在Chef-Solo中应用几个节点变量。否则,我必须继续进行调配。

I am desperately trying to figure out how to apply a few node variables in Chef-Solo. Otherwise I have to keep reprovisioning.

第1步,我登录到配置框:

$ vagrant ssh
$ sudo chef-solo -o virtualenv::addsite -j /vagrant/resources/attr_test.json 

第2步,我有一个JSON文件,该数据传递了我试图在配方中运行的数据。 我相信它具有node.normal模式,其优先级高于默认模式?

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?

这里是我目前在
中所拥有的 attr_test.json

{
    "username": "vagrant",
    "params": {
        "vhost": "fake"
    },
    "user": {
        "name": "vagrant"
    },
    "overwrite_attributes": {
        "username": "vagrant"
    }
}

这是食谱,但我无法填充node.default数据。

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

错误

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'

有人知道怎么做吗?
我需要在JSON中为 json_type 传递 Chef :: Environment Chef :: Role 吗?

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?

更新

这是我尝试过Chef :: Node,Chef的另一件事:::环境,厨师::角色等。我仍然收到 You Passed {},看不到用户名或其他项目:(

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": [
   ]
}

解决方案

我勾选了正确的答案。这是我的JSON,因此可以帮助任何人!问题是RECIPE:

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:[]}

{ "json_class": "Chef::Node", "chef_type": "node", "name": "node_test", "normal": { "username": "vagrant", "params": { "vhost": "fakeit" } }, "override": {}, "default": {}, "automatic": {}, "run_list": [] }




  • 写入/设置值我只使用 node.default [ value]

  • 要读取一个值,我只使用 node [ value]

    • To WRITE/SET a value I just use node.default["value"]
    • To READ a value I just use node["value"]
    • 推荐答案

      您应使用 node.default ['whatever'] 仅在您为食谱中的属性设置默认值时。

      You should use node.default['whatever'] only when you set the default value for an attribute in a recipe.

      要使用atrtibute只需使用 node ['whatever']

      To use an atrtibute just use node['whatever']

      我认为错误来自于'USER'环境变量一个空值。

      I think the error comes from the 'USER' environement variable getting an empty value.

      此代码应可与您的第一个json文件按预期工作。

      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
      

      我认为不需要#{} node ['params'] ['vhost'] 附近,因为它不在需要插值的字符串中,但如果使用HereDoc语法,我可能会错失败,将其读取。

      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.

      这篇关于Chef-solo传递自定义json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆