prompt: "--- more --- " answer: " "
时间: 2024-05-28 11:14:06 浏览: 20
这段代码的作用是在命令行输出中检查是否出现 `--- more ---`,如果出现则自动发送空格以继续显示输出。`prompt` 参数指定要检查的字符串,`answer` 参数指定要发送的响应。
在这个例子中,`--- more ---` 后面有一个空格,所以在 `prompt` 参数的值中也要包含这个空格。如果 `prompt` 参数的值不包含空格,则在发送空格时可能会出现问题。
相关问题
tasks: - name: get config cli_command: command: get config register: result1 ignore_errors: True 回显为--- more --- 则一直执行空格 ,直到回显为> 则返回变量到result1 中,要用cli_commond模块,注意prompt跟answer模块在cli_commond中无法使用
如果您需要在 Ansible 中使用 `cli_command` 模块来执行命令并获取输出结果,在遇到 `--- more ---` 提示时自动执行空格直到回显为 `>` 字符,可以尝试以下代码:
```
- name: execute get config command on remote host
hosts: myswitch
tasks:
- name: get config
cli_command:
command_string: get config
register: result1
ignore_errors: True
- name: process output
set_fact:
config_lines: "{{ result1.stdout_lines | select('match', '^(?!--- more ---)[^>]*$') | list }}"
- name: check for more output
when: result1.stdout.find('--- more ---') != -1
cli_command:
command_string: " "
register: more_output
until: more_output.stdout.find(">") != -1
retries: 10
delay: 2
- name: process more output
set_fact:
more_lines: "{{ more_output.stdout_lines | select('match', '^(?!--- more ---)[^>]*$') | list }}"
- name: combine output
set_fact:
all_lines: "{{ config_lines + more_lines | default([]) }}"
- debug:
var: all_lines
```
在这个例子中,我们使用 `cli_command` 模块来执行 `get config` 命令,并将输出结果保存在 `result1` 变量中。由于命令的输出可能会包含 `--- more ---` 字符串,我们在 `cli_command` 模块中使用 `ignore_errors: True` 来忽略错误。接下来,我们使用 `set_fact` 模块和一系列 Ansible 过滤器来处理输出结果。具体来说,我们使用 `select` 过滤器将行过滤为不包含 `--- more ---` 或 `>` 字符的行,并使用 `list` 过滤器将其转换为列表。最后,我们使用 `debug` 模块来打印处理后的输出结果。
如果输出结果包含 `--- more ---` 提示符,则我们使用另一个 `cli_command` 模块来发送空格字符,以便获取更多的输出结果。我们使用 `until` 选项来等待回显中包含 `>` 字符,表示已经获取了所有的输出结果。由于设备可能需要一段时间来生成更多的输出,我们使用 `retries` 和 `delay` 选项来重试获取更多输出的操作。最后,我们使用 `set_fact` 模块和一系列 Ansible 过滤器来处理更多的输出结果,并将其与之前的输出结果合并。最后,我们使用 `debug` 模块来打印所有的输出结果。
请注意,`cli_command` 模块需要在目标设备上安装支持 CLI 的软件,并且需要指定正确的登录凭据,比如用户名和密码。另外,某些网络设备可能需要使用特定的协议来连接,比如 SSH 或 Telnet。您需要根据实际情况来选择正确的协议。
tasks: - name: get config cli_command: command: get config register: result1 ignore_errors: True 回显为--- more --- 则一直执行空格 ,直到回显为> 则返回变量到result1 中,要用cli_commond模块
如果您希望在 Ansible 中使用 `cli_command` 模块来执行命令并获取输出结果,同时在遇到 `--- more ---` 提示时自动执行空格直到回显为 `>` 字符,您可以尝试以下代码:
```
- name: execute get config command on remote host
hosts: myswitch
tasks:
- name: get config
cli_command:
command_string: get config
prompt: "> "
answer: " "
register: result1
ignore_errors: True
```
在这个例子中,我们使用 `cli_command` 模块来执行 `get config` 命令,并使用 `prompt` 选项指定了命令行提示符为 `>` 字符。由于命令的输出可能会包含 `--- more ---` 字符串,我们在 `cli_command` 模块中使用 `ignore_errors: True` 来忽略错误。接下来,我们使用 `answer` 选项指定了答案为一个空格字符,这样当遇到 `--- more ---` 提示时,Ansible 就会自动执行空格字符,直到回显为 `>` 字符。最后,我们将输出结果保存在 `result1` 变量中。
请注意,`cli_command` 模块需要在目标设备上安装支持 CLI 的软件,并且需要指定正确的登录凭据,比如用户名和密码。另外,某些网络设备可能需要使用特定的协议来连接,比如 SSH 或 Telnet。您需要根据实际情况来选择正确的协议。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)