在Ansible中,可以使用expect
模块来实现对非预设的远程shell输出进行交互响应。下面是一个示例代码:
- name: Run remote command and respond interactively
hosts: remote_host
gather_facts: false
tasks:
- name: Run remote command
expect:
command: "remote_command"
responses:
'Expected output 1': 'response 1'
'Expected output 2': 'response 2'
'Expected output 3': 'response 3'
register: result
- name: Show command output
debug:
var: result.stdout_lines
在上面的示例中,remote_host
是远程主机的名称或IP地址。remote_command
是要执行的远程命令。
responses
参数是一个字典,它定义了期望的远程shell输出和相应的响应。当远程命令的输出中包含任何一个期望的输出时,Ansible将自动发送相应的响应。
在注册的result
变量中,可以通过result.stdout_lines
获取远程命令的输出。
请根据实际情况调整Expected output
和response
的值以及其他参数,以满足你的需求。