【问题标题】:Using Command in Ansible playbook throws "Failed to connect to the host via ssh"在 Ansible 剧本中使用命令抛出“无法通过 ssh 连接到主机”
【发布时间】:2017-06-08 16:14:27
【问题描述】:

我的剧本中有三个任务。对于所有这些,Ansible 需要连接到清单文件中指定的主机。前两个任务执行得很好。第三个任务说

<10.182.1.23> ESTABLISH SSH CONNECTION FOR USER: root
<10.182.1.23> SSH: EXEC sshpass -d12 ssh -C -q -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o User=root -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/ansible-ssh-%h-%p-%r 10.182.1.23 '/bin/sh -c '"'"'( umask 77 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1485219301.67-103341754305609 `" && echo ansible-tmp-1485219301.67-103341754305609="` echo $HOME/.ansible/tmp/ansible-tmp-1485219301.67-103341754305609 `" ) && sleep 0'"'"''
fatal: [10.182.1.23]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh.", "unreachable": true}

这是我的剧本 This is a screenshot of my playbook

这里是 playbook.yml

---
- hosts: all
  strategy: debug
  gather_facts: no
  vars:
    contents: "{{ lookup('file','/etc/redhat-release')}}"
    mydate: "{{lookup('pipe','date +%Y%m%d%H%M%S**.%5N**')}}"
  tasks:
    - name: cat file content
      debug: msg='the content of file is {{contents}} at date {{mydate}}.'
    - name: second task
      debug: msg='this is second task at time {{mydate}}.'
    - name: fourth task
      command: sudo cat /etc/redhat-release
      register: result
    - debug: var=result

这是我的库存文件

[hosts]
10.182.1.23  ansible_connection=ssh ansible_ssh_user=username ansible_ssh_pass=passphrase

我无法理解它是如何连接到主机以执行前两个任务的,以及为什么它会为第三个任务抛出错误。 我是使用 Ansible 的新手。请帮我解决这个问题。

【问题讨论】:

    标签: ssh ansible


    【解决方案1】:

    我的剧本中有三个任务。对于所有这些,Ansible 需要连接到清单文件中指定的主机。

    那不是真的。所有查找插件都在控制机器上本地执行它们的操作。

    "Lookups: Like all templating, these plugins are evaluated on the Ansible control machine"

    我无法理解它是如何连接到主机来执行前两个任务的,以及为什么它会为第三个任务抛出错误。

    因为您的前两个任务使用带有查找插件的 debug 模块。他们只是将msg 参数的值打印到输出中,不需要连接到远程主机。

    所以你的前两个任务显示本地文件/etc/redhat-release 和本地日期时间的内容。

    第三个任务尝试在目标机器上运行代码,但连接失败。

    【讨论】:

      【解决方案2】:

      尝试下面的 linux 命令来确定 ssh 是否完美。

      ssh remoteuser@remoteserver
      

      它不应该提示输入密码。

      【讨论】:

        猜你喜欢
        • 2017-08-08
        • 2017-02-08
        • 2018-07-22
        • 1970-01-01
        • 2023-03-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多