2

I am trying to use juniper_junos_facts from the Ansible Junos module to query some VM's that I provisioned using Vagrant. However I am getting the following error.

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)"}

I see in the following document Here on juniper.net that this error occurs when you don't have the host defined correctly in the inventory file. I don't believe this to be an issue with my inventory file because when I run ansible-inventory --host all appears to be in order

~/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"
}

My playbook is copied from the following document which I got from Here on juniper.net.

My Inventory File

[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

My Playbook

---
- 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
Zeitounator
  • 38,476
  • 7
  • 53
  • 66
Barry Keegan
  • 81
  • 2
  • 9
  • ConnectUnknownHostError(r1) means r1 is sent as host and not the IP to PyEZ. You need to pass ansible_ssh_host as host. I think if you don't pass any, we take them as default. – Nitin Kr Mar 20 '19 at 16:19
  • Thanks for the feedback, can I ask how you pass ansible_ssh_host as host to PyEZ? – Barry Keegan Mar 22 '19 at 07:38

2 Answers2

3

As you're using connection: local you need to give the module full connection details (usually packaged in a provider dictionary at the play level to reduce repetition):

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

Full docs are here (watch out for the correct role version in the URL): https://junos-ansible-modules.readthedocs.io/en/2.1.0/juniper_junos_facts.html where you can also see what the defaults are.

To fully explain the "provider" method, your playbook should look something like this:

---
- 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
Zeitounator
  • 38,476
  • 7
  • 53
  • 66
cmsirbu
  • 46
  • 2
  • 2
    This worked, thank you so much cmsirbu. I did have to add 1 line to the conncetion_info to point to my ssh_key that I was using which was ssh_private_key_file: "{{ ansible_ssh_private_key_file }}" – Barry Keegan Mar 28 '19 at 13:20
0

This answer for people who will find this question by error message.

If you use connection plugin different from local, it can, and usually caused by this bug related to variables ordering

Bug already fixed in Release 2.2.1 and later, try to update module from Galaxy.

Villi
  • 1