shell - 让 ansible 在没有退出代码停止 ssh 连接的情况下运行脚本

标签 shell ssh ansible

我们有一个 ansible 任务,如下所示

- name: Check if that created
  script: verification.sh
  register: verification
  changed_when: verification.rc == 1

上述任务运行一个脚本,如果失败或成功则返回退出信号。一个示例部分是
    if [[ "$item" == "$name" ]]; then
        printf "TEST"
        exit 1
    fi

问题在于,当返回 0 值以外的退出信号时,ansible 中的 ssh 似乎终止并给出错误为
TASK [Test Task] ******************
fatal: [default]: FAILED! => {"changed": true, "failed": true, "rc": 1, "stderr": "Shared connection to 127.0.0.1 closed.\r\n", "stdout": "TEST", "stdout_lines": ["TEST"]}

然而,当我们在脚本中返回 0 的退出信号时,这是有效的

我猜这是因为“退出”在远程主机上运行,​​然后终止 ssh 连接。

我们如何绕过这个并返回退出信号而不出现错误。

最佳答案

您可以使用 failed_when控制what defines failure :

- name: Check if that created
  script: verification.sh
  register: verification
  changed_when: verification.rc == 1
  failed_when: verification.rc not in [0,1]
当退出代码既不是 0 也不是 1 时,这将导致失败。

关于shell - 让 ansible 在没有退出代码停止 ssh 连接的情况下运行脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44525784/

相关文章:

python - 如何使用特定的 HostkeyAlgorithms 配置 pysftp/paramiko 连接 - python

linux - 基于匹配属性构建列表(ansible)

git - 如何使git clone与ssh一起使用?

c# - 检查远程文件夹是否存在 - Renci.ssh

shell - 仅当存在第 3 个模式时 SED 删除(包括)2 个模式之间

shell - 如何使用 Ansible 自动化一次性任务?

ansible - 登录 Ansible 后切换用户

automation - Jira 应用程序属性存储在哪里

unix - 使用sudo运行时在shell脚本中获取$ USER?

linux - 在windows azure中设置ssh以命令行访问windows虚拟机