custom module is failing with MODULE FAILURE\nSee stdout/stderr for the exact error" rc:0

Hi Team,

I have written custom ansible module. Module suppose to insert data in database via graphsql template.
While executing module via playbook, it does insert the data in database. however task is failing with below error,

“msg”: “MODULE FAILURE\nSee stdout/stderr for the exact error”,
“rc”: 0

I am running this is verbose mode, module.exit_json is providing the output but in “Failed” state.
Need your help here.

Thanks,
Tejas

Have you figure this out?
It looks like ansible can’t parse the return from your module.
Do you have any prints in your module dumping extra data out?
Ansible should also be giving you stdout and stderr lines with this message.
For example, I wrote a module that prints a message that looks like json (but is invalid) and then exists:

print(“{ something: invalid }”)
module.exit_json(msg=“My Exit Message”)

And Ansible returned this to me:

TASK [my_fail] ************************************************************************************************************************************************

fatal: [localhost]: FAILED! => {
“changed”: false,
“module_stderr”: “",
“module_stdout”: "{ something: invalid }\n\n{"msg": "My Exit Message", "invocation": {"module_args": {}}}\n”,
“msg”: "MODULE FAILURE\nSee stdout/stderr for the exact error”,
“rc”: 0
}

-John

Hi John,
Apologies for the late reply,

I had figured that out, and you rightly mentioned I had few print statement in the module, that was causing the problem.

Observation:
I am using python3.6.
Custom module had couple of print statement with f-string, and few print statement are normal (print statement used before python 3.6). Print statement with f-string was not causing any issue, only normal print statement were causing module to failed.

Not sure why this behavior though.

Thanks,
Tejas

because ansible expects a single JSON document as a response on STDOUT
of the module .. if you do print statements .. you are probably
breaking that requirement, hence the error.