We developed declarative pipeline code to execute an Ansible playbook. We tested the playbook from CLI and it is working properly. My playbook actually performs some shell commands on remote server.

SSH configuration between two servers has been done: I have generated an ssh key and copied it to the remote server, and I am able to ping the server.

The Error msg:

ansible-playbook /var/lib/jenkins/workspace/test1/playbook2.yml -i 
/var/lib/jenkins/workspace/test1/inventory.ini -b --become-user root
[WARNING] Ansible is in a world writable directory 
(/var/lib/jenkins/workspace/test1), ignoring it as an ansible.cfg source

fatal: []: UNREACHABLE! => {"changed": false, "msg": "Failed to 
connect to the host via ssh: Permission denied (publickey,gssapi- 
keyex,gssapi-with-mic,password).\r\n", "unreachable": true}
FATAL: command execution failed
hudson.AbortException: Ansible playbook execution failed.

I have given sudo access to Jenkins user and Jenkins is running as Jenkins user.

  • 1
    The error is coming from ssh. So either the key you created is not the one being used by Jenkins, or the user that Jenkins is connecting as to run the playbook is incorrect. Can you debug the issue running the playbook verbosely? Ie ansible-playbook -vvv Commented Oct 24, 2018 at 15:09
  • 1
    Does your Jenkins user have access to your ssh key?
    – Preston Martin
    Commented Oct 25, 2018 at 20:30

2 Answers 2


If you have Jenkins user public key, on your target hosts authorized keys file, for the user you want to login as, ssh connection authorization should work.

This is an example of Jenkinsfile step.

ansiblePlaybook colorized: true, installation: 'Ansible', inventory: 'inventory', playbook: 'playbook.yml',tags: "" , skippedTags: "", extras: "--extra-vars ' hst=targetHost "

This is Ansible Inventory example. You can define the args at the host level as well.

[targetHost] ansible_ssh_user=sshuser

ansible_ssh_common_args='-o StrictHostKeyChecking=no -oHostKeyAlgorithms=+ssh-dss'

--become-user root

I think you are doing ssh connection between two servers as a root user. I would like to know why. I would never do this.

Assuming those servers are Unix/Linux . Since there is no details how you test with CLI.I am also curious about it.

This might help. Enable root login over SSH

Command line options --ask-become-pass, -K ask for privilege escalation password; does not imply become will be used. Note that this password will be used for all hosts. --become, -b run operations with become (no password implied) --become-method=BECOME_METHOD privilege escalation method to use (default=sudo), valid choices: [ sudo | su | pbrun | pfexec | doas | dzdo | ksu | runas | machinectl ] --become-user=BECOME_USER run operations as this user (default=root), does not imply –become/-b

Understanding Privilege Escalation

I think you just want it working :) Ansible documentation is short and brief.

Working with Inventory ansible_ssh_pass
The ssh password to use (never store this variable in plain text; always use a vault. See Variables and Vaults) ansible_ssh_private_key_file Private key file used by ssh. Useful if using multiple keys and you don’t want to use SSH agent. ansible_ssh_common_args This setting is always appended to the default command line for sftp, scp, and ssh. Useful to configure a ProxyCommand for a certain host (or group).

Working with Inventory

  • 2
    Please avoid using the answer section as comment, this is not an answer to the question and --become-user root is to tell ansible to sudo to user root before doing anything else, the solution is not to allow root login as it won't fix the login problem anyway..
    – Tensibai
    Commented Nov 6, 2018 at 12:48
  • Hi, Finally i did it... Added ssh key into known_host jenkins workspace. And in inventory host given the servers details in below format. [jboss] 192.xx.xx.xx. ansible_ssh_pass=xxxxx123 ansible_ssh_user=user1
    – Nithin
    Commented Nov 8, 2018 at 12:02
  • In inventory file I would not put user/password. You can direct keys path in that file. "--become-user root" why do you need this ?
    – hakkican
    Commented Nov 8, 2018 at 12:16
  • I dont know the logc behind it. I also tried without mentioning user/password. It doesnt worked for me. After executing this i tried multiple possibilities still it only worked when i mention user/password.
    – Nithin
    Commented Nov 8, 2018 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.