无法建立PyEZ连接:ConnectUnknownHostError

时间:2019-03-20 14:43:06

标签: ansible vagrant junos-automation pyez

我正在尝试使用Ansible Junos模块中的 juniper_junos_facts 来查询我使用Vagrant设置的某些VM。但是我遇到以下错误。

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上的以下文档Here中看到,当您在清单文件中未正确定义主机时,会发生此错误。我不认为这与我的清单文件有关,因为当我运行 ansible-inventory --host 时,一切似乎都井然有序

~/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上Here获得的以下文档中复制的。

我的库存文件

[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

2 个答案:

答案 0 :(得分:1)

在使用connection: local时,您需要为模块提供完整的连接详细信息(通常在播放级别打包在提供程序字典中,以减少重复):

    - name: retrieve facts
      juniper_junos_facts:
        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 }}" 
        savedir: "{{ playbook_dir }}"

完整的文档在这里(请注意URL中正确的角色版本):https://junos-ansible-modules.readthedocs.io/en/2.1.0/juniper_junos_facts.html,您还可以在其中查看默认值。

要完全解释“提供者”方法,您的剧本应如下所示:

---
- 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

答案 1 :(得分:0)

此答案适用于将通过错误消息找到此问题的人。

如果您使用与local不同的连接插件,它可能并且通常是由与变量排序相关的bug引起的

在版本2.2.1和更高版本中已修复的错误,请尝试从Galaxy更新模块。