【问题标题】:How do you pass Knife a config file and pem key via command line?如何通过命令行向 Knife 传递配置文件和 pem 密钥?
【发布时间】:2020-12-03 20:21:08
【问题描述】:

我想在我的 CICD 服务器上运行 Knife 来上传食谱。

我运行临时从站,因此在默认情况下创建一个包含所有文件的静态目录是不可能的或痛苦的(每次容器从站运行时都写出该结构)。

我可以通过 Knife 命令将 knife.rb 配置作为文本传递,并将 pem 文件作为 args 传递吗?

我正在尝试使用--config-option,但 Knife 仍然需要配置文件:

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"

得到错误:

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)

【问题讨论】:

  • --config-option OPTION=VALUE 允许您“覆盖单个配置选项”,所以也许您应该使用--config 指向一个(空?)配置文件?

标签: chef-infra knife


【解决方案1】:

由于刀配置通常是一次性设置,因此不需要每次或动态配置。

相反,您可以将您的knife configuration (knife.rb or client.rb) 放在.chef directory in your chef repository (.chef/knife.rb)

另外,由于配置是用 ruby​​ 编写的,您可以在配置中使用环境变量。

如果您有多个刀配置,请使用knife-spork to switch between them

【讨论】:

  • 如我的帖子中所述,我的用例是带有容器从属的 CICD——这就是所有现代 cicd 系统的工作方式。我也有需要动态配置的开发和生产环境
  • 动态创建您的刀\客户端配置(或使用环境变量),然后使用刀叉使用所需的配置。
【解决方案2】:

您可以尝试类似的方法 - 创建 (touch) 一个虚拟的 config.rb 文件,然后使用 --config-option 传递所有选项。

例子:

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"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-17
    • 2014-11-05
    • 2019-01-17
    • 2023-01-20
    • 2012-09-02
    • 1970-01-01
    • 1970-01-01
    • 2014-06-26
    相关资源
    最近更新 更多