How do you pass Knife a config file and pem key via command line?

672 Views Asked by At

I want to run Knife in my CICD server to upload cookbooks.

I run ephemeral slave so creating a static directory with all the files in the place knife expects by default would be impossible or painful (to write out that structure every time the container slave runs).

Can I pass the Knife command a knife.rb config as text and also a pem file as args?

I'm trying to use the --config-option but Knife is still demanding a configuration file:

knife status \
--config-option log_level=:info \
--config-option log_location=STDOUT \
--config-option node_name="myadminuser" \
--config-option client_key="admin.pem" \
--config-option chef_server_url="https://myserver.com/organizations/myorg"

get error:

WARNING: No knife configuration file found. See https://docs.chef.io/config_rb/ for details.
Traceback (most recent call last):
        25: from /bin/knife:360:in `<main>'
        24: from /bin/knife:360:in `load'
        23: from /opt/chef-workstation/embedded/lib/ruby/gems/2.7.0/gems/chef-16.3.45/bin/knife:24:in `<top (required)>'
        22: from /opt/chef-workstation/embedded/lib/ruby/gems/2.7.0/gems/chef-16.3.45/lib/chef/application/knife.rb:163:in `run'
        21: from /opt/chef-workstation/embedded/lib/ruby/gems/2.7.0/gems/chef-16.3.45/lib/chef/knife.rb:228:in `run'
        20: from /opt/chef-workstation/embedded/lib/ruby/gems/2.7.0/gems/chef-16.3.45/lib/chef/knife.rb:469:in `configure_chef'
        19: from /opt/chef-workstation/embedded/lib/ruby/gems/2.7.0/gems/chef-config-16.3.45/lib/chef-config/config.rb:134:in `apply_extra_config_options'
        18: from /opt/chef-workstation/embedded/lib/ruby/gems/2.7.0/gems/chef-config-16.3.45/lib/chef-config/config.rb:134:in `inject'
        17: from /opt/chef-workstation/embedded/lib/ruby/gems/2.7.0/gems/chef-config-16.3.45/lib/chef-config/config.rb:134:in `each'
        16: from /opt/chef-workstation/embedded/lib/ruby/gems/2.7.0/gems/chef-config-16.3.45/lib/chef-config/config.rb:146:in `block in apply_extra_config_options'
        15: from /opt/chef-workstation/embedded/lib/ruby/2.7.0/psych.rb:360:in `safe_load'
        14: from /opt/chef-workstation/embedded/lib/ruby/2.7.0/psych/visitors/to_ruby.rb:32:in `accept'
        13: from /opt/chef-workstation/embedded/lib/ruby/2.7.0/psych/visitors/visitor.rb:6:in `accept'
        12: from /opt/chef-workstation/embedded/lib/ruby/2.7.0/psych/visitors/visitor.rb:16:in `visit'
        11: from /opt/chef-workstation/embedded/lib/ruby/2.7.0/psych/visitors/to_ruby.rb:313:in `visit_Psych_Nodes_Document'
        10: from /opt/chef-workstation/embedded/lib/ruby/2.7.0/psych/visitors/to_ruby.rb:32:in `accept'
         9: from /opt/chef-workstation/embedded/lib/ruby/2.7.0/psych/visitors/visitor.rb:6:in `accept'
         8: from /opt/chef-workstation/embedded/lib/ruby/2.7.0/psych/visitors/visitor.rb:16:in `visit'
         7: from /opt/chef-workstation/embedded/lib/ruby/2.7.0/psych/visitors/to_ruby.rb:123:in `visit_Psych_Nodes_Scalar'
         6: from /opt/chef-workstation/embedded/lib/ruby/2.7.0/psych/visitors/to_ruby.rb:60:in `deserialize'
         5: from /opt/chef-workstation/embedded/lib/ruby/2.7.0/psych/scalar_scanner.rb:74:in `tokenize'
         4: from /opt/chef-workstation/embedded/lib/ruby/2.7.0/psych/class_loader.rb:82:in `symbolize'
         3: from /opt/chef-workstation/embedded/lib/ruby/2.7.0/psych/class_loader.rb:32:in `symbolize'
         2: from /opt/chef-workstation/embedded/lib/ruby/2.7.0/psych/class_loader.rb:39:in `block (2 levels) in <class:ClassLoader>'
         1: from /opt/chef-workstation/embedded/lib/ruby/2.7.0/psych/class_loader.rb:28:in `load'
/opt/chef-workstation/embedded/lib/ruby/2.7.0/psych/class_loader.rb:97:in `find': Tried to load unspecified class: Symbol (Psych::DisallowedClass)
2

There are 2 best solutions below

2
Mr. On

since the knife configuration is usually a one-time setup, there is not need to configure it each time or dynamically.

rather, you can have your knife configuration (knife.rb or client.rb) placed in .chef directory in your chef repository (.chef/knife.rb)

plus, since the configurations are written in ruby, you can use environment variable in the configurations.

if you have multiple knife configurations, use knife-spork to switch between them

0
seshadri_c On

You could try something like - create (touch) a dummy config.rb file, followed by passing all the options with --config-option.

Example:

touch config.rb; \
knife status \
--config config.rb
--config-option log_level=:info \
--config-option log_location=STDOUT \
--config-option node_name="myadminuser" \
--config-option client_key="admin.pem" \
--config-option chef_server_url="https://myserver.com/organizations/myorg"