Luv33preet
Luv33preet

Reputation: 1867

Jenkins: ansible host not reachable

I am trying Jenkins to execute an ansible playbook. But I am getting the unreachable host error which I don't get otherwise.

fatal: [vogo-alpha.cloudapp.net]: UNREACHABLE! => {"changed": false, "msg": "Authentication failure.", "unreachable": true}

I have given this variable in ansible hosts file,

ansible_ssh_private_key_file=/home/luvpreet/.ssh/id_rsa

I think it is because the user jenkins is playing those playbooks and it cannot read this private key file. I tried to make jenkins' user home folder but it was not successful.

It can be done if I switch to the user luvpreet and then run these playbooks.

How do I switch to another user via jenkins shell ? OR

Is there any other way this problem can be solved ?

Upvotes: 0

Views: 3404

Answers (1)

kfreezy
kfreezy

Reputation: 1579

There are a couple of possibilities why your solution is working. Most likely because Ansible is trying to ssh to your target machine as the jenkins user which isn't on said machine. I'd approach the problem from a different angle.

First, I'd install the Ansible plugin for Jenkins. This allows you to use the built in credentials located at "Manage Jenkins > Manage Credentials". There you can copy and paste your key in (or point to a key file located on the jenkins server) and set the username that will ssh to the target machine. In your job configuration choose "Invoke Ansible Playbook" for your build step rather than shell. There will be a "Credentials" parameter where you can specify the ssh key you added earlier. The rest should be pretty self explanatory.

Upvotes: 1

Related Questions