I know there's a related question here: Invoke Ansible playbook in Jenkins
But this question is different from that in some perspectives:
So I have Jenkins and Ansible installed in a single server (EC2 instance). I have created a playbook.yml which copies files from this server to another server. This is successful if I do ansible-playbook playbook.yml using ec2-user.
The same if I try to do using Jenkins (Created a project and added a build step as run ansible playbook and mentioned the location of playbook.yml and running as sudo ec2-user). This is failing with the error:
fatal: [172.xx.xx.xx]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: no such identity: /home/ec2-user/.ssh/Host_Keypair.pem: Permission denied\r\nPermission denied (publickey).\r\n", "unreachable": true}
to retry, use: --limit @/var/lib/jenkins/workspace/Ansible/copy.retr
playbook.yml:
---
- hosts: 172.xx.xx.xx
remote_user: ec2-user
become: yes
become_method: sudo
tasks:
- copy:
src: /var/lib/jenkins/workspace/Ansible/myfile.conf
dest: /etc/myfile.conf
owner: ec2-user
group: ec2-user
mode: 0644
I am stuck on this and not able to move to next step without executing this. Can anyone please help me on what other configuration I am missing (possibly in Jenkins side). Any help would be much appreciated.
Playbook is executed under
jenkins
user which doesn't have access to/home/ec2-user/.ssh/Host_Keypair.pem
key on ansible/jenkins host to authenticate on remote server.There is an error message: