我有一个相对简单的指令失败了。以下安装服务。我想断言结果文本。正确的文本存在,但断言失败。那怎么可能?
这是带有断言的代码:
- name: Install the tomcat service on the remote machine
win_command: "{{ tomcat_install_folder }}{{ tomcat_service_installer }}"
register: cmd_result_tomcat_service_install
- assert:
that:
- "'has been installed' in cmd_result_tomcat_service_install.stdout_lines"
此处包含已断言的Text'的结果已在stdout_lines中安装。
"cmd_result_tomcat_service_install": {
"stdout": "\r\nC:\\Users\\service_ansible>SET JAVA_HOME=D:\\Products\\CPeRef\\java\\jdk1.8.0_51 \r\n\r\nC:\\Users\\service_ansible>cd /d D:\\Products\\CPeRef\\tomcat\\apache-tomcat-8.0.26\\bin \r\n\r\nD:\\Products\\CPeRef\\tomcat\\apache-tomcat-8.0.26\\bin>service.bat install CPeRef2 \r\nInstalling the service 'CPeRef2' ...\r\nUsing CATALINA_HOME: \"D:\\Products\\CPeRef\\tomcat\\apache-tomcat-8.0.26\"\r\nUsing CATALINA_BASE: \"D:\\Products\\CPeRef\\tomcat\\apache-tomcat-8.0.26\"\r\nUsing JAVA_HOME: \"D:\\Products\\CPeRef\\java\\jdk1.8.0_51\"\r\nUsing JRE_HOME: \"D:\\Products\\CPeRef\\java\\jdk1.8.0_51\\jre\"\r\nUsing JVM: \"D:\\Products\\CPeRef\\java\\jdk1.8.0_51\\jre\\bin\\server\\jvm.dll\"\r\nThe service 'CPeRef2' has been installed.\r\n",
"stdout_lines": [
"",
"C:\\Users\\service_ansible>SET JAVA_HOME=D:\\Products\\CPeRef\\java\\jdk1.8.0_51 ",
"",
"C:\\Users\\service_ansible>cd /d D:\\Products\\CPeRef\\tomcat\\apache-tomcat-8.0.26\\bin ",
"",
"D:\\Products\\CPeRef\\tomcat\\apache-tomcat-8.0.26\\bin>service.bat install CPeRef2 ",
"Installing the service 'CPeRef2' ...",
"Using CATALINA_HOME: \"D:\\Products\\CPeRef\\tomcat\\apache-tomcat-8.0.26\"",
"Using CATALINA_BASE: \"D:\\Products\\CPeRef\\tomcat\\apache-tomcat-8.0.26\"",
"Using JAVA_HOME: \"D:\\Products\\CPeRef\\java\\jdk1.8.0_51\"",
"Using JRE_HOME: \"D:\\Products\\CPeRef\\java\\jdk1.8.0_51\\jre\"",
"Using JVM: \"D:\\Products\\CPeRef\\java\\jdk1.8.0_51\\jre\\bin\\server\\jvm.dll\"",
"The service 'CPeRef2' has been installed."
]
}
}
这里出现错误信息:
TASK [tomcat : assert]
fatal: [v-sax-769-e-a.develop.ebiz.grp]: FAILED! => {
"assertion": "'has been installed' in cmd_result_tomcat_service_install.stdout_lines",
"changed": false,
"evaluated_to": false,
"failed": true
我想知道问题可能是结果实际上是一个数组吗?
答案 0 :(得分:1)
这个条件
'has been installed' in cmd_result_tomcat_service_install.stdout_lines
如果字符串列表中有完全文字字符串has been installed
,则为真(在这种情况下为stdout_lines
)。
但是这个
'has been installed' in cmd_result_tomcat_service_install.stdout
当字符串has been installed
是一个长字符串的子字符串(在这种情况下为stdout
)时,为true。
如果要独立分析行,可以使用:
cmd_result_tomcat_service_install.stdout_lines | select('search','has been installed') | list | count > 0