Custom Windows module output problem

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

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?

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.

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.

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
).

Thanks all,

I got confused because my custom module worked fine with printing to output lines not starting with [ :slight_smile: 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?

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

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?