This guide will show you how to fix issue on Ansible for hosts file inventory failed to connect to the host via ssh.
Basic inventory for Ansible hosts
file:
[all:vars] ansible_connection=ssh [dev] dev_host ansible_host=remote_host ansible_user=ubuntu ansible_private_key_file=/var/jenkins_home/ansible/remote-key
To run a test connection for your basic inventory for Ansible, type command:
ansible -i hosts -m ping dev_host
Then if ever you will get this message error: Failed to connect to the host via ssh
:
dev_host | UNREACHABLE! => { "changed": false, "msg": "Failed to connect to the host via ssh: Control socket connect(/var/jenkins_home/.ansible/cp/fec9c58b80): Connection refused\r\nFailed to connect to new control master", "unreachable": true }
Apply this solution:
Change the permission of your remote-key
file using:
chmod 600 remote-key
Go to /var/jenkins_home
folder and make a new file .ansible.cfg
And paste this entry:
[ssh_connection] ssh_args = -C -o ControlMaster=auto -o ControlPersist=60s control_path = /dev/shm/cp%%h-%%p-%%r
Then run test connection again:
ansible -i hosts -m ping dev_host
Output:
dev_host | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python3" }, "changed": false, "ping": "pong" }
That’s all.
Reference: Github Issue
Also: Learn how to SSH without using password on Linux Ubuntu
I hope this tutorial helped you and feel free to comment section below for more suggestions.
Tags: ansible, aws, docker, ec2, jenkins, ssh