You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Chef配方报错ResourceNotFound:service[edge-api]未找到,求解决

解决Chef配方中service资源找不到的错误

错误原因清晰:你的remote_file、template等资源在notify时指定了service[edge-api],但整个配方里没有定义Chef的service资源——Chef的notify是基于自身资源集合的,并非直接调用系统服务,因此必须显式声明对应的service资源。

另外还有两个潜在问题需要修正:

  • Ruby字符串插值错误:你使用了{application}格式,Ruby中正确的变量插值应该是#{application},否则变量不会被替换,会导致路径错误。
  • 冗余的Upstart配置:CentOS 7使用systemd,/etc/init/edge-api.conf这个Upstart模板已无用,应当移除。

修改后的配方代码:

application = "edge-api"

remote_file "/usr/local/edge-api/#{application}-#{application_version}.jar" do
  source "#{node.default["thecloud"]["repo_url"]}/#{application}/#{application}-#{application_version}.jar"
  mode "0644"
  checksum application_sha256
  notifies :restart, "service[edge-api]", :delayed
end

template "/usr/local/edge-api/application.properties" do
  owner "root"
  group "edge-api"
  mode "0640"
  variables(
    :clouddbIp => node['tcDatabase']['clouddb']['ip'],
    :clouddwhIp => node['tcDatabase']['clouddwh']['ip'],
    :elasticnodes => elasticnodes
  )
  notifies :restart, "service[edge-api]", :delayed
end

template "/usr/local/edge-api/logback.xml" do
  owner "root"
  group "edge-api"
  mode "0640"
end

template "/etc/systemd/system/#{application}.service" do
  source "#{application}.service.erb"
  owner "root"
  group "root"
  mode "0644"
  notifies :run, 'execute[daemon-reload]', :immediately
end

execute 'daemon-reload' do
  command 'systemctl daemon-reload'
  action :nothing
end

# 新增:定义Chef的systemd service资源
service 'edge-api' do
  action [:enable, :start]
  provider Chef::Provider::Service::Systemd
  # 当systemd配置文件更新时自动重启服务
  subscribes :restart, 'template[/etc/systemd/system/edge-api.service]', :immediately
end

关键修改点说明:

  1. 添加service资源:显式声明service[edge-api],指定systemd provider(Chef 12+会自动识别systemd环境,但显式声明更稳妥),设置开机自启和启动动作。
  2. 修正字符串插值:将所有{application}、{application_version}改为#{application}、#{application_version},确保变量正确替换。
  3. 简化notify动作:把原来的先stop再start改为直接restart,更简洁高效。
  4. 移除冗余Upstart模板:删除/etc/init/edge-api.conf的template块,避免生成无用文件。
  5. 添加service订阅:让service资源在systemd配置文件更新时自动重启,确保新配置生效。

内容的提问来源于stack exchange,提问作者Charan Raj Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 00:47:26