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: [17.14.69.21]: 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}
[17.14.69.21]: 
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.

  • 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 – Bruce Becker Oct 24 at 15:09
  • Does your Jenkins user have access to your ssh key? – PrestonM Oct 25 at 20:30

--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

  • 1
    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 Nov 6 at 12:48
  • How do you know the user does not want to allow root login? Yours is a comment/suggestion but not an answer. Instead of writing comments you better should do the helping. – hakkican Nov 6 at 13:17
  • 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 Nov 8 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 Nov 8 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 Nov 8 at 15:20

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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