Ansible – x.x.x.x | UNREACHABLE! => { “changed”: false, “msg”: “Failed to connect to the host via ssh … }

ansibleansible-playbook

I created a cluster and i try to reach the master node from an another Ansible server. I copied the ssh key to the master server. and i can reach it via below command, from ansible server

ssh -i ~/.ssh/id_rsa ubuntu@x.x.x.x

but i cant reach it with

sudo ansible-playbook -i hosts /etc/ansible/playbook/test.yml

And i took this error.

x.x.x.x | UNREACHABLE! => { "changed": false, "msg": "Failed to connect to the host via ssh: ssh: connect to host x.x.x.x port 22: Connection timed out\r\n", "unreachable": true }

May you help me please? Other server fault issues didnt help me.

Note:

x.x.x.x (this is master node ip)


PLAY [nodes] ***************************************************************************************************************** TASK [Gathering Facts] *******************************************************************************************************

ok: [t.t.t.t]

fatal: [x.x.x.x]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: connect to host x.x.x.x port 22: Connection timed out\r\n", "unreachable": true}

TASK [test] *********************************************************************************

PLAY RECAP *******************************************************************************************************************

x.x.x.x : ok=0 changed=0 unreachable=1 failed=0

t.t.t.t : ok=1 changed=0 unreachable=0 failed=1```

Best Answer

The ansible-playbook command probably fails because the path to the ssh-key is not defined.

Ansible recommends to use the ssh-agent:

$ ssh-agent bash
$ ssh-add ~/.ssh/id_rsa

Another way to add private key files without using ssh-agent is using ansible_ssh_private_key_file in an inventory file as explained here

https://docs.ansible.com/ansible/latest/user_guide/connection_details.html