ruby迭代或循环失败按预期

时间:2015-07-27 23:33:42

标签: ruby chef

我正在尝试让厨师独奏在Jenkins奴隶上创建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刀文件,它确实创造了它们。

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-分期

##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

1 个答案:

答案 0 :(得分:1)

我相信你需要添加括号。

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

但我更喜欢:

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