ruby 迭代或循环按预期失败

ruby Iteration or loop failing as expected

我正在尝试让 chef-solo 在 Jenkins slaves 上创建 knife.rb 文件。所以,我在属性中有以下内容:

default['chef']['projects'] = ['ost', 'jt']
default['chef']['environments'] = ['dev', 'test', 'staging', 'production']

这是配方中的方块:

node[:chef][:projects].each do |project|
  node[:chef][:environments].each do |env|
    template "#{node[:jenkins][:master][:home]}/.chef/knife-123#{project}-#{env}.rb" do
      source 'knife.rb.erb'
      owner 'jenkins'
      group 'jenkins'
      variables(
        :environment => env,
        :project => project == 'ost' ? '' : project
      )
    end
  end
end

和模板:knife.rb.erb

##This file is generated by Chef

current_dir = File.dirname(__FILE__)
log_level                :info
log_location             STDOUT
node_name                "jenkins-ro"
client_key               "#{current_dir}/jenkins-ro.pem"
validation_client_name   "123<%=@project%>-<%= @environment %>-validator"
validation_key           "#{current_dir}/123<%=@project%>-<%= @environment %>-validator.pem"
chef_server_url          "https://CHEF_URL/organizations/123<%=@project%>-<%= @environment %>"
cache_type               'BasicFile'
cache_options( :path => "#{ENV['HOME']}/.chef/checksums" )
cookbook_path            ["#{current_dir}/../cookbooks"]
ssl_verify_mode          :verify_none

现在,有了这个,我期待 8 个 knife 文件,它确实创建了它们。

1. knife-123ost-dev.rb
2. knife-123ost-test.rb
3. knife-123ost-staging.rb
4. knife-123ost-production.rb
5. knife-123jt-dev.rb
6. knife-123jt-test.rb
7. knife-123jt-staging.rb
8. knife-123jt-production.rb

我面临的问题是以下文件。数组中项目 ost 的其他文件按预期工作。

5. knife-123jt-dev.rb
6. knife-123jt-test.rb
7. knife-123jt-staging.rb
8. knife-123jt-production.rb

没有在模板中填充 project 变量 的值。例如,文件 knife-123jt-dev.rb,看起来像

##This file is generated by Chef

current_dir = File.dirname(__FILE__)
log_level                :info
log_location             STDOUT
node_name                "jenkins-ro"
client_key               "#{current_dir}/jenkins-ro.pem"
validation_client_name   "123-dev-validator"
validation_key           "#{current_dir}/123-dev-validator.pem"
chef_server_url          "https://CHEF_URL/organizations/123-dev"
cache_type               'BasicFile'
cache_options( :path => "#{ENV['HOME']}/.chef/checksums" )
cookbook_path            ["#{current_dir}/../cookbooks"]
ssl_verify_mode          :verify_none

预期的文件应如下所示。

123jt-dev

##This file is generated by Chef

current_dir = File.dirname(__FILE__)
log_level                :info
log_location             STDOUT
node_name                "jenkins-ro"
client_key               "#{current_dir}/jenkins-ro.pem"
validation_client_name   "123jt-dev-validator"
validation_key           "#{current_dir}/123jt-dev-validator.pem"
chef_server_url          "https://CHEF_URL/organizations/123jt-dev"
cache_type               'BasicFile'
cache_options( :path => "#{ENV['HOME']}/.chef/checksums" )
cookbook_path            ["#{current_dir}/../cookbooks"]
ssl_verify_mode          :verify_none

123jt-测试

##This file is generated by Chef

current_dir = File.dirname(__FILE__)
log_level                :info
log_location             STDOUT
node_name                "jenkins-ro"
client_key               "#{current_dir}/jenkins-ro.pem"
validation_client_name   "123jt-test-validator"
validation_key           "#{current_dir}/123jt-test-validator.pem"
chef_server_url          "https://CHEF_URL/organizations/123jt-test"
cache_type               'BasicFile'
cache_options( :path => "#{ENV['HOME']}/.chef/checksums" )
cookbook_path            ["#{current_dir}/../cookbooks"]
ssl_verify_mode          :verify_none

123jt-staging

##This file is generated by Chef

current_dir = File.dirname(__FILE__)
log_level                :info
log_location             STDOUT
node_name                "jenkins-ro"
client_key               "#{current_dir}/jenkins-ro.pem"
validation_client_name   "123jt-staging-validator"
validation_key           "#{current_dir}/123jt-staging-validator.pem"
chef_server_url          "https://CHEF_URL/organizations/123jt-staging"
cache_type               'BasicFile'
cache_options( :path => "#{ENV['HOME']}/.chef/checksums" )
cookbook_path            ["#{current_dir}/../cookbooks"]
ssl_verify_mode          :verify_none

123jt-生产

##This file is generated by Chef

current_dir = File.dirname(__FILE__)
log_level                :info
log_location             STDOUT
node_name                "jenkins-ro"
client_key               "#{current_dir}/jenkins-ro.pem"
validation_client_name   "123jt-production-validator"
validation_key           "#{current_dir}/123jt-production-validator.pem"
chef_server_url          "https://CHEF_URL/organizations/123jt-production"
cache_type               'BasicFile'
cache_options( :path => "#{ENV['HOME']}/.chef/checksums" )
cookbook_path            ["#{current_dir}/../cookbooks"]
ssl_verify_mode          :verify_none

我认为您需要添加括号。

:project => (project == 'ost' ? '' : project)

但我更愿意:

:project => project unless project == 'ost'