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

Ansible核心2.17.4忽略ansible.cfg超时配置,长时SSH任务100秒超时失败

解决Ansible SSH长任务100秒超时问题

我使用Ansible [core 2.17.4]执行SSH长运行任务时,总是在100秒后失败,报错信息如下:

"msg": "command timeout triggered, timeout value is 100 secs.
See the timeout setting options in the Network Debug and Troubleshooting Guide."

已在ansible.cfg中配置了各类300秒超时参数,但问题依旧:

[defaults]
inventory = inventory
host_key_checking = False
deprecation_warnings=False
forks = 200
display_skipped_hosts = false
retry_files_enabled = True
vars_plugins = plugins/vars
filter_plugins = plugins/filter
terminal_plugins = plugins/terminal
cliconf_plugins = plugins/cliconf
action_plugins = plugins/action
ansible_timeout = 300
timeout = 300
[persistent_connection]
command_timeout = 300
connect_timeout = 300
persistent_command_timeout = 300
persistent_connect_timeout = 300

以下是可行的排查与解决思路:

1. 补充SSH连接专属超时配置

当前配置未覆盖SSH连接插件的核心参数,需在ansible.cfg中添加ssh_connection段:

[ssh_connection]
# 保持SSH连接活跃,防止远端主动断开
ssh_args = -o ServerAliveInterval=30 -o ServerAliveCountMax=10
# 设置SSH连接超时
timeout = 300
# 控制套接字超时,适配长任务场景
control_path_timeout = 300

2. 给单个任务显式指定超时

若全局配置不生效,可在剧本的目标任务中直接设置超时变量:

- name: 执行长运行脚本
  command: /path/to/your-long-task.sh
  vars:
    # 命令执行超时
    ansible_command_timeout: 300
    # SSH连接超时
    ansible_ssh_timeout: 300

3. 检查远端SSH服务器配置

远端机器的sshd_config可能存在强制断开的设置,导致100秒超时。需修改远端配置:

# 每60秒发送一次存活检测
ClientAliveInterval 60
# 连续5次检测无响应则断开(总时长300秒)
ClientAliveCountMax 5

修改后重启sshd服务(如systemctl restart sshd)。

4. 验证配置是否正确加载

执行以下命令确认所有超时配置已生效,避免被其他优先级更高的配置文件(如~/.ansible.cfg)覆盖:

ansible-config dump | grep -E '(timeout|ssh_args)'

内容的提问来源于stack exchange,提问作者Jeff-C

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 02:00:04