首页 > 解决方案 > Ansible ping 有效,但 ansible-playbook 收到“无法通过 ssh 连接到主机”

问题描述

当我 ping 所有可用的库存时,我遇到了这个问题:

ansible all -m ping --user ubuntu  --private-key /path/to_key

它成功地ping通了他们

10.0.1.50 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
10.0.1.40 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
10.0.1.100 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}
10.0.1.200 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}

但是如果我尝试运行剧本

ansible-playbook system_info.yml  --user ubuntu  --private-key /path/to_key

我收到“无法通过 ssh 连接到主机:端口 22 关闭连接”,“无法访问”:true}

fatal: [10.0.1.200]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Connection closed by 10.0.1.200 port 22", "unreachable": true}
fatal: [10.0.1.40]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Connection closed by 10.0.1.40 port 22", "unreachable": true}
fatal: [10.0.1.50]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Connection closed by 10.0.1.50 port 22", "unreachable": true}
fatal: [10.0.1.100]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Connection closed by 10.0.1.100 port 22", "unreachable": true}

我错过了什么?

标签: ansible

解决方案


我们验证了它ansible all -a hostname -m shell -u ubuntu --key-file=...有效。我们了解到,选项必须在剧本之前指定:

ansible-playbook --user ubuntu --private-key /path/to_key system_info.yml

推荐阅读