napalm-automation/napalm-ansible

Example using inventory_hostname

FloLaco opened this issue · 2 comments

Hello guys,

When we follow examples (http://napalm.readthedocs.io/en/latest/tutorials/ansible-napalm.html or https://github.com/napalm-automation/napalm-ansible#examples), the hostname is using the {{ inventory_hostname }}

But a classic host file is :

[cisco]
spine1 ansible_host=10.0.100.101
spine2 ansible_host=10.0.100.102
leaf1 ansible_host=10.0.100.103
leaf2 ansible_host=10.0.100.104
leaf3 ansible_host=10.0.100.105
leaf4 ansible_host=10.0.100.106

[cisco:vars]
ansible_python_interpreter= "/usr/bin/env python"

Here's the playbook example :

- name: Test Inventory #The Task Name
  hosts: cisco         #This will be in your ansible inventory file
  connection: local    #Required
  gather_facts: no     #Do not gather facts

  tasks:                                     #Begin Tasks
    - name: get facts from device            #Task Name
      napalm_get_facts:                      #Call the napalm module, in this case napal_get_facts
        hostname: "{{ inventory_hostname }}" #This is a parameter and is derived from your ansible inventory file
        username: 'admin'                     #The username to ssh with
        dev_os: 'nxos_ssh'                        #The hardware operating system
        password: 'xxxx'                 #The line level password
        filter: 'facts'                      #The list of items you want to retrieve. The filter keyword is _inclusive_ of what you want
      register: result                       #Ansible function for collecting output

    - name: print results                    #Task Name
      debug: msg="{{ result }}"              #Display the collected output

The playbook does not work

(flacommare_venv2.7) florian@ubuntuvm:~/ansible$ ansible-playbook playbooks/get_facts.yaml -i inventory/host.ini

PLAY [Test Inventory] ************************************************************************************************************************************************************************************************************************

TASK [get facts from device] *****************************************************************************************************************************************************************************************************************
fatal: [leaf2]: FAILED! => {"changed": false, "msg": "cannot connect to device: Cannot connect to leaf2"}
fatal: [leaf1]: FAILED! => {"changed": false, "msg": "cannot connect to device: Cannot connect to leaf1"}
fatal: [leaf3]: FAILED! => {"changed": false, "msg": "cannot connect to device: Cannot connect to leaf3"}
fatal: [spine2]: FAILED! => {"changed": false, "msg": "cannot connect to device: Cannot connect to spine2"}
fatal: [spine1]: FAILED! => {"changed": false, "msg": "cannot connect to device: Cannot connect to spine1"}
fatal: [leaf4]: FAILED! => {"changed": false, "msg": "cannot connect to device: Cannot connect to leaf4"}
	to retry, use: --limit @/home/florian/ansible/playbooks/get_facts.retry

PLAY RECAP ***********************************************************************************************************************************************************************************************************************************
leaf1                      : ok=0    changed=0    unreachable=0    failed=1
leaf2                      : ok=0    changed=0    unreachable=0    failed=1
leaf3                      : ok=0    changed=0    unreachable=0    failed=1
leaf4                      : ok=0    changed=0    unreachable=0    failed=1
spine1                     : ok=0    changed=0    unreachable=0    failed=1
spine2                     : ok=0    changed=0    unreachable=0    failed=1

Indeed, the module code set the local params to override provider. (

# allow local params to override provider
)
So my IP is not use but the "name" of the equipment, which is not a dns entry.

Is there some mistake in my inventory file ? Or maybe the example have a mistake and should use {{ ansible_host }} instead ?

Yes, you need to use {{ ansible_host }} and not {{ inventory_hostname }}

[cisco]
spine1 ansible_host=10.0.100.101
spine2 ansible_host=10.0.100.102
leaf1 ansible_host=10.0.100.103
leaf2 ansible_host=10.0.100.104
leaf3 ansible_host=10.0.100.105
leaf4 ansible_host=10.0.100.106

inventory_hostname is spine1, spine2, et cetera and would need to be DNS resolvable to work in the playbook.

You should follow the last example which doen't use any hostname parameter.
If you don't provide the hostname parameter, ansible_host will be used instead of the inventory hostname if provided in the vars.