Reputation: 1399
I am trying to make chef-solo create the knife.rb
files on the Jenkins slaves. So, I have the below in the attributes:
default['chef']['projects'] = ['ost', 'jt']
default['chef']['environments'] = ['dev', 'test', 'staging', 'production']
This is the block in the recipe:
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
And the template: 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
Now, with this, I am expecting 8 knife files namely and it does creates them.
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
The problem I am facing is with the below files. The other files for the project ost
in the array works as expected.
5. knife-123jt-dev.rb
6. knife-123jt-test.rb
7. knife-123jt-staging.rb
8. knife-123jt-production.rb
doesn't have the value of the project
variable populated in the template. Example, the file knife-123jt-dev.rb
, looks like
##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
The expected files should look like the below.
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-test
##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-production
##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
Upvotes: 1
Views: 100
Reputation: 4223
I believe you need to add parentheses.
:project => (project == 'ost' ? '' : project)
But I'd prefer:
:project => project unless project == 'ost'
Upvotes: 1