Answers: <p>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. </p>
  
  <p>This is an example of Jenkinsfile step. </p>
  
  <pre><code>ansiblePlaybook colorized: true, installation: 'Ansible', inventory: 'inventory', playbook: 'playbook.yml',tags: "" , skippedTags: "", extras: "--extra-vars ' hst=targetHost "
  
  </code></pre>
  
  <p>This is Ansible Inventory example. You can define the args at the host level as well.</p>
  
  <pre><code>[targetHost]
  17.14.69.21 ansible_ssh_user=sshuser
  
  [all:vars]
  ansible_connection=ssh
  ansible_ssh_common_args='-o StrictHostKeyChecking=no -oHostKeyAlgorithms=+ssh-dss'
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2020-05-09T22:18:08.140Z
Answers: 2020-05-09T22:18:08.140Z
Answers: nandilov
Answers: <blockquote>
    <p>--become-user root</p>
  </blockquote>
  
  <p>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.</p>
  
  <p>Assuming those servers are Unix/Linux . Since there is no details how you test with CLI.I am also curious about it. </p>
  
  <p>This might help.
  <a href="https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html/v2v_guide/preparation_before_the_p2v_migration-enable_root_login_over_ssh" rel="nofollow noreferrer">Enable root login over SSH</a></p>
  
  <blockquote>
    <p>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</p>
  </blockquote>
  
  <p><a href="https://docs.ansible.com/ansible/2.6/user_guide/become.html#becoming-an-unprivileged-user" rel="nofollow noreferrer">Understanding Privilege Escalation</a></p>
  
  <p>I think you just want it working :) Ansible documentation is short and brief.</p>
  
  <blockquote>
    <p>Working with Inventory
    ansible_ssh_pass<br>
        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).</p>
  </blockquote>
  
  <p><a href="https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html" rel="nofollow noreferrer">Working with Inventory</a></p>
  
Answers: CC BY-SA 4.0
Answers: 2018-11-06T12:48:33.057Z
Answers: 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..
Answers: Tensibai
Answers: CC BY-SA 4.0
Answers: 2018-11-08T12:02:35.387Z
Answers: 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
Answers: Nithin
Answers: CC BY-SA 4.0
Answers: 2018-11-08T12:16:44.687Z
Answers: 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 ?
Answers: hakkican
Answers: CC BY-SA 4.0
Answers: 2018-11-08T15:20:55.737Z
Answers: 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.
Answers: Nithin
Answers: CC BY-SA 4.0
Answers: 2018-11-06T12:19:03.297Z
Answers: 2018-11-08T12:37:54.610Z
Answers: 2018-11-08T12:37:54.610Z
Answers: hakkican
Body: <p>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.</p>
  
  <p>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.</p>
  
  <p>The Error msg:</p>
  
  <pre><code>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! =&gt; {"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.
  </code></pre>
  
  <p>I have given sudo access to Jenkins user and Jenkins is running as Jenkins user.</p>
  
Comments: CC BY-SA 4.0
Comments: 2018-10-24T15:09:56.210Z
Comments: 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`
Comments: Bruce Becker
Comments: CC BY-SA 4.0
Comments: 2018-10-25T20:30:16.940Z
Comments: Does your Jenkins user have access to your ssh key?
Comments: Preston Martin
Tags: <jenkins><ansible><jenkins-pipeline>
Title: How to execute an ansible playbook using a jenkins pipeline script?
UserDisplayName: Nithin
ThreadText: # How to execute an ansible playbook using a jenkins pipeline script?
  
  (Asked by: username_0 on 2018-10-24)
  
  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.
  
  **Tags:** jenkins, ansible, jenkins-pipeline
  
  > **Comments:**
  > **username_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`  
  > **username_2**: Does your Jenkins user have access to your ssh key?  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_3 on 2020-05-09. Score: 1
  
  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]
  17.14.69.21 ansible_ssh_user=sshuser
  
  [all:vars]
  ansible_connection=ssh
  ansible_ssh_common_args='-o StrictHostKeyChecking=no -oHostKeyAlgorithms=+ssh-dss'
  
  ```
  
  
  ---
  
  ### Answer by username_4 on 2018-11-06. Score: -2
  
  > --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](https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html/v2v_guide/preparation_before_the_p2v_migration-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](https://docs.ansible.com/ansible/2.6/user_guide/become.html#becoming-an-unprivileged-user)
  
  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](https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html)
  
  > **Comments:**
  > **username_5**: 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..  
  > **username_0**: 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  
  > **username_4**: 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 ?  
  > **username_0**: 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.  
  
  ---
  
  