Ansible 无法建立PyEZ连接:ConnectUnknownHostError
我正试图使用Ansible junos模块中的JUniter_junos_facts来查询我使用Vagrant配置的一些VM。然而,我得到以下错误Ansible 无法建立PyEZ连接:ConnectUnknownHostError,ansible,vagrant,junos-automation,pyez,Ansible,Vagrant,Junos Automation,Pyez,我正试图使用Ansible junos模块中的JUniter_junos_facts来查询我使用Vagrant配置的一些VM。然而,我得到以下错误 fatal: [r1]: FAILED! => {"changed": false, "msg": "Unable to make a PyEZ connection: ConnectUnknownHostError(r1)"} fatal: [r2]: FAILED! => {"changed": false, "msg": "Unab
fatal: [r1]: FAILED! => {"changed": false, "msg": "Unable to make a PyEZ connection: ConnectUnknownHostError(r1)"}
fatal: [r2]: FAILED! => {"changed": false, "msg": "Unable to make a PyEZ connection: ConnectUnknownHostError(r2)"}
我在juniper.net上的以下文档中看到,如果在清单文件中没有正确定义主机,则会发生此错误。我不认为这是我的清单文件的问题,因为当我运行ansible inventory时,主机似乎一切正常
~/vagrant-projects/junos$ ansible-inventory --host r1
{
"ansible_ssh_host": "127.0.0.1",
"ansible_ssh_port": 2222,
"ansible_ssh_private_key_file": ".vagrant/machines/r1/virtualbox/private_key",
"ansible_ssh_user": "root"
}
~/vagrant-projects/junos$ ansible-inventory --host r2
{
"ansible_ssh_host": "127.0.0.1",
"ansible_ssh_port": 2200,
"ansible_ssh_private_key_file": ".vagrant/machines/r2/virtualbox/private_key",
"ansible_ssh_user": "root"
}
我的剧本是从我从juniper.net上获得的以下文档中复制的
我的库存文件
[vsrx]
r1 ansible_ssh_host=127.0.0.1 ansible_ssh_port=2222 ansible_ssh_private_key_file=.vagrant/machines/r1/virtualbox/private_key
r2 ansible_ssh_host=127.0.0.1 ansible_ssh_port=2200 ansible_ssh_private_key_file=.vagrant/machines/r2/virtualbox/private_key
[vsrx:vars]
ansible_ssh_user=root
我的剧本
---
- name: show version
hosts: vsrx
roles:
- Juniper.junos
connection: local
gather_facts: no
tasks:
- name: retrieve facts
juniper_junos_facts:
host: "{{ inventory_hostname }}"
savedir: "{{ playbook_dir }}"
- name: print version
debug:
var: junos.version
使用
connection:local
时,需要向模块提供完整的连接详细信息(通常在播放级别打包在提供商字典中,以减少重复):
完整文档在这里(注意URL中的正确角色版本):在这里您还可以看到默认值
要全面解释“提供者”方法,您的剧本应该如下所示:
---
- name: show version
hosts: vsrx
roles:
- Juniper.junos
connection: local
gather_facts: no
vars:
connection_info:
host: "{{ ansible_ssh_host }}"
port: "{{ ansible_ssh_port }}"
user: "{{ ansible_ssh_user }}"
passwd: "{{ ansible_ssh_pass }}"
ssh_private_key_file: "{{ ansible_ssh_private_key_file }}"
tasks:
- name: retrieve facts
juniper_junos_facts:
provider: "{{ connection_info }}"
savedir: "{{ playbook_dir }}"
- name: print version
debug:
var: junos.version
此答案适用于通过错误消息找到此问题的人 如果您使用的连接插件不同于
local
,那么它可能会,并且通常是由变量排序相关的连接插件引起的
在2.2.1版及更高版本中已修复的错误,请尝试从Galaxy更新模块。ConnectUnknownHostError(r1)表示r1作为主机而不是IP发送给PyEZ。您需要将ansible\u ssh\u主机作为主机传递。我想如果你没有通过任何测试,我们会将它们作为默认设置。谢谢你的反馈,我可以问你如何将ansible_ssh_主机作为主机传递给PyEZ吗?这很有效,非常感谢你。我确实需要在连接信息中添加一行来指向我正在使用的ssh密钥,它是ssh密钥文件:“{{ansible\u ssh\u private\u key\u file}”
---
- name: show version
hosts: vsrx
roles:
- Juniper.junos
connection: local
gather_facts: no
vars:
connection_info:
host: "{{ ansible_ssh_host }}"
port: "{{ ansible_ssh_port }}"
user: "{{ ansible_ssh_user }}"
passwd: "{{ ansible_ssh_pass }}"
ssh_private_key_file: "{{ ansible_ssh_private_key_file }}"
tasks:
- name: retrieve facts
juniper_junos_facts:
provider: "{{ connection_info }}"
savedir: "{{ playbook_dir }}"
- name: print version
debug:
var: junos.version