简体   繁体   English

如何在Chef / Ruby execute命令中使用变量?

[英]How to use variables in Chef/Ruby execute command?

I'm creating a simple Chef cookbook that sets up vhosts on a VPS. 我正在创建一个简单的Chef食谱,用于在VPS上设置虚拟主机。 I have this working in terms of creating the correct .conf files by passing the domain variable to a Chef/.erb template, but I'm also trying to do this too (simplified code example): 我通过将domain变量传递给Chef / .erb模板来创建正确的.conf文件,但是我也在尝试这样做(简化的代码示例):

node[:domains].each do |domain|
    execute 'sudo mkdir -p /var/www/#{domain}/public_html'
end

However, the variables aren't being output into the string, and the command is simply creating a folder called: 但是,变量不会输出到字符串中,命令只是创建一个名为:

/var/www/#{domain}/public_html

So... is there an accepted way to use variables from a loop in a Chef execute command - or is it that my Ruby syntax is out of whack? 那么...在Chef执行命令中是否存在使用循环中的变量的公认方法-还是我的Ruby语法不合常规?

Use double quotes, not single quotes, when trying to inject variables into strings. 尝试将变量注入字符串时,请使用双引号而不是单引号。

node [: domains] .each do | domain |
     execute "create_dir_#{domain}" do
       command   "sudo mkdir /var/www/#{domain}/public_html"
     end
end

and I recommend using the directory resource rather than execute 我建议使用目录资源而不是执行

node [: domains] .each do | domain |
     directory "/var/www/#{domain}/public_html" do
       recursive true
       action :create
     end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM