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, 2019 in Jenkins by Nishant
• 160 points

recategorized Apr 2, 2019 by Nishant 2,530 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, 2019 by pavan

reshown Dec 2, 2019 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,910 points
410 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, 2019 in Jenkins by Sirajul
• 50,550 points
2,140 views
+1 vote
4 answers
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
185 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, 2019 in Jenkins by anonymous
12,550 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
• 51,830 points
104 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,400 points
425 views