Home > database >  Ansible failed to connect to the host via ssh Jenkins
Ansible failed to connect to the host via ssh Jenkins

Time:09-30

I am working with Ansible plugin on Jenkins and I had the next error:

> "Failed to connect to the host via ssh: Load key \"/etc/ansible/jenkinsPem\": Permission denied\r\nmadchabelo@targethost: Permission denied (publickey,password).", "unreachable": true}

my hosts file:

[targets]
targethost ansible_user=madchabelo ansible_private_key_file=/etc/ansible/jenkinsPem
mysqlhost ansible_user=madchabelo ansible_private_key_file=/etc/ansible/jenkinsPem

My playbook:

---
- name: Playbook for jenkins
  hosts: [targets]
  tasks:
   - name: hello world
     shell: echo Hello World again

But if I run on shell:

ansible-playbook -i /etc/ansible/hosts /etc/ansible/playbook/jenkinsansible.yaml

All its good:

PLAY [Playbook for jenkins] *******************************************

TASK [Gathering Facts] ************************************************
[WARNING]: Platform linux on host targethost is using the discovered Python interpreter at /usr/bin/python3, but future installation of another Python interpreter
could change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.

ok: [targethost]

[DEPRECATION WARNING]: Distribution fedora 36 on host mysqlhost should use /usr/bin/python3, but is using /usr/bin/python for backward compatibility with prior Ansible
 releases. A future Ansible release will default to using the discovered platform python for this host. See 
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information. This feature will be removed in version 2.12. Deprecation 
warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.

ok: [mysqlhost]

TASK [hello world] ***************************************************
changed: [targethost]
changed: [mysqlhost]

PLAY RECAP ***********************************************************
mysqlhost: ok=2  changed=1  unreachable=0  failed=0  skipped=0  rescued=0  ignored=0   
targethost: ok=2 changed=1  unreachable=0  failed=0  skipped=0    rescued=0    ignored=0 

Here are the paths on Jenkins:

enter image description here

CodePudding user response:

The private key should be stored in the Jenkins credentials.
The plugin enter image description here In the Build steps shown on the screenshot in your question, there should be the option Credentials under the inventory option (It may not exsist because the ssh-credentials plugin isn't installed).

The saved credentials should be found there to be chosen.
Jenkins screenshot The playbook should now be executed without any problem.

  • Related