2017-01-24 216 views
0

我在我的手册中有三个任务。对于所有这些,Ansible需要连接到清单文件中指定的主机。前两项任务执行得很好。第三项任务说在Ansible playbook中使用命令会抛出“无法通过ssh连接到主机”

<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 

我不能了解它如何能够连接到主机的前两个任务和wh它为第三个投掷错误。 我是使用Ansible的新手。请帮我解决一下这个。

回答

2

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

这是不正确的。所有查找插件在控制机器上本地执行其操作。

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

我无法了解它是如何能够连接到主机的顶部两个任务,以及为什么它扔了一个错误的三分之一。

因为前两个任务使用带有查找插件的debug模块。它们只是将输出值msg输出到输出,并且不需要连接到远程主机。

因此,您的前两个任务显示本地文件/etc/redhat-release和本地日期时间的内容。

第三项任务尝试运行目标机器上的代码并且无法连接。

0

尝试在linux命令下面确定ssh是否完美无缺。

ssh [email protected] 

它不应该提示输入密码。

相关问题