Why does my custom Ansible module fail?

2019-08-09 16:23发布

问题:

I am learning ansible so I wrote the most simple playbook and module I could think of, and it failed.

My playbook

---
- hosts: demo
  tasks:
    - name: install demo
      action: install

My module

echo "changed=True msg=OK"

The following seems fine :

  • bash script mode is 755
  • playbook is linked to module correctly

This is the output:

FAILED! => {"changed": false, "failed": true, "module_stderr": "", "module_stdout": "changed=True msg=OK\r\n", "msg": "MODULE FAILURE", "parsed": false}

What am I doing wrong? How can I make this work?

回答1:

The output of a module must be JSON.

Try this:

echo "{\"changed\": false, \"msg\" : \"ok\"}"

From the docs:

You should also never do this in a module:

print "some status message"

Because the output is supposed to be valid JSON.

and:

If a module returns stderr or otherwise fails to produce valid JSON, the actual output will still be shown in Ansible, but the command will not succeed.