ntc-ansible 模块的 SSH 连接问题

SSH connectivity issues with ntc-ansible modules

我正在尝试在 Ubuntu (WSL) 上将 ntc-ansible 模块与 Ansible 运行ning 结合使用。我通过 ssh 连接到我的远程设备 (Cisco 2960X),我可以使用内置的 Ansible 网络模块 (ios_command) 运行 将 ansible playbooks 连接到同一个远程交换机,并且它工作正常。

问题:

当我尝试 运行 任何 ntc-ansible 模块时,它失败了,无法连接到设备。可能很简单,但我碰壁了。关于如何使用 ntc-ansible 模块,我缺少一些东西。 Ansible 正在查看模块,因为我可以按照自述文件中建议的测试查看文档。

我在这里安装了 ntc-ansible 模块:/home/melshman/.ansible/plugins/modules/ntc-ansible 我从这里 运行 整理我的剧本:~/projects/ansible/ 我第一次 运行 带有 ntc-ansible 模块的剧本失败了,基于错误消息和一些研究,我安装了 sshpass (sudo apt-get install sshpass)。但是在使用 ntc-ansible 时仍然有 ssh 问题......(下面的剧本和回溯)

我听说有人在找一个索引文件,但我找不到那个文件?它住在哪里,我需要用它做什么?

我的连接应该设置成什么?当地的? SSH? Netmiko_ssh?

我应该使用什么平台? Cisco_ios? cisco_ios_ssh?

感谢我能得到的任何帮助。我已经 运行 转了几个小时了。


Ansible 版本信息:

VTMNB17024:~/projects/ansible $ ansible --version
ansible 2.5.3
  config file = /home/melshman/projects/ansible/ansible.cfg
  configured module search path = [u'/home/melshman/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python2.7/dist-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 2.7.12 (default, Dec  4 2017, 14:50:18) [GCC 5.4.0 20160609]

工作手册 (ios_command:) 注意:ansible_ssh_pass 和 ansible_user 组 var:

- name:  Test Net Automation
  hosts:  ctil-ios-upgrade
  connection: local
  gather_facts: no

  tasks:
    - name:  Grab run config
      ios_command:
        commands:
          - show run
      register: config

    - name:  Create backup of running configuration
      copy:
        content:  "{{config.stdout[0]}}"
        dest: "backups/show_run_{{inventory_hostname}}.txt"

使用 ntc-ansible 模块的 Playbook(不工作)(注意:用户名和密码在组 VAR 中定义:

- name: Cisco IOS Automation
  hosts: ctil-ios-upgrade
  connection: local
  gather_facts: no

  tasks:
  - name: GET UPTIME
    ntc_show_command:
       connection: ssh
       platform: "cisco_ios"
       command: 'show version | inc uptime'
       host: "{{ inventory_hostname }}"
       username: "{{ username }}"
       password: "{{ password }}"
       use_templates: True
       template_dir: /home/melshman/.ansible/plugins/modules/ntc-ansible/ntc-templates/templates

这是我在发生错误时得到的回溯:

An exception occurred during task execution. To see the full traceback, use -vvv. The error was: netmiko.ssh_exception.NetMikoTimeoutException: Connection to device timed-out: cisco_ios VTgroup_SW:22 fatal: [VTgroup_SW]: FAILED! => {"changed": false, "module_stderr": "Traceback (most recent call last):\n File \"/tmp/ansible_RJRY9m/ansible_module_ntc_save_config.py\", line 279, in \n main()\n File \"/tmp/ansible_RJRY9m/ansible_module_ntc_save_config.py\", line 251, in main\n device = ntc_device(device_type, host, username, password, **kwargs)\n File \"/usr/local/lib/python2.7/dist-packages/pyntc-0.0.6-py2.7.egg/pyntc/__init__.py\", line 35, in ntc_device\n return device_class(*args, **kwargs)\n File \"/usr/local/lib/python2.7/dist-packages/pyntc-0.0.6-py2.7.egg/pyntc/devices/ios_device.py\", line 39, in __init__\n self.open()\n File \"/usr/local/lib/python2.7/dist-packages/pyntc-0.0.6-py2.7.egg/pyntc/devices/ios_device.py\", line 55, in open\n verbose=False)\n File \"build/bdist.linux-x86_64/egg/netmiko/ssh_dispatcher.py\", line 178, in ConnectHandler\n File \"build/bdist.linux-x86_64/egg/netmiko/base_connection.py\", line 207, in __init__\n File \"build/bdist.linux-x86_64/egg/netmiko/base_connection.py\", line 693, in establish_connection\nnetmiko.ssh_exception.NetMikoTimeoutException: Connection to device timed-out: cisco_ios VTgroup_SW:22\n", "module_stdout": "", "msg": "MODULE FAILURE", "rc": 1}

这是一个使用 ntc_show_command 到 Cisco IOS 设备的有效解决方案。

- name: Cisco IOS Automation
  hosts: pynet-rtr1
  connection: local
  gather_facts: no

  tasks:
  - name: GET UPTIME
    ntc_show_command:
       connection: ssh
       platform: "cisco_ios"
       command: 'show version'
       host: "{{ ansible_host }}"
       username: "{{ ansible_user }}"
       password: "{{ ansible_ssh_pass }}"
       use_templates: True
       template_dir: '/home/kbyers/ntc-templates/templates'

如果你打算使用 ntc-templates,我可能不会有 '|在 'show version' 中包含正常运行时间'。换句话说,让 TextFSM 先将输出转换为结构化数据,然后从结构化数据中获取正常运行时间。

我将 inventory_hostname 修改为 ansible_host 以与我的清单格式保持一致(我的 inventory_hostname 实际上并未在 DNS 中解析)。

我将用户名和密码修改为 'ansible_user' 和 'ansible_ssh_pass' 以与我的清单保持一致,也与 Ansible 2.5/2.6 变量命名更加一致。

关于您的上述问题,您的异常消息与您的 playbook 不匹配(即您确定这是您为该 playbook 获得的异常)。

这是我的库存文件(我简化了它以删除一些不必要的设备并隐藏机密信息)

[all:vars]
ansible_connection=local
ansible_python_interpreter=/home/kbyers/VENV/ansible/bin/python
ansible_user=user
ansible_ssh_pass=password

[local]
localhost ansible_connection=local

[cisco]
pynet-rtr1 ansible_host=cisco1.domain.com
pynet-rtr2 ansible_host=cisco2.domain.com