【问题标题】:Ansible won't connect to emulated network devices over TelnetAnsible 不会通过 Telnet 连接到模拟网络设备
【发布时间】:2018-10-19 23:35:02
【问题描述】:

我正在尝试使用 Ansible 启用对 EVE-NG 环境中模拟的网络设备的 SSH 访问。我的目标是使用 Ansible 剧本来配置 SSH。 EVE-NG 的工作方式类似于 GNS3,因为您可以通过特定端口上的 telnet 连接到设备。对于此示例,假设我的 EVE-NG 服务器位于 192.168.1.10。 EVE-NG 将为该设备分配一个 telnet 端口,例如 32899。如果我运行telnet 192.168.1.10 32899,我就可以按预期访问该设备。但是,如果我执行我的剧本,剧本似乎在 telnet 模块执行期间什么都不做。下面是我的剧本的 sn-p。

- name: Configure new EVE-NG router
  hosts: new_devices:cisco
  gather_facts: no
  tasks:
    - debug:
        msg: '{{ansible_host}} {{ansible_port}}'
    - name: Setup domain information
      telnet:
        port: '{{ansible_port}}'
        prompts:
          - "[>|#]"
        command:
          - term length 0
          - en
          - conf t

运行ansible-playbook-vvv 不会显示任何有用的信息。当 Ansible 正在执行时,netstat 确实会在 ESTABLISHED 状态下显示出站连接。

【问题讨论】:

  • 模块文档似乎暗示它正在等待登录提示,因此您可能希望使用login_prompt: ">" 将其伪装出来,并使用user: "term length 0" 将您的命令之一运行以响应到虚假的“登录提示”
  • 有趣的想法,但这不起作用。闲置的结果相同。
  • 好的,那么如果您熟悉socat,您可能会在查看流量以查找正在发送和未发送的内容时获得一些调试成功。我没有任何可以说 telnet 的方便的东西来提供更具体的调试技巧:-(

标签: ansible telnet


【解决方案1】:

我会使用 shell 模块结合 /usr/bin/expect 作为可执行文件来解决这个问题。这使您可以更灵活地与系统交互,并可以在单独的日志文件中跟踪进度。

- name: Interact with system
  shell: |
    set timeout 120
    log_file {{ '~/logs/your_log_file.explog' }}

    spawn telnet {{ inventory_hostname }}

    expect "username: "
    send "{{ your_username }}\n"

    expect "password:"
    send "{{ your_password }}\n"

    expect "# "
    send "term length 0"

    expect "# "
    send "en"

    expect "# "
    send "conf -t"
  args:
    executable: /usr/bin/expect
  changed_when: yes
  delegate_to: localhost

或者您可以使用 脚本 模块来保持剧本整洁。

- name: Interact with system
  script: interact-with-system.expect
  args:
    executable: /usr/bin/expect
  changed_when: yes
  delegate_to: localhost

PS:如果你使用shebang,你甚至不需要添加可执行文件。

查看 expect 手册页或在线示例,了解您可以使用 expect 做什么。它非常强大,但需要一些时间来适应。

【讨论】:

    猜你喜欢
    • 2017-05-23
    • 2011-08-15
    • 2014-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-03
    相关资源
    最近更新 更多