Repository: ansible/ansible

Issue:
Ansible's debug module doesn't print vars having a name comprising of a single letter, present in a word "invocation"
##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
debug

##### ANSIBLE VERSION
```
ansible 2.4.2.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/yuri/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.14 (default, Sep 20 2017, 01:25:59) [GCC 7.2.0]
```

##### OS / ENVIRONMENT
N/A

##### STEPS TO REPRODUCE
`playbook.yml`:

```
- hosts: all
  tasks:
    - set_fact: a=123
    - debug: var=a
    - set_fact: b=123
    - debug: var=b
```

```
$ ansible-playbook -i hosts -l HOST playbook.yml
```

##### EXPECTED RESULTS
variable `a` to be displayed

##### ACTUAL RESULTS
```
PLAY [all] ************************************************************************************

TASK [Gathering Facts] ************************************************************************
ok: [production]

TASK [set_fact] *******************************************************************************
ok: [production]

TASK [debug] **********************************************************************************
ok: [production] => {}

TASK [set_fact] *******************************************************************************
ok: [production]

TASK [debug] **********************************************************************************
ok: [production] => {
    "b": "123"
}

PLAY RECAP ************************************************************************************
production                 : ok=5    changed=0    unreachable=0    failed=0
```

The reason is at [this line][1]. Missing comma?

[1]: https://github.com/ansible/ansible/pull/31812/files#diff-ab09fa2a9291a017c38c5db420d7ee0fR222


Discussion:
Files identified in the description:
* [lib/ansible/modules/utilities/logic/debug.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/utilities/logic/debug.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @dagwieers
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
So, it probably makes sense to mention @bcoca here, and try to add component:

!component +lib/ansible/plugins/callback/__init__.py
Files identified in the description:
* [lib/ansible/modules/utilities/logic/debug.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/utilities/logic/debug.py)
* [lib/ansible/plugins/callback/__init__.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/callback/__init__.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->

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
@@ -236,9 +236,7 @@ def _process_items(self, result):
     def _clean_results(self, result, task_name):
         ''' removes data from results for display '''
         if task_name in ['debug']:
-            for remove_key in ('invocation'):
-                if remove_key in result:
-                    del result[remove_key]
+            result.pop('invocation', None)
 
     def set_play_context(self, play_context):
         pass


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
@@ -67,15 +67,53 @@ def test_get_item_no_log(self):
         res = cb._get_item(results)
         self.assertEquals(res, "some_item")
 
-    def test_clean_results(self):
+    def test_clean_results_debug_task(self):
         cb = CallbackBase()
         result = {'item': 'some_item',
                   'invocation': 'foo --bar whatever [some_json]',
+                  'a': 'a single a in result note letter a is in invocation',
+                  'b': 'a single b in result note letter b is not in invocation',
                   'changed': True}
 
+        cb._clean_results(result, 'debug')
+
+        # See https://github.com/ansible/ansible/issues/33723
+        self.assertTrue('a' in result)
+        self.assertTrue('b' in result)
+        self.assertFalse('invocation' in result)
         self.assertTrue('changed' in result)
-        self.assertTrue('invocation' in result)
+
+    def test_clean_results_debug_task_no_invocation(self):
+        cb = CallbackBase()
+        result = {'item': 'some_item',
+                  'a': 'a single a in result note letter a is in invocation',
+                  'b': 'a single b in result note letter b is not in invocation',
+                  'changed': True}
+
         cb._clean_results(result, 'debug')
+        self.assertTrue('a' in result)
+        self.assertTrue('b' in result)
+        self.assertTrue('changed' in result)
+        self.assertFalse('invocation' in result)
+
+    def test_clean_results_debug_task_empty_results(self):
+        cb = CallbackBase()
+        result = {}
+        cb._clean_results(result, 'debug')
+        self.assertFalse('invocation' in result)
+        self.assertEqual(len(result), 0)
+
+    def test_clean_results(self):
+        cb = CallbackBase()
+        result = {'item': 'some_item',
+                  'invocation': 'foo --bar whatever [some_json]',
+                  'a': 'a single a in result note letter a is in invocation',
+                  'b': 'a single b in result note letter b is not in invocation',
+                  'changed': True}
+
+        expected_result = result.copy()
+        cb._clean_results(result, 'ebug')
+        self.assertEqual(result, expected_result)
 
 
 class TestCallbackDumpResults(unittest.TestCase):