首页 > 解决方案 > 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 文件中遗漏了一些东西,表明连接到“管理员”用户,就像在第一个测试中一样。

对此问题的任何建议将不胜感激。

谢谢你。

标签: sshansible

解决方案


错误是

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


笔记


推荐阅读