如果输出消息中包含“ instance”,我将尝试将telnet任务设置为失败,但是它不起作用。
这是日志:
TASK [ISAM Log to log1 and log2] **************************************************************************************************************************************
task path: /Users/sascha.mueller/Downloads/isam_log.yml:9
changed: [1.2.3.4] => {
"changed": true,
"failed_when_result": false,
"output": [
"configure system syslog no route dslamlog msg-type all\r\n\r\nError : instance does not exist\r\n\r\nISAM-ISAM_7363_Labor_Zw>#",
"configure system syslog no destination dslamlog\r\n\r\nError : instance does not exist\r\n\r\nISAM-ISAM_7363_Labor_Zw>#"
]
}
TASK [fail the play if the previous command did not succeed] **********************************************************************************************************
task path: /Users/sascha.mueller/Downloads/isam_log.yml:27
skipping: [1.2.3.4] => {
"changed": false,
"skip_reason": "Conditional result was False"
}
我也尝试过command_output.stderr例如但所有这些值都不存在
---
-
hosts: all
connection: local
become: no
tasks:
- name: ISAM Log to log1 and log2
ignore_unreachable: yes
telnet:
user: bla
password: blubb
login_prompt: "login: "
password_prompt: "password: "
timeout: 5
prompts:
- "[#|$]"
command:
- configure system syslog no route dslamlog msg-type all
- configure system syslog no destination dslamlog
register: command_output
failed_when: "'instance' in command_output.output"
- name: fail the play if the previous command did not succeed
fail:
msg: "the command failed"
when: "'instance' in command_output.output"
是否可以使用telnet命令进行检查,或者直接使用variable.output或仅使用variable.stderr进行检查?
答案 0 :(得分:0)
在示例中您可以看到output
是列表:
"output": [
"configure system syslog no route dslamlog msg-type all\r\n\r\nError : instance does not exist\r\n\r\nISAM-ISAM_7363_Labor_Zw>#",
"configure system syslog no destination dslamlog\r\n\r\nError : instance does not exist\r\n\r\nISAM-ISAM_7363_Labor_Zw>#"
]
当您询问是否为"'instance' in command_output.output"
时,您正在询问outout
中是否有一个项目,它是字符串instance
。也就是说,如果output
看起来像这样,您的表达式将为true:
"output": [
"this item does not match",
"instance"
]
您真正想问的是“ output
中的任何项目是否包含字符串instance
?”。最简单的方法可能是将所有行连接到一个字符串中:
"'instance' in ''.join(command_output.output)"
您应该可以在failed_when
的情况下使用此功能:
- name: ISAM Log to log1 and log2
ignore_unreachable: yes
telnet:
user: bla
password: blubb
login_prompt: "login: "
password_prompt: "password: "
timeout: 5
prompts:
- "[#|$]"
command:
- configure system syslog no route dslamlog msg-type all
- configure system syslog no destination dslamlog
register: command_output
failed_when: "'instance' in ''.join(command_output.output)"
或者在when
任务的fail
语句中:
- name: fail the play if the previous command did not succeed
fail:
msg: "the command failed"
when: "'instance' in ''.join(command_output.output)"