Michael Hoeller
Michael Hoeller

Reputation: 24308

Ansible assertion not possible in an array?

I have a relatively simple instruction which fails. The following installs a service. I want to assert the result text. The correct text is there but the assert fails. How can that be??

This is the code with the assert:

- 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"

Here the result which contains the asserted Text 'has been installed' in 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."
        ]
    }
}

Here the error message:

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

I wonder if the problem can be that the result is actually an array?

Upvotes: 0

Views: 354

Answers (1)

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68289

This condition

'has been installed' in cmd_result_tomcat_service_install.stdout_lines

is true when there is exact literal string has been installed among list of strings (stdout_lines in this case).

But this

'has been installed' in cmd_result_tomcat_service_install.stdout

is true when string has been installed is a substring of one long string (stdout in this case).

If you want to parse lines independently, you may use:

cmd_result_tomcat_service_install.stdout_lines | select('search','has been installed') | list | count > 0

Upvotes: 1

Related Questions