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 10,260 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.

0 votes
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
0 votes
1 answer

not able to launch new node via SSH in Jenkins!

Hi@akhtar, To launch one new node via SSH, ...READ MORE

answered May 29, 2020 in Jenkins by MD
• 95,460 points
3,848 views
0 votes
1 answer

How to run Jenkins job from remote system?

Hi@akhtar, You can run your job from remote ...READ MORE

answered May 13, 2020 in Jenkins by MD
• 95,460 points
2,853 views
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
• 7,340 points
2,067 views
+2 votes
6 answers

Jenkins unable to run jobs (pending - waiting for next executor)

Increase the number of executors. I had ...READ MORE

answered Apr 24, 2019 in Jenkins by Maya
41,470 views
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
• 59,230 points
10,677 views
+1 vote
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
• 52,350 points
1,434 views
+15 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 27, 2018 in DevOps & Agile by DragonLord999
• 8,450 points
4,062 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP