Ansible无法通过ssh Jenkins连接到主机

ttisahbt  于 2022-10-06  发布在  Jenkins
关注(0)|答案(1)|浏览(247)

我正在Jenkins上使用Ansible插件,但我遇到了下一个错误:

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

我的主机文件:

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

我的剧本:

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

但如果我在外壳上运行:

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

这一切都很好:

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

以下是在Jenkins身上的路径:

gev0vcfq

gev0vcfq1#

私钥应该存储在Jenkins凭据中。
插件ssh-credentials应该安装在Jenkins中。
然后:

Manage Jenkins ==> Manage Credentials ==> add credentials ==> SSH username with private key

给它一个IDusername,将密钥粘贴到Private Key部分并保存。


在您问题的屏幕截图上显示的Build steps中,在Inventory选项下应该有选项Credentials(它可能不存在,因为没有安装ssh-Credentials插件)。

应在那里找到保存的凭据以供选择。


现在执行剧本应该没有任何问题。

相关问题