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 3,171 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
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 in Jenkins by MD
• 31,750 points
33 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 in Jenkins by MD
• 31,750 points
48 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
• 6,910 points
485 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
12,141 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
• 54,020 points
3,163 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,890 points
127 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,420 points
536 views