我正在尝试使用Ansible来启用SSH访问在EVE-NG环境中模拟的网络设备。我的目标是让一个可操作的剧本来配置SSH。EVE-NG的工作方式类似于GNS3,即通过特定端口通过telnet连接到设备。对于这个例子,假设我的EVE服务器位于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不会显示任何有用的信息。当Ansible执行时,netstat确实显示出处于ESTABLISHED状态的出站连接。
发布于 2018-10-19 15:22:18
我将使用与/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: localhostPS:如果您使用的是shebang,您甚至不需要添加可执行文件。
查看expect手册页或在线示例,看看您可以使用expect做些什么。这是相当强大的,但需要一些习惯。
https://stackoverflow.com/questions/52837619
复制相似问题