【发布时间】:2021-10-20 10:40:42
【问题描述】:
我有一个 gitlab 存储库,其中包含我的 ansible 和 terraform 代码来部署我的基础架构。 (CI/CD) 我将 ssh 密钥存储在我的 gitlab 中。 当我的詹金斯代理测试 ssh 连接时,它工作正常。但是,当它启动“ansible-playbook”命令时,由于以下错误无法建立连接:
> [0m[1google_compute_instance.instance
> (remote-exec):[0m [0mConnecting to remote host via SSH...
> [0m[1google_compute_instance.instance
> (remote-exec):[0m [0m Host: ip address
> [0m[1.google_compute_instance.instance
> (remote-exec):[0m [0m User: devops
> [0m[1google_compute_instance.instance
> (remote-exec):[0m [0m Password: false
> [0m[1google_compute_instance.instance
> (remote-exec):[0m [0m Private key: true
> [0m[1google_compute_instance.instance
> (remote-exec):[0m [0m Certificate: false
> [0m[1google_compute_instance.instance
> (remote-exec):[0m [0m SSH Agent: false
> [0m[1google_compute_instance.instance
> (remote-exec):[0m [0m Checking Host Key: false
> [0m[1google_compute_instance.instance
> (remote-exec):[0m [0m Target Platform: unix
> [0m[1google_compute_instance.instance
> (remote-exec):[0m [0mConnected!
[0m[1google_compute_instance.instance (local-exec):[0m [0mfatal: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Warning: Permanently added (ED25519) to the list of known hosts.\r\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\r\n@ WARNING: UNPROTECTED PRIVATE KEY FILE! @\r\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\r\nPermissions 0644 for Ansible/lab_ssh_key' are too open.\r\nIt is required that your private key files are NOT accessible by others.\r\nThis private key will be ignored.\r\nLoad key \"/var/jenkins_home/workspace/Ansible/lab_ssh_key\": bad permissions\r\n Permission denied (publickey).", "unreachable": true}
[0m[1google_compute_instance.instance (local-exec):[0m [0mPLAY RECAP *********************************************************************
[0m[1google_compute_instance.instance (local-exec):[0m [0m : ok=0 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0
我没有找到在 gitlab 上保护我的私钥的方法。我尝试使用 chmod 更改其权限,但之后我无法将其上传到 gitlab。 对此有任何解决方案或其他想法吗? 提前致谢。
【问题讨论】:
-
如何将 SSH 密钥注入 CI 构建?
-
为了扩展上述内容,SSH 密钥可能应该作为凭证存储在某个地方(在 Jenkins、Vault 等内部),然后使用 Jenkins 插件绑定进行检索。然后,您可以在 Jenkins Pipeline 代码中轻松地将其注入 Ansible 和 Terraform。此外,如果您在 Terraform 配置中而不是在管道中执行 Ansible,那么您可能会发现在
null_resource而不是provisionerremote-exec中执行此操作更容易。 -
我的密钥作为文件存储在我的仓库中。谢谢你的回答,我试试看。
标签: ssh ansible gitlab terraform