【问题标题】:Chef ruby_block execution leads to errorChef ruby​​_block 执行导致错误
【发布时间】:2018-02-21 17:33:59
【问题描述】:

我写的一些chef 代码有问题。 ruby 块中的 shell 命令总是在执行其他所有操作之前执行。只要未安装软件包,bin 就无法使用。每次尝试以某种方式链接它都失败了。

错误:

Errno::ENOENT
-------------
No such file or directory - icinga2

Cookbook Trace:
---------------
  /var/tmp/cookbooks/hit_icinga2/recipes/icinga2_client.rb:25:in ``'
  /var/tmp/cookbooks/hit_icinga2/recipes/icinga2_client.rb:25:in `block in from_file'
  /var/tmp/cookbooks/hit_icinga2/recipes/icinga2_client.rb:23:in `from_file'
  /var/tmp/cookbooks/hit_icinga2/recipes/default.rb:8:in `from_file'

Relevant File Content:
----------------------
/var/tmp/cookbooks/hit_icinga2/recipes/icinga2_client.rb:

 18:    package 'icinga2' do
 19:      action :install
 20:    end
 21:  end
 22:
 23:  ruby_block 'name' do
 24:    # get Icinga version output
 25>>   get_icinga_version_information = %x[icinga2 -V]
 26:    #we clean up
 27:    #lines.first - just return first line of output
 28:    ##chomp - remove line break
 29:    #scan- match regex
 30:    #flatten - flatten array
 31:    #[n] - use array element number n
 32:    #to_s - convert to string
 33:    #split - split string at caracter
 34:    #gsub - search and replace

System Info:
------------
chef_version=13.6.4
platform=centos
platform_version=7.3.1611
ruby=ruby 2.4.2p198 (2017-09-14 revision 59899) [x86_64-linux]
program_name=chef-solo worker: ppid=7301;start=16:37:42;
executable=/opt/chefdk/bin/chef-solo

代码:

package 'icinga2' do
  action :install
end

ruby_block 'detect_icinga_version' do
  # get Icinga version output
  get_icinga_version_information = %x[icinga2 -V]
  #we clean up
  #lines.first - just return first line of output
  ##chomp - remove line break
  #scan- match regex
  #flatten - flatten array
  #[n] - use array element number n
  #to_s - convert to string
  #split - split string at caracter
  #gsub - search and replace
  #to_i - convert to integer
  clean_icinga_version =         get_icinga_version_information.lines.first.chomp.scan(/version: [^)]*/).flatten.to_s.scan(/[0-9\.-]+/)[0].to_s.split('-')[0].gsub('.', '').to_i
  if clean_icinga_version < 280
          icinga_pki_path = '/etc/icinga2/pki'
  else
          icinga_pki_path = '/var/lib/icinga2/certs'
  end
end

知道如何实现吗?

【问题讨论】:

    标签: ruby linux chef-infra devops


    【解决方案1】:

    您还需要将代码放在带有block do ... end 的块中。

    【讨论】:

      【解决方案2】:

      您必须在下面的行中给出 icinga2 的完整路径

      get_icinga_version_information = %x[icinga2 -V]

      或者将这个 icinga2 的父目录添加到环境变量中

      【讨论】:

        【解决方案3】:

        解决方案很有趣。 Chef 拒绝了适当的链接和设置条件。它现在是一个 Ohai 插件,在安装包后会重新加载。

        【讨论】:

          猜你喜欢
          • 2014-12-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-06-12
          • 2018-03-01
          • 1970-01-01
          相关资源
          最近更新 更多