【发布时间】:2015-01-23 22:10:26
【问题描述】:
我有一个 Vagrant 设置,我正在尝试使用 Chef-solo 生成一个 conf 文件,该文件循环通过定义的变量传递给应用程序。除循环外,一切正常,我对 Ruby/Chef 不够熟悉,无法发现错误。
我将列出整个事件链,以防万一出现问题,但这个过程的第一部分似乎工作正常。
配置文件是用 yaml 编写的,其中包含要传递的环境变量定义:
...
variables:
- DEBUG: 2
...
配置文件被 Vagrantfile 读入 ruby 哈希并用于创建 Chef json 节点:
...
settings = YAML::load(File.read("config.yaml"))
# Provision The Virtual Machine Using Chef
config.vm.provision "chef_solo" do |chef|
chef.json = {
"mysql" => {"server_root_password" => "secret"},
"postgresql" => {"password" => {"postgres" => "secret"}},
"nginx" => {"pid" => "/run/nginx.pid"},
"php-fpm" => {"pid" => "/run/php5-fpm.pid"},
"databases" => settings["databases"] || [],
"sites" => settings["sites"] || [],
"variables" => settings["variables"] || []
}
...
运行了一堆厨师食谱(apt、php、nginx、mysql 等),最后是我的定制食谱,这让我很伤心。手册中负责创建 conf 文件的部分如下所示:
# Configure All Of The Server Environment Variables
template "#{node['php-fpm']['pool_conf_dir']}/vars.conf" do
source "vars.erb"
owner "root"
group "root"
mode 0644
variables(
:vars => node['variables']
)
notifies :restart, "service[php-fpm]"
end
而 vars.erb 只是一个单行代码
<%= @vars.each {|key, value| puts "env[" + key + " = " + value } %>
所以,当我运行所有这些厨师时,会吐出一个关于无法将哈希转换为字符串的错误。
can't convert Chef::Node::immutableMash into String
因此,由于某种原因,这被视为 immutableMash 并且 key 的值最终成为哈希 [{"DEBUG"=>2}] 并且 value 最终成为 nil 对象,但我不确定为什么或如何更正它。
【问题讨论】:
-
如果有人在路上偶然发现,Dave Konopka 在下面的回答完美地回答了这个问题,但我觉得还有一个错字/错误应该指出。在 erb 中使用“puts”似乎没有按预期工作,所以 erb 应该看起来更像这样 github.com/Indemnity83/bakery/blob/…
标签: ruby chef-solo vagrantfile