Repository: ansible/ansible

Issue:
"changed_when: True" does not display the task as changed
Ansible version 2.0.0.2
Expected "Test changed_when" to be "changed" and yellow. Was "ok" and green instead.

**changed_when_test.yml:**

``` yaml

---
- name: Test Play
  hosts: localhost
  gather_facts: no
  tasks:
    - name: Test changed_when
      debug: msg="This should be changed"
      changed_when: True
```

**Result:**

```
PLAY [Test Play] *********************************************

TASK [Test changed_when] *******************************************************
ok: [localhost] => {
    "msg": "This should be changed"
}

PLAY RECAP *********************************************************************
localhost        : ok=1    changed=1    unreachable=0    failed=0
```



Discussion:
so the status IS set to changed, though the debug output is 'hardcoded' as OK as it cannot change the acutal machines (play summary does show 1 changed task). You can see the status using the following:

```
---
- name: Test Play
  hosts: localhost
  gather_facts: no
  tasks:
    - debug: msg="test"
      changed_when: True
      register: mystatus

   - debug: var=mystatus
```

output:

```
PLAY ***************************************************************************

TASK [debug] *******************************************************************
ok: [localhost] => {
    "msg": "test"
}

TASK [debug] *******************************************************************
ok: [localhost] => {
    "mystatus": {
        "changed": true, 
        "msg": "test"
    }
}

PLAY RECAP *********************************************************************
localhost                  : ok=1   changed=1    unreachable=0    failed=0   
```

That's confusing. It counts as changed in the recap, but not the task? In Ansible 1.9, both recap count and task display showed changed.

That is why i didn't close this, still an issue and can be used by subsequent tasks. Not sure why you would want this 'feature' to display changed though.

A fair point. My use case is a bit goofy. If important tasks in my playbook are changed, I notify a "you should know this" handler. I set changed_when on the handler so that it's yellow and stands out. Something like:

``` yaml
handlers:
  - name: elasticsearch config changed
    debug: msg="ES config changed! Rolling restart needed"
    changed_when: True
```

The reason I do this is that some services are painful or dangerous to restart, so it's best to just know that it needs to happen.

Just now, I did a Pull Request that solves this issue. While it might not be a 'big' issue, it seemed like a good start to get involved in the project. At least I learned a lot by debugging it.


Resolving patch:
diff --git a/lib/ansible/plugins/callback/__init__.py b/lib/ansible/plugins/callback/__init__.py
--- a/lib/ansible/plugins/callback/__init__.py
+++ b/lib/ansible/plugins/callback/__init__.py
@@ -192,8 +192,6 @@ def _process_items(self, result):
         del result._result['results']
 
     def _clean_results(self, result, task_name):
-        if 'changed' in result and task_name in ['debug']:
-            del result['changed']
         if 'invocation' in result and task_name in ['debug']:
             del result['invocation']
 


Test patch:
diff --git a/test/units/plugins/callback/test_callback.py b/test/units/plugins/callback/test_callback.py
--- a/test/units/plugins/callback/test_callback.py
+++ b/test/units/plugins/callback/test_callback.py
@@ -76,8 +76,6 @@ def test_clean_results(self):
         self.assertTrue('changed' in result)
         self.assertTrue('invocation' in result)
         cb._clean_results(result, 'debug')
-        self.assertFalse('changed' in result)
-        self.assertFalse('invocation' in result)
 
 
 class TestCallbackDumpResults(unittest.TestCase):