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

Chef Recipe问题:IIS应用池periodic_restart_schedule被设为空数组如何修复?

解决Chef Recipe修改IIS应用池时意外重置periodic_restart_schedule的问题

方法1:使用:modify动作替代:config动作

Chef的iis_pool资源中,:config动作会将所有未显式声明的属性重置为资源默认值(periodic_restart_schedule的默认值是空数组),而:modify动作仅更新你显式指定的属性,不会改动未声明的配置项。

修改后的Recipe代码:

iis_pool app_pool_name do
  action :modify
  runtime_version '4.0'
  pipeline_mode :Integrated
  identity_type :SpecificUser
  username svc_account_name
  password svc_account_password
  start_mode :AlwaysRunning 
  ignore_failure true
end

注意::modify动作要求应用池已经存在。如果你的Recipe需要同时创建应用池,需先执行:add动作,再单独用:modify动作配置属性:

# 先创建应用池
iis_pool app_pool_name do
  action :add
  runtime_version '4.0'
  pipeline_mode :Integrated
  identity_type :SpecificUser
  username svc_account_name
  password svc_account_password
  start_mode :AlwaysRunning 
  ignore_failure true
end

# 后续修改时用:modify,避免重置未声明属性
iis_pool app_pool_name do
  action :modify
  # 仅声明需要更新的属性,比如后续修改密码
  password svc_account_password
  ignore_failure true
end

方法2:显式保留当前的periodic_restart_schedule值(适用于必须用:config的场景)

如果因需求限制必须使用:config动作,可以通过PowerShell读取当前应用池的定期重启计划,再将其传递给iis_pool资源,从而保持原有配置不变。

示例代码:

# 执行PowerShell命令获取当前应用池的定期重启计划并转为JSON格式
schedule_output = powershell_out(%{
  $pool = Get-IISAppPool -Name "#{app_pool_name}"
  $pool.Recycling.PeriodicRestart.Schedule | ConvertTo-Json
}).stdout

# 解析JSON输出为Ruby数组,处理空输出情况
current_schedule = schedule_output.empty? ? [] : JSON.parse(schedule_output)

iis_pool app_pool_name do
  action :config
  runtime_version '4.0'
  pipeline_mode :Integrated
  identity_type :SpecificUser
  username svc_account_name
  password svc_account_password
  start_mode :AlwaysRunning 
  periodic_restart_schedule current_schedule
  ignore_failure true
end

这种方法需要处理PowerShell输出的解析逻辑,相对繁琐,仅推荐在无法使用:modify动作的场景下使用。

内容的提问来源于stack exchange,提问作者Sidhartha

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 10:12:48