【问题标题】:Ansible task failed while executing the specific long running taskAnsible 任务在执行特定的长时间运行任务时失败
【发布时间】:2018-01-24 10:08:04
【问题描述】:

在一次运行所有任务时,Ansible 在运行需要 26 小时才能完成的任务之间断开了 SSH 会话,但在执行 6 小时后,ansible 断开了 SSH 会话。目标服务器 SSH 配置以保持会话如下:

ClientAliveInterval 172000
ClientAliveCountMax 10

Ansible 任务:

- name: Executing script
  remote_user: "{{admin_user}}"
  become: yes
  shell: sudo -u test bash ./customscript.sh  > /log_dir/customscript.log 2>&1
  args:
    chdir: "deployment_source/common"
  tags:
     - custom-test

在下面找到错误日志:

22:11:44 TASK [role-deployment : Executing script] ************
22:11:44 fatal: [x.x.x.x]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Shared connection to x.x.x.x closed.\r\n", "unreachable": true}
22:11:44 
22:11:44 NO MORE HOSTS LEFT *************************************************************
22:11:44  to retry, use: --limit @/opt/ansible/test/deployment.retry
22:11:44 
22:11:44 PLAY RECAP *********************************************************************
22:11:44 x.x.x.x : ok=6    changed=2    unreachable=1    failed=0

请告知,断开连接的问题是什么?怎么解决?

【问题讨论】:

    标签: linux ssh ansible devops


    【解决方案1】:

    你永远不应该期望网络连接能稳定那么长时间。

    Ansible 中有 async 机制来处理长时间运行的作业。

    将您的代码重构为:

    - name: Executing script
      remote_user: "{{admin_user}}"
      become: yes
      shell: sudo -u test bash ./customscript.sh  > /log_dir/customscript.log 2>&1
      args:
        chdir: "deployment_source/common"
      async: 180000
      poll: 60
      tags:
         - custom-test
    

    让您的任务最多执行 50 小时,并每 60 秒检查一次是否完成。

    【讨论】:

    • 我有一个 ansible 任务,它只在一些服务器和一些客户端上失败,错误为UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Shared connection to [host] closed.", "unreachable": true} 使用async 选项立即停止了失败,使我不必深入研究无数的 ssh 配置。从现在开始,对于需要一两分钟以上的任务,这将是我的首选。谢谢!
    猜你喜欢
    • 2020-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-07
    • 2022-10-23
    • 2021-10-15
    • 1970-01-01
    相关资源
    最近更新 更多