Ansible raw ssh 模块可以像"expect"一样使用吗?

Can the Ansible raw ssh module be used like "expect"?

换句话说,ansible "expect" 模块可以通过原始 SSH 连接使用吗?

我正在尝试自动化一些没有完整 Python 甚至 shell 的简单嵌入式设备。 ansible raw 模块适用于简单的命令,但我想以类似预期的方式驱动事物。

这可以做到吗?

expect 模块是 written in python,所以不,那行不通。

Ansible 确实有一个模型可以与网络交换机等设备进行交互,而这些设备同样没有 运行 python;您可以在 How Network Automation Is Different 中阅读相关内容。我认为这不会为您的问题提供任何直接的解决方案,但如果与 Ansible 集成真的很重要,它会建议一种追求事物的方法。

使用实际的 expect 程序而不是 Ansible 可能会更简单。

使用 Ansible 2.7 及更高版本,您可以使用 cli_command 模块。它有点像 'expect' 并且不需要在目标上使用 python。 https://docs.ansible.com/ansible/latest/modules/cli_command_module.html

- name: multiple prompt, multiple answer (mandatory check for all prompts)
  cli_command:
    command: "copy sftp sftp://user@host//user/test.img"
    check_all: True
    prompt:
      - "Confirm download operation"
      - "Password"
      - "Do you want to change that to the standby image"
    answer:
      - 'y'
      - <password>
      - 'y'