厨师ruby_block执行导致错误

时间:2018-01-26 16:53:49

标签: ruby linux chef devops

我写的一些chef代码存在问题。 ruby块中的shell命令总是先执行其他操作。只要未安装包,就不能使用垃圾箱。每次试图以某种方式将其链接失败。

ERROR:

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

CODE:

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

知道如何实现这一目标吗?

3 个答案:

答案 0 :(得分:0)

您还需要将代码放在包含 a{text-decoration: none;} 的块中。

答案 1 :(得分:0)

你必须在下面的行中给出icinga2的整个路径

get_icinga_version_information =%x [icinga2 -V]

或将此icinga2的父目录添加到环境变量

答案 2 :(得分:0)

解决方案很有趣。厨师拒绝正确的链接并设定条件。它现在是一个Ohai插件,在包安装发生后重新加载。