【问题标题】:Chef - Pass value from one resource to other resource厨师 - 将价值从一种资源传递到另一种资源
【发布时间】:2021-11-09 13:32:32
【问题描述】:

这是我的一段主厨 ruby​​ 代码,我正在尝试将一个值从一个块传递到另一个块。 我们在服务器上运行了多个端口,下面的代码将检查端口,如果它没有运行,它将尝试执行第二个块。我附上了代码和错误。请帮助我哪里出错了。

ports.each do |port|
  bash "Check_port_running_#{port}" do
   code <<-EOH
     pidval=$(ps -ef | grep /opt/redis/src/redis-server | grep *:#{port} | grep -vc grep)
     if (($pidval == 0));then
       portval_#{port}=true
     else
       portval_#{port}=false
     fi
   EOH
  end

  bash "Starting_redis_after_crash_for_port - #{port}" do
    only_if { "portval_#{port}" }
    code <<-EOH
      rm -f #{port}.pid
      sudo service redis@#{port} start
    EOH
  end
end

错误:低于错误

       ================================================================================
       Error executing action `run` on resource 'bash[Starting_redis_after_crash_for_port - 54321]'
       ================================================================================

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

       Resource Declaration:
       ---------------------
       # In /tmp/kitchen/cache/cookbooks/csg_orderservices_redis/recipes/configure.rb

       184:   bash "Starting_redis_after_crash_for_port - #{port}" do
       185:     only_if "portval_#{port}"
       186:     code <<-EOH
       187:       rm -f #{port}.pid
       188:       sudo service redis@#{port} start
       189:     EOH
       190:   end
       191: end

       Compiled Resource:
       ------------------
       # Declared in /tmp/kitchen/cache/cookbooks/csg_orderservices_redis/recipes/configure.rb:184:in `block in from_file'

       bash("Starting_redis_after_crash_for_port - 54321") do
         action [:run]
         default_guard_interpreter :default
         backup 5
         interpreter "bash"
         declared_type :bash
         cookbook_name "csg_orderservices_redis"
         recipe_name "configure"
         code "      rm -f 54321.pid\n      sudo service redis@54321 start\n"
         domain nil
         user nil
         only_if "portval_54321"
       end

【问题讨论】:

  • only_if 守卫应该评估为true/false。您可能需要一个表达式来比较它并返回 true 或 false 状态。
  • 或者您是否希望找到具有该名称的文件(根据错误)?
  • 不..我不期待任何文件..不知道为什么找不到目录或文件,
  • 那么当你使用only_if的时候,你想和portval_#{port}匹配什么条件呢?
  • 试图从第一个块中获取真值或假值。

标签: ruby shell chef-infra


【解决方案1】:

我的意见是避免为此使用 bash 资源。这是为了确保您的 Chef 食谱是幂等的,并且可以简化您的代码,因为大部分繁重的工作都是由 Chef 客户端完成的。我只在不得已的情况下才使用 bash、powershell 等资源。

以下是我的建议和示例:

在食谱的库文件夹中创建一个名为 redis.rb 的库文件。我更喜欢命名我的库以帮助组织我的方法。这个redis_active? 方法可以在你的食谱中包含这个模块的任何地方使用。

库/redis.rb:

# Replace CookbookName with actual cookbook name

module CookbookName
  module Redis
    include Chef::Mixin::ShellOut

    def redis_active?(port)
      cmd = "ps -ef | grep /opt/redis/src/redis-server | grep *:#{port} | grep -vc grep"
      pid_exists = shell_out(cmd).stdout.strip.to_i

      return true if pid_exists > 0
    end
  end
end

在您的 redis 配方中,file 资源只有在 redis_active? 方法为指定端口返回 false 时才会执行操作,然后 file 资源将收敛并删除 pid 文件。当这个资源收敛时,会通知service资源立即重启redis服务器服务。

recipes/redis.rb:

# Replace CookbookName with actual cookbook name

Chef::Resource::File.send(:include, CookbookName::Redis)

ports.each do |port|
  file "/path/to/#{port}.pid" do
    action :delete
    not_if { redis_active?(port) }
    notifies :restart, "service[redis@#{port}]", :immediately
  end

  service "redis@#{port}" do
    action :nothing
  end
end

【讨论】:

    猜你喜欢
    • 2015-06-25
    • 2020-12-20
    • 1970-01-01
    • 2014-05-02
    • 2020-03-02
    • 1970-01-01
    • 2013-07-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多