failed_when/changed_when with script module

I’m using ansible 2.0, and I’m trying to create a script play which will correctly flag when a change was made, but so far I’ve been unable to get this to work. Here’s what I have for the play:

  • script: script {{ arg }}
    register: command_result
    failed_when: command_result.rc != 0 and command_result.rc != 1
    changed_when: command_result.rc == 1

The script runs correctly, and reports success if no change happens. But, if it exits with code 1 to indicate that a change was made ansible is reporting that it failed:

fatal: [vagrant]: FAILED! => {“stdout”: “”, “changed”: true, “failed”: false, “stderr”: “”, “rc”: 1, “failed_when_result”: false}

I’ve also tried using:

  • script: script {{ arg }}

register: command_result
changed_when: “‘updated’ in command_result.stderr”

With the script using stderr rather than the exit status to report the change, but in that case ansible never reports that a change was made.

Is ideas about what I’m doing wrong?

Thanks.