skip to Main Content

I try to set up a very basic ansible connection with Cisco alwayson sandbox, but experience problem to connect to the devices using Ansible.

I am using WSL with ubuntu 20.04 under Windows, within ubuntu, I created a folder call "ansible test" both the hosts file and playbook are saved here.

When I run ansible-playbook -i hosts firstplay.yml, I get below error message

TASK [Checking connectivity] *******************************************************************************************
fatal: [131.226.217.143]: FAILED! => {"changed": false, "msg": "ssh connection failed: ssh connect failed: Failed to resolve hostname inventory_hostname (Name or service not known)"}
...ignoring
fatal: [sandbox-iosxr-1.cisco.com]: FAILED! => {"changed": false, "msg": "ssh connection failed: ssh connect failed: Failed to resolve hostname inventory_hostname (Name or service not known)"}
...ignoring

I searched the forum, someone mentioned same error may related to ip/dns mapping or typo or network connection, but none of them apply to this.
As you can see I tried to use both ip and FQDN, double checked no typo, and I can ssh to the device using FQDN, so I do not think it is DNS issue.

Any suggestion about what I have done wrong?

hosts file

[iosxe]
131.226.217.143

[iosxr]
sandbox-iosxr-1.cisco.com

[iosxe:vars]
ansible_network_os=ios
ansible_user='admin'
ansible_ssh_pass='C1sco12345'

[iosxr:vars]
ansible_network_os='iosxr'
ansible_user='admin'
ansible_ssh_pass='C1sco12345'

playbook

---

- hosts: all
  connection: network_cli
  gather_facts: no

  tasks:
      - name: Checking connectivity
        ignore_errors: True
        ios_command:
          commands:
            - show ip route
        register: ios_output

      - name: Verification of output
        debug:
          msg: "{{ 'Failed with message: ' ~ ios_output.msg if ios_output.failed else 'Success' }}"

      - local_action:
          module: copy
          content: "{{ inventory_hostname, 'Failed with message: ' ~ ios_output.msg if ios_output.failed else 'Success'  ~ ios_output.stdout_lines }}"
          dest: "./{{ inventory_hostname }}.txt"

2

Answers


  1. Chosen as BEST ANSWER

    Based on the error message and Mikael's advice, I think there is something wrong with the ssh connection via Ansible. I reinstalled Ubuntu under WSL and reintalled Ansible, when try to run the playbook, got below error,

    [WARNING]: ansible-pylibssh not installed, falling back to paramiko
    

    I had the error before and pip installed ansible-pylibssh

    This time I googled this error message and found other people had issue with ansible-pylibssh and advised to install paramiko instead. I did the same and there is no issue to connect to those devices via ansible. So conclusion is do not install ansible-pylibssh


  2. The inventory would normally be given a local inventory name, and optionally, an IP or hostname with ansible_host variable, e.g.

    [iosxe]
    iosxe1  ansible_host=131.226.217.143
    

    but the actual ssh errors here:

    ssh connection failed: ssh connect failed: Failed to resolve hostname inventory_hostname
    

    is what one would get if you literally tried to use the string "inventory_hostname" as the hostname, e.g:

    $ ssh inventory_hostname
    ssh: Could not resolve hostname inventory_hostname: Name or service not known
    

    I’m not able to reproduce this error with the given inputs (on linux), so I would recommend making doubly sure that you are running what you think you are running, and there is no funny ansible.cfg. Running with --verbose might be a good start to check.

    Alternatively, there might be some bug with your ansible, WSL, ssh combo where the template argument "inventory_hostname" doesn’t get expanded into the actual "131.226.217.143" or "sandbox-iosxr-1.cisco.com" respectively.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search