# Custom Windows module output problem

**URL:** <https://forum.ansible.com/t/custom-windows-module-output-problem/16486>\
**Category:** Ansible Developer\
**Tags:** windows\
**Created:** [October 10, 2014, 4:07pm UTC](https://forum.ansible.com/t/custom-windows-module-output-problem/16486 "2014-10-10T16:07:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ivaylo\_Bratoev](https://avatars.discourse-cdn.com/v4/letter/i/f9ae1b/32.png) [@Ivaylo\_Bratoev](https://forum.ansible.com/u/Ivaylo_Bratoev)\
**Post date:** [October 10, 2014, 4:07pm UTC](https://forum.ansible.com/t/custom-windows-module-output-problem/16486/1 "2014-10-10T16:07:35Z")

</div>

I have a custom Windows module that uses DSC modules to perform some configuration (using Start-DscConfiguration commandlet). The important part is that this command output **lines starting with “[”**. Ansible gets confused and returns error with “parsed: false”. Quickly browsing in the code it seems that the \ ***ilter\_leading\_non\_json\_lines** function is responsible for that as it _“need to filter anything which starts not with ‘{’, ‘[’, ', ‘=’ or is an empty line.”_.

My questions are:

Is this expected and _by design_?  
If it is, what is the recommend approach to workaround the issue (ex: prefix the all lines with something)?

Ivaylo Bratoev

---

<div class="post-metadata">

**Author:** ![Chris\_Church1](https://avatars.discourse-cdn.com/v4/letter/c/ac8455/32.png) [@Chris\_Church1](https://forum.ansible.com/u/Chris_Church1)\
**Post date:** [October 10, 2014, 5:21pm UTC](https://forum.ansible.com/t/custom-windows-module-output-problem/16486/2 "2014-10-10T17:21:43Z")

</div>

Are you using PowerShell's ConvertTo-Json to generate output, or just  
letting the output from Start-DscConfiguration pass through?

Ansible tries to parse stdout as JSON, so running a command that may  
generate its own output could break things. I generally try to capture the  
output into a variable ($output = Some-Cmd) or discard it (Some-Cmd |  
Out-Null) to make sure the only thing getting to stdout is the JSON  
response.

Do you have an example of your output that doesn't parse?

---

<div class="post-metadata">

**Author:** ![Ivaylo\_Bratoev](https://avatars.discourse-cdn.com/v4/letter/i/f9ae1b/32.png) [@Ivaylo\_Bratoev](https://forum.ansible.com/u/Ivaylo_Bratoev)\
**Post date:** [October 10, 2014, 7:35pm UTC](https://forum.ansible.com/t/custom-windows-module-output-problem/16486/3 "2014-10-10T19:35:15Z")

</div>

I am using the output from Start-DscConfiguration with -Verbose switch. The module is not really interesting. It wraps some old DSC config files in an Ansible module. To better describe the issue, here is a sample module that reproduces the problem clearly:

#!powershell

# WANT\_JSON

# POWERSHELL\_COMMON

“[ this is a bug”

$result = New-Object psobject  
Set-Attr $result “changed” $true  
Exit-Json $result

I will try using ConvertTo-Json to workaround the issue.

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [October 10, 2014, 10:48pm UTC](https://forum.ansible.com/t/custom-windows-module-output-problem/16486/4 "2014-10-10T22:48:27Z")

</div>

Ah, yes. In general, the only thing your module should output is just the JSON at the end. You will want to ensure that nothing else prints out anything before then.

---

<div class="post-metadata">

**Author:** ![Chris\_Church1](https://avatars.discourse-cdn.com/v4/letter/c/ac8455/32.png) [@Chris\_Church1](https://forum.ansible.com/u/Chris_Church1)\
**Post date:** [October 11, 2014, 5:04am UTC](https://forum.ansible.com/t/custom-windows-module-output-problem/16486/5 "2014-10-11T05:04:51Z")

</div>

For Python-based modules, there is a run\_command method to allow a module  
to execute external commands and capture return code, stdout and stderr.

It may make sense to have a common PowerShell function to wrap  
Invoke-Command or Start-Process for any modules needing to run other  
programs (may something similar to  
[https://github.com/ansible/ansible/pull/8860/files#diff-a040ed616580e4687eb4f5032f8d6388R819](https://github.com/ansible/ansible/pull/8860/files#diff-a040ed616580e4687eb4f5032f8d6388R819)  
).

---

<div class="post-metadata">

**Author:** ![Ivaylo\_Bratoev](https://avatars.discourse-cdn.com/v4/letter/i/f9ae1b/32.png) [@Ivaylo\_Bratoev](https://forum.ansible.com/u/Ivaylo_Bratoev)\
**Post date:** [October 14, 2014, 6:43pm UTC](https://forum.ansible.com/t/custom-windows-module-output-problem/16486/6 "2014-10-14T18:43:38Z")

</div>

Thanks all,

I got confused because my custom module worked fine with printing to output lines not starting with [ 🙂 It seems the _filter\_leading\_non\_json\_lines_ function fixed that for me till now. I understand that it is not expected to print anything except the result so I just fixed my modules to do that.

What I miss now for my modules is a way to log some verbose output. Is there any way to log verbose log from modules?

---

<div class="post-metadata">

**Author:** ![Trond\_Hindenes](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@Trond\_Hindenes](https://forum.ansible.com/u/Trond_Hindenes)\
**Post date:** [October 14, 2014, 9:24pm UTC](https://forum.ansible.com/t/custom-windows-module-output-problem/16486/7 "2014-10-14T21:24:02Z")

</div>

DSC verbose is very chatty. Instead you could probably run your DSC config, and then use the LCM CIM classes to check whether anything was changed. If you need more logging than that, you could pick that up from the event logs in object-shape so that you’d avoid pulling your hair out parsing the output. Life’s too short for unnecessary parsing.

Let me know if you need help getting started, a general-purpose DSC module could be very valuable to a lot of people.

-Trond

---

<div class="post-metadata">

**Author:** ![Ivaylo\_Bratoev](https://avatars.discourse-cdn.com/v4/letter/i/f9ae1b/32.png) [@Ivaylo\_Bratoev](https://forum.ansible.com/u/Ivaylo_Bratoev)\
**Post date:** [October 14, 2014, 9:46pm UTC](https://forum.ansible.com/t/custom-windows-module-output-problem/16486/8 "2014-10-14T21:46:33Z")

</div>

Actually, in the end I changed my approach to not run DSC but copied the DSC code in Ansible modules themselves (like Chris suggested in a previous reply). IMO, this is a cleaner approach.

About the verbose log I think I didn’t explain it clearly enough. I am looking for the following functionality (or something similar):

- Log something verbose in the module like:

> `
> 
> > log-verbose “this is verbose log”
> 
> `

- When I run ansible-playbook with -vvv for example, I would like to see the verbose log.
- When I run ansible-playbook with less or no ‘-v’-s I would like to _not_ see the verbose log.

My current approach is to include verbose log in the module result directly (for example with a ‘verbose-out’ key.  
Is there any other Ansible way of doing this?
