与服务器的共享连接已关闭

时间:2017-03-08 16:02:32

标签: ssh ansible

当我运行时:

$ ansible -i s1, s1 -m raw -a 'echo test' -u root -k

我明白了:

s1 | SUCCESS | rc=0 >>
test
Shared connection to s1 closed.

但是这样:

$ ansible -i s1, s1 -m command -a 'echo test' -u root -k

我没有收到“与s1关闭的共享连接”。部分:

s1 | SUCCESS | rc=0 >>
test

为什么?

P.S。以上是重现问题的简化方法。我面临的是,当我在运行剧本时,我会得到这条额外的路线。

UPD 该行显然来自ssh。如果我用raw运行-vvvv命令,我会得到:

Using /etc/ansible/ansible.cfg as config file
Loading callback plugin minimal of type stdout, v2.0 from /usr/lib/python2.7/site-packages/ansible/plugins/callback/__init__.pyc
<s1> ESTABLISH SSH CONNECTION FOR USER: root
<s1> SSH: EXEC sshpass -d13 ssh -vvv -C -o ControlMaster=auto
  -o ControlPersist=60s -o User=root -o ConnectTimeout=10
  -o ControlPath=/home/yuri/.ansible/cp/ansible-ssh-%h-%p-%r -tt s1
  'echo test'
s1 | SUCCESS | rc=0 >>
test
OpenSSH_7.4p1, OpenSSL 1.0.2k  26 Jan 2017
debug1: Reading configuration data /home/yuri/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: auto-mux: Trying existing master
debug1: Control socket "/home/yuri/.ansible/cp/ansible-ssh-s1-22-root" does not exist
<...a lot of output from ssh...>

但是command,只是:

Using /etc/ansible/ansible.cfg as config file
Loading callback plugin minimal of type stdout, v2.0 from
  /usr/lib/python2.7/site-packages/ansible/plugins/callback/__init__.pyc
Using module file
  /usr/lib/python2.7/site-packages/ansible/modules/core/commands/command.py
<s1> ESTABLISH SSH CONNECTION FOR USER: root
<s1> SSH: EXEC sshpass -d13 ssh -vvv -C -o ControlMaster=auto
  -o ControlPersist=60s -o User=root -o ConnectTimeout=10
  -o ControlPath=/home/yuri/.ansible/cp/ansible-ssh-%h-%p-%r s1
  '/bin/sh -c '"'"'(
    umask 77
    && mkdir -p "` echo ~/.ansible/tmp/ansible-tmp-1488989540.6-73006073289737 `"
    && echo ansible-tmp-1488989540.6-73006073289737="` echo ~/.ansible/tmp/ansible-tmp-1488989540.6-73006073289737 `"
  ) && sleep 0'"'"''
<s1> PUT /tmp/tmpes82wL TO
  /root/.ansible/tmp/ansible-tmp-1488989540.6-73006073289737/command.py
<s1> SSH: EXEC sshpass -d13 sftp -o BatchMode=no -b - -vvv -C
  -o ControlMaster=auto -o ControlPersist=60s -o User=root
  -o ConnectTimeout=10
  -o ControlPath=/home/yuri/.ansible/cp/ansible-ssh-%h-%p-%r '[s1]'
<s1> ESTABLISH SSH CONNECTION FOR USER: root
<s1> SSH: EXEC sshpass -d13 ssh -vvv -C -o ControlMaster=auto
  -o ControlPersist=60s -o User=root -o ConnectTimeout=10
  -o ControlPath=/home/yuri/.ansible/cp/ansible-ssh-%h-%p-%r s1
  '/bin/sh -c '"'"'
    chmod u+x /root/.ansible/tmp/ansible-tmp-1488989540.6-73006073289737/ /root/.ansible/tmp/ansible-tmp-1488989540.6-73006073289737/command.py
    && sleep 0'"'"''
<s1> ESTABLISH SSH CONNECTION FOR USER: root
<s1> SSH: EXEC sshpass -d13 ssh -vvv -C -o ControlMaster=auto
  -o ControlPersist=60s -o User=root -o ConnectTimeout=10
  -o ControlPath=/home/yuri/.ansible/cp/ansible-ssh-%h-%p-%r -tt s1
  '/bin/sh -c '"'"'
    /usr/bin/python /root/.ansible/tmp/ansible-tmp-1488989540.6-73006073289737/command.py;
    rm -rf "/root/.ansible/tmp/ansible-tmp-1488989540.6-73006073289737/" > /dev/null 2>&1
    && sleep 0'"'"''
s1 | SUCCESS | rc=0 >>
test

所有ssh输出都在哪里?

1 个答案:

答案 0 :(得分:3)

  

与s1的共享连接已关闭。

此消息是来自ssh客户端的错误消息。

使用raw: echo test Ansible执行ssh <many parameters> s1 'echo test'并从 ssh 命令获取 stdout / stderr。这样就可以在任务结果中弹出有关共享连接的消息。

使用command: echo test Ansible copy python-wrapper(command.py)并执行此包装器,该包装器生成echo test并从 echo stdout / stderr strong>命令。然后command.pyecho的结果作为带有stdout / stderr / rc密钥的JSON对象打印出来。 ssh错误消息仍然出现,但你没有看到它(它被Ansible过滤),因为Ansible从JSON-object键得到任务结果,而不是从ssh plain stdout / stderr / rc得到。

  

所有ssh输出都去了哪里?

由于处理raw / command的区别相同,这是相关的。要查看详细的ssh输出集ANSIBLE_DEBUG=1环境变量。

如果要隐藏此错误消息,可以使用ansible_ssh_extra_args='-o LogLevel=QUIET'广告资源变量。但我不确定这是否会带来其他一些意想不到的结果。