equality not consistent

I assume I’m making some strange error here, but I cannot figure out what the error is.

I have a “changed_when” clause I’ve constructed that never seems to work. However, if I take out the clause into its own debug statement, it seems to work.

I have reproduced this with the following ansible:

`

  • hosts: localhost
    gather_facts: False
    tasks:

  • name: Update Jenkins-Jobs on Jenkins Master - docker container
    command: |
    echo "INFO:jenkins_jobs.cli.subcommand.update:Updating jobs in [‘./jobs/’] ()
    INFO:root:Caching type parameters of parameters = jenkins_jobs.modules.parameters:Parameters
    INFO:root:Caching type builders of builders = jenkins_jobs.modules.builders:Builders
    INFO:jenkins_jobs.builder:Number of jobs generated: 1
    INFO:jenkins_jobs.cli.subcommand.update:Number of jobs updated: 0
    INFO:jenkins_jobs.builder:Number of views generated: 0
    INFO:jenkins_jobs.cli.subcommand.update:Number of views updated: 0
    "
    register: jjb_update
    changed_when:

  • ( jjb_update.stdout_lines | regex_replace(‘.jobs updated.\s+(\d+).’, ‘\1’) != ‘0’ )

  • name: Number of jobs updated
    debug:
    msg: |
    {{ jjb_update.stdout_lines | regex_replace(‘.jobs updated.\s+(\d+).’, ‘\1’) }}

  • name: Number of jobs updated equals 0?
    debug:
    msg: |
    {{ jjb_update.stdout_lines | regex_replace(‘.jobs updated.\s+(\d+).’, ‘\1’) == ‘0’ }}

`

The output I get in both 2.8.6 and 2.9.1 is

`

TASK [Update Jenkins-Jobs on Jenkins Master - docker container] ************************************************************************************************************
changed: [localhost]

TASK [Number of jobs updated] **********************************************************************************************************************************************
ok: [localhost] => {
“msg”: “0\n”
}

TASK [Number of jobs updated equals 0?] ************************************************************************************************************************************
ok: [localhost] => {
“msg”: “True\n”
}

`

If the output is 0 and the check is true, the first task should NOT show changed.

jjb_update.stdout_lines is a list and the regex_replace filter work on strings, so it probably should have failed.

You can use the search test instead on stdout

   changed_when: jjb_update.stdout is not search('jobs updated:.0')

This seems to have been some sort of quoting issue.

After much troubleshooting, I found the working solution:

changed_when:

  • ((jjb_update.stderr_lines | regex_replace(‘.jobs updated.\s+(\d+).’, ‘\1’)) != ‘0’ )