2017-01-24 112 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] 

它不應該提示輸入密碼。

相關問題