user363436
user363436

Reputation: 1

Using Command in Ansible playbook throws "Failed to connect to the host via ssh"

I have three tasks in my playbook. For all of those, Ansible needs to connect to hosts specified in the inventory file. The first two tasks executed well. The third task says

<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}

Here is my playbook This is a screenshot of my playbook

Here is 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

Here is my inventory file

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

I am not able to understand how it is able to connect to the host for the top two tasks and why it threw an error for the third. I am new to using Ansible. Please help me with this.

Upvotes: 0

Views: 1185

Answers (2)

Praveen Raj Kumar
Praveen Raj Kumar

Reputation: 208

try below linux command to determine whether ssh is flawless.

ssh remoteuser@remoteserver

It shouldn't prompt for password.

Upvotes: 1

techraf
techraf

Reputation: 68439

I have three tasks in my playbook. For all of those, Ansible needs to connect to hosts specified in the inventory file.

That's not true. All lookup plugins perform their actions locally on the control machine.

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

I am not able to understand how it is able to connect to the host for the top two tasks and why it threw an error for the third.

Because your first two tasks use the debug module with lookup plugins. They just print the value of msg argument to the output and do not require connection to the remote host.

So your first two tasks display the contents of the local file /etc/redhat-release and local date-time.

The third task tries to run the code on the target machine and fails to connect.

Upvotes: 2

Related Questions