Repository: ansible/ansible-modules-core

Issue:
Show stdout from validating command in template module
##### Issue Type: Feature Idea
##### Ansible Version: 1.9.4
##### Summary:

The `validate` option of template module allow checking the final file before moving it to the destination, but the output from Ansible is not very useful right now. Only stderr and the return code are shown, but a lot of tools prompt everything to stdout, and the option doesn't allow to use shell tricks. It would be very nice to get both stdout and stderr of the validating command from Ansible.
##### Steps To Reproduce:

Tried with a configuration file (named.conf) for bind9. A validation error results in:

```
failed: [myhost] => {"failed": true}
msg: failed to validate: rc:1 error:
```
##### Expected Results:

In the previous case, the prompted error should be:

```
failed: [myhost] => {"failed": true}
msg: failed to validate: rc:1 error: /etc/bind/named.conf:63: option 'allow-query' is not allowed in 'forward' zone 'my.zone'
```

Show stdout from validating command in template module
##### Issue Type: Feature Idea
##### Ansible Version: 1.9.4
##### Summary:

The `validate` option of template module allow checking the final file before moving it to the destination, but the output from Ansible is not very useful right now. Only stderr and the return code are shown, but a lot of tools prompt everything to stdout, and the option doesn't allow to use shell tricks. It would be very nice to get both stdout and stderr of the validating command from Ansible.
##### Steps To Reproduce:

Tried with a configuration file (named.conf) for bind9. A validation error results in:

```
failed: [myhost] => {"failed": true}
msg: failed to validate: rc:1 error:
```
##### Expected Results:

In the previous case, the prompted error should be:

```
failed: [myhost] => {"failed": true}
msg: failed to validate: rc:1 error: /etc/bind/named.conf:63: option 'allow-query' is not allowed in 'forward' zone 'my.zone'
```



Discussion:
[module: files/template.py]

@komuta Thanks for submitting this feature idea.

We encourage all Ansible community members to submit pull requests to satisfy feature requests. Consult the module guidelines (http://docs.ansible.com/ansible/developing_modules.html) for more information.

@ansible/core team -- for the template module.

I would expand this to all modules that use validate, also rc, stdout and stderr should be returned to the registered var 

Alright. I've already a local patch, so I'll submit a pull request. On which target branch should I submit the patch ? I'm currently branched on **stable-1.9**, is this fine?

[module: files/template.py]

@komuta Thanks for submitting this feature idea.

We encourage all Ansible community members to submit pull requests to satisfy feature requests. Consult the module guidelines (http://docs.ansible.com/ansible/developing_modules.html) for more information.

@ansible/core team -- for the template module.

I would expand this to all modules that use validate, also rc, stdout and stderr should be returned to the registered var 

Alright. I've already a local patch, so I'll submit a pull request. On which target branch should I submit the patch ? I'm currently branched on **stable-1.9**, is this fine?


Resolving patch:
diff --git a/files/copy.py b/files/copy.py
--- a/files/copy.py
+++ b/files/copy.py
@@ -279,7 +279,7 @@ def main():
                 # os.path.exists() can return false in some
                 # circumstances where the directory does not have
                 # the execute bit for the current user set, in
-                # which case the stat() call will raise an OSError 
+                # which case the stat() call will raise an OSError
                 os.stat(os.path.dirname(dest))
             except OSError, e:
                 if "permission denied" in str(e).lower():
@@ -308,7 +308,7 @@ def main():
                     module.fail_json(msg="validate must contain %%s: %s" % (validate))
                 (rc,out,err) = module.run_command(validate % src)
                 if rc != 0:
-                    module.fail_json(msg="failed to validate: rc:%s error:%s" % (rc,err))
+                    module.fail_json(msg="failed to validate", exit_status=rc, stdout=out, stderr=err)
             if remote_src:
                 _, tmpdest = tempfile.mkstemp(dir=os.path.dirname(dest))
                 shutil.copy2(src, tmpdest)


Test patch: