Repository: ansible/ansible

Issue:
"ansible_facts" empty when used to output module result
##### ISSUE TYPE
 - Bug Report

##### ANSIBLE VERSION
<!--- Paste verbatim output from “ansible --version” between quotes below -->
```
ansible 2.2.1.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
```

##### OS / ENVIRONMENT
Control host: Fedora 23
Remote host: RHEL 7

##### SUMMARY
When using an ansible module, such as [docker_container](https://docs.ansible.com/ansible/docker_container_module.html), the module's output is placed under a key named [ansible_facts](https://github.com/ansible/ansible-modules-core/blob/devel/cloud/docker/docker_container.py#L1672) in the dictionary that it returns. However, that key returns with an empty value by the time it has gone through [module.exit_json](https://github.com/ansible/ansible-modules-core/blob/devel/cloud/docker/docker_container.py#L2010).

Note that is is only an issue when executing a module from a python file using the [_execute_module](https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/action/__init__.py#L608) method.

##### STEPS TO REPRODUCE
1. Execute an ansible module, that places its results under `ansible_facts` in its returned dictionary (such as `docker_container`), from a python file using the [_execute_module](https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/action/__init__.py#L608) method
and attempt to read results.

<!--- Paste example playbooks or commands between quotes below -->
```
results = self._execute_module("docker_container", {"name": "my_image"}, task_vars)
return {"msg": "%s" % results["ansible_facts"]} # empty output
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
The module's documented return value https://docs.ansible.com/ansible/docker_container_module.html#id5

##### ACTUAL RESULTS
The module's result was filtered out.

Original issue opened here: https://github.com/openshift/openshift-ansible/issues/3564

cc @rhcarvalho @detiber @jimi-c 



Discussion:
From @detiber in https://github.com/openshift/openshift-ansible/issues/3564
```
@juanvallejo @rhcarvalho It looks like it is by design:
https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/action/__init__.py#L776-L834
```

From @detiber in https://github.com/openshift/openshift-ansible/issues/3564
```
@juanvallejo @rhcarvalho: actually, I think it's getting sanitized because of the Encoding/Decoding issue in 2.2.1.0: https://github.com/ansible/ansible/blob/devel/lib/ansible/vars/unsafe_proxy.py
```
anything starting with `ansible_docker_` will get removed, we need to update the plugin to avoid that namespace

Resolving patch:
diff --git a/lib/ansible/modules/cloud/docker/docker_container.py b/lib/ansible/modules/cloud/docker/docker_container.py
--- a/lib/ansible/modules/cloud/docker/docker_container.py
+++ b/lib/ansible/modules/cloud/docker/docker_container.py
@@ -611,8 +611,9 @@
 '''
 
 RETURN = '''
-ansible_docker_container:
+docker_container:
     description:
+      - Before 2.3 this was 'ansible_docker_container' but was renamed due to conflicts with the connection plugin.
       - Facts representing the current state of the container. Matches the docker inspection output.
       - Note that facts are not part of registered vars but accessible directly.
       - Empty if C(state) is I(absent)
@@ -1675,7 +1676,7 @@ def __init__(self, client):
             self.results['diff'] = self.diff
 
         if self.facts:
-            self.results['ansible_facts'] = {'ansible_docker_container': self.facts}
+            self.results['ansible_facts'] = {'docker_container': self.facts}
 
     def present(self, state):
         container = self._get_container(self.parameters.name)


Test patch: