How to execute an ansible playbook using a jenkins pipeline script? 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! =&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.


I have given sudo access to Jenkins user and Jenkins is running as Jenkins user.
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'