Ansible 不会通过 Telnet 连接到模拟网络设备
Ansible won't connect to emulated network devices over Telnet
我正在尝试使用 Ansible 启用 SSH 访问 EVE-NG 环境中模拟的网络设备。我的目标是使用 Ansible 剧本来配置 SSH。 EVE-NG 的工作方式类似于 GNS3,因为您可以通过特定端口上的 telnet 连接到设备。对于此示例,假设我的 EVE-NG 服务器位于 192.168.1.10。 EVE-NG 将为该设备分配一个 telnet 端口,例如 32899。如果我 运行 telnet 192.168.1.10 32899
我能够按预期访问该设备。但是,如果我执行我的剧本,剧本似乎在 telnet 模块执行期间什么都不做。下面是我的剧本片段。
- 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
没有显示任何有用的信息。 netstat
在 Ansible 执行时确实显示 ESTABLISHED
状态下的出站连接。
我会使用 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
或者您可以使用 script 模块来保持您的剧本整洁。
- name: Interact with system
script: interact-with-system.expect
args:
executable: /usr/bin/expect
changed_when: yes
delegate_to: localhost
PS:如果你使用shebang,你甚至不需要添加可执行文件。
查看 expect 手册页或在线示例,了解您可以使用 expect 做什么。它非常强大,但需要一些时间来适应。
我正在尝试使用 Ansible 启用 SSH 访问 EVE-NG 环境中模拟的网络设备。我的目标是使用 Ansible 剧本来配置 SSH。 EVE-NG 的工作方式类似于 GNS3,因为您可以通过特定端口上的 telnet 连接到设备。对于此示例,假设我的 EVE-NG 服务器位于 192.168.1.10。 EVE-NG 将为该设备分配一个 telnet 端口,例如 32899。如果我 运行 telnet 192.168.1.10 32899
我能够按预期访问该设备。但是,如果我执行我的剧本,剧本似乎在 telnet 模块执行期间什么都不做。下面是我的剧本片段。
- 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
没有显示任何有用的信息。 netstat
在 Ansible 执行时确实显示 ESTABLISHED
状态下的出站连接。
我会使用 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
或者您可以使用 script 模块来保持您的剧本整洁。
- name: Interact with system
script: interact-with-system.expect
args:
executable: /usr/bin/expect
changed_when: yes
delegate_to: localhost
PS:如果你使用shebang,你甚至不需要添加可执行文件。
查看 expect 手册页或在线示例,了解您可以使用 expect 做什么。它非常强大,但需要一些时间来适应。