Not able to connect remote host via jenkins to run ansible playbook

0 votes

Not able to connect remote host via jenkins to run ansible playbook.I am able to run the playbook directly via the terminal but getting error when i do with jenkins getting below error.

fatal: [webservers]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ubuntu@<<ipaddress-masked>>: Permission denied (publickey).", "unreachable": true}
	to retry, use: --limit @/etc/ansible/ansiblecopy.retry

I have masked the ipaddress
Jenkins and ansible server are on same machine
Apr 2 in Jenkins by Nishant
• 160 points

recategorized Apr 2 by Nishant 1,704 views

Hey @Nishant, Can you post the output of the following command

ansible all -m ping -vvv

From the terminal when i excute ansible all -m ping -vvv - below is the output

webservers | SUCCESS => {
    "changed": false,
    "invocation": {
        "module_args": {
            "data": "pong"
        }
    },
    "ping": "pong"
}

When i execute from Jenkins under Build and use execute shell then also i get the success

webservers | SUCCESS => {
    "changed": false,
    "invocation": {
        "module_args": {
            "data": "pong"
        }
    },
    "ping": "pong"
}
META: ran handlers
META: ran handlers
Finished: SUCCESS

However when i execute from Anbile playbook(added ansible plugin) using plugin

TASK [Gathering Facts] *********************************************************
fatal: [webservers]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ubuntu@17x.xx.xx.xx: Permission denied (publickey).", "unreachable": true}
to retry, use: --limit @/etc/ansible/first.retry
PLAY RECAP *********************************************************************
webservers                 : ok=0    changed=0    unreachable=1    failed=0  

This will be related to some permissions issues within jenkins and i have ansible server and jenkins server on the same machine

Use SSH keys and add the Jenkins user's Private key to the Jenkins credentials store and select those in the Ansible Configuration.

Make sure you've done this!

Thanks a lot this helps can you please let me know why Jenkins credentials need to be stored and used in ansible Config i used ubuntu credentials and was trying to use those in ansible config, if you can throw some light or any link will help

That's how you let Ansible know that its a Jenkins build. This lets you ssh into the remote hosts as Jenkins and ansible have been configured with the required keys and credentials.
can you please send the detailed method to do that? and also in my case, jenkins and ansible are on different server
You are looking for detailed steps to add your ssh keys into jenkins server?

1 answer to this question.

+1 vote
set "host_key_checking = False" in /etc/ansible/ansible.cfg
answered Dec 2 by pavan

reshown Dec 2 by Vardhan

Related Questions In Jenkins

0 votes
1 answer

Error while talking to remote Oracle DB using Jenkins-CI

I installed Oracle XE to talk to ...READ MORE

answered May 1, 2018 in Jenkins by shubham
• 6,890 points
336 views
+2 votes
6 answers
0 votes
2 answers

Error Jenkins: Failed to connect to repository

Jenkins runs as another user, not as ...READ MORE

answered Aug 7 in Jenkins by Sirajul
• 41,000 points
1,103 views
0 votes
1 answer

How do I pass value from my Fake # script to the host build server (Jenkins)

You can use the EnvInject plugin to pass environment ...READ MORE

answered Jul 11, 2018 in Jenkins by Kalgi
• 2,620 points
120 views
0 votes
5 answers

How to resolve this error “Jenkins Host key verification failed”?

HI Mam, Please check the following solution:: 1. Switch ...READ MORE

answered May 3 in Jenkins by anonymous
7,937 views
0 votes
1 answer
0 votes
1 answer

permissions related to AWS ECR

if you add allowContainerRegistry: true, kops will add those permissions ...READ MORE

answered Oct 9, 2018 in Kubernetes by Kalgi
• 46,110 points
83 views
+13 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 26, 2018 in DevOps & Agile by DragonLord999
• 8,380 points
261 views