ssh - Ansible Notebook ping 用户的问题
问题描述
在阅读了一些内容之后,我刚刚创建了我的第一个 Ansible Playbook 来 ping 主机。
首先,我确保为管理服务器创建 SSH 密钥并将其复制到目标服务器:
$ # ssh-keygen -t rsa -b4096
# ssh-copy-id administrator@nap-01.vm
# ssh-copy-id administrator@nap-02.vm
然后我将目标服务器添加到 /etc/ansible/hosts 文件中:
[testing]
nap-01.vm
nap-02.vm
首先,我使用 ping 模块进行了测试。一切顺利:
$ ansible -i /etc/ansible/hosts testing -m ping -u administrator
nap-02.vm | SUCCESS => {
"changed": false,
"ping": "pong"
}
nap-01.vm | SUCCESS => {
"changed": false,
"ping": "pong"
}
我在测试服务器中有几个用户,所以我使用管理员用户帐户。
接下来,我尝试使用剧本执行相同的操作(ping):
$ cat test.yml
---
- name: "Get ping response"
hosts: testing
tasks:
- action: ping
register: hello
- debug: msg="{hello.stdout}"
但是运行之后:
$ ansible-playbook test.yml
我得到以下输出:
PLAY [Get ping response] *******************************************************************************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************************************************************************************
fatal: [nap-01.vm]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: oscar@nap-01.vm: Permission denied (publickey,password).", "unreachable": true}
fatal: [nap-02.vm]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: oscar@nap-02.vm: Permission denied (publickey,password).", "unreachable": true}
PLAY RECAP *********************************************************************************************************************************************************************************
nap-01.vm : ok=0 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0
nap-02.vm : ok=0 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0
所以看起来远程不允许通过 SSH 连接并且权限被拒绝。但是,由于 ping 是通过模块工作的,我猜我可能在 YAML 文件中遗漏了一些东西,表明连接到“管理员”用户,就像在第一个测试中一样。
对此问题的任何建议将不胜感激。
谢谢你。
解决方案
错误是
ssh连接主机失败:oscar@nap-01.vm
有几个选项如何连接为administrator@nap-01.vm*
1)remote_user
在命令行中指定
$ ansible-playbook test.yml -u administrator
2)remote_user
在剧本中指定
- name: "Get ping response"
hosts: testing
remote_user: administrator
tasks:
...
3)ansible_user
在库存中指定
[testing]
nap-01.vm ansible_user=administrator
nap-02.vm ansible_user=administrator
4) 设置ANSIBLE_REMOTE_USER
笔记
推荐阅读
- intellij-idea - IntelliJ 或 WebStorm 键盘映射在 Debian 9 中不起作用
- liferay - 如何在 Liferay 7 控制面板中编辑默认 portlet?
- protractor - 对于使用 target='_blank_' 编写的重定向 url,量角器测试失败
- macos - MacOS X 上的 AudioKit - 如何删除安全弹出警报
- php - 如何在 laravel 中重定向到此 URL?
- swift - 如何屏蔽 Swift 中的最后一个数字?
- android - 如何使用 Android MVI 架构管理服务
- javascript - 在变量中设置弹出窗口的高度和宽度(可能是语法问题)
- php - GKE / 运行 php 应用程序 / 通过 nginx 或 apache 公开?
- bixby - 让 bixby 胶囊收集电话号码的最佳方法是什么?