播放过程中出现 Ansible SSH 错误 [英] Ansible SSH error during play

查看:22
本文介绍了播放过程中出现 Ansible SSH 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 Ansible 时遇到一个奇怪的错误.首先,第一个角色工作正常,但是当 Ansible 尝试执行第二个角色时,由于 ssh 错误而失败.

I get a strange error with Ansible. First of all, the first role works fine but when Ansible tries to execute the seconde one it failed because of ssh error.

环境:

  • 操作系统:CentOS 7
  • Ansible 版本:2.2.1.0
  • Python 版本:2.7.5
  • OpenSSH 版本:OpenSSH_6.6.1p1、OpenSSL 1.0.1e-fips 2013 年 2 月 11 日

执行的 Ansible 命令:

ansible-playbook -vvvv -i inventory/dev playbook_update_system.yml --limit "db[0]"

手册:

- name: "HUB Playbook | Updating system packages on {{ ansible_hostname }}"
  hosts: release_first_half
  roles:
    - upgrade_system_package
    - reboot_server

角色:upgrade_system_package:

- name: "upgrading CentOS system packages on {{ ansible_hostname }}"
  shell: sudo puppet apply -e 'exec{"upgrade-package":command => "/usr/bin/yum clean all; /usr/bin/yum -y update;"}'
  when: ansible_distribution == 'CentOS' and 'cassandra' not in group_names

角色:reboot_server:

- name: "reboot CentOS [{{ ansible_hostname }}] server"
  shell: sudo puppet apply -e 'exec{"reboot-os":command => "/usr/sbin/reboot"}'
  when: ansible_distribution == 'CentOS' and 'cassandra' not in group_names

当前行为:

  1. 连接到db1"节点并执行角色升级系统包"=> OK
  2. 尝试连接到db1"并执行角色rebo​​ot_server"=> 由于 ssh 失败.

Ansible 返回的错误消息:

致命:[db1]:无法访问!=> {改变":假,"msg": "无法通过 ssh 连接到主机:OpenSSH_6.6.1,OpenSSL 1.0.1e-fips 2013 年 2 月 11 日\r\ndebug1:读取配置数据/USR/newtprod/.ssh/config\r\ndebug1:读取配置数据/etc/ssh/ssh_config\r\ndebug1:/etc/ssh/ssh_config line 56: Applying options for *\r\ndebug1: auto-mux: Trying existing master\r\ndebug2: fd 3 setting O_NONBLOCK\r\ndebug2: mux_client_hello_exchange: 主版本 4\r\ndebug3: mux_client_forwards: 请求转发: 0 本地, 0 远程\r\ndebug3: mux_client_request_session: 进入\r\ndebug3: mux_client_request_alive: 进入\r\ndebug3: mux_client_request_done = 0r\ndebug3: mux_client_request_session: 会话请求已发送\r\ndebug1: mux_client_request_session: 主会话 ID: 2\r\ndebug3: mux_client_read_packet: 读取头失败: 管道损坏\r\ndebug2: 控制主机意外终止\r\n与 db1 的共享连接已关闭.\r\n",无法访问":true}

fatal: [db1]: UNREACHABLE! => { "changed": false, "msg": "Failed to connect to the host via ssh: OpenSSH_6.6.1, OpenSSL 1.0.1e-fips 11 Feb 2013\r\ndebug1: Reading configuration data /USR/newtprod/.ssh/config\r\ndebug1: Reading configuration data /etc/ssh/ssh_config\r\ndebug1: /etc/ssh/ssh_config line 56: Applying options for *\r\ndebug1: auto-mux: Trying existing master\r\ndebug2: fd 3 setting O_NONBLOCK\r\ndebug2: mux_client_hello_exchange: master version 4\r\ndebug3: mux_client_forwards: request forwardings: 0 local, 0 remote\r\ndebug3: mux_client_request_session: entering\r\ndebug3: mux_client_request_alive: entering\r\ndebug3: mux_client_request_alive: done pid = 64994\r\ndebug3: mux_client_request_session: session request sent\r\ndebug1: mux_client_request_session: master session id: 2\r\ndebug3: mux_client_read_packet: read header failed: Broken pipe\r\ndebug2: Control master terminated unexpectedly\r\nShared connection to db1 closed.\r\n", "unreachable": true }

我不明白,因为之前的角色已经在这个节点上成功执行了.此外,我们有很多使用相同清单文件的剧本,它们运行良好.我也在另一个节点上尝试过,但结果相同.

I don't understand because the previous role has been executed successfully on this node. Moreover, we have a lot of playbook which are using same inventory file and they works fine. I tried on another node too but same result.

推荐答案

这是一个简单且众所周知的问题:关闭过程会导致 SSH 守护进程退出,这会中断当前的 SSH 会话(您会得到管道损坏"错误).服务器正常重启,但 Ansible 流程中断.

It's a simple and pretty well-known issue: the shutdown process causes SSH daemon to quit and this breaks the current SSH session (you get the "broken pipe" error). The server reboots properly, but Ansible flow gets interrupted.

您需要为 shell 命令添加延迟并使用 async 选项运行它,以便 Ansible 的 SSH 会话可以在它被终止之前完成.

You need to add a delay to your shell command and run it with async option, so that Ansible's SSH session can finish before it gets killed.

shell: sleep 5; sudo puppet apply -e 'exec{"reboot-os":command => "/usr/sbin/reboot"}'
async: 0
poll: 0

这篇关于播放过程中出现 Ansible SSH 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆