Custom module: Using Display() to print messages (ideally in real time) over a local connection

I’m running a set of custom Ansible modules which are ment for local connections only.

I was trying to use Display() to print out some custom output - ideally in real time. I’m using Ansible 2.0.1.0 on Ubuntu 14.04.

This would be my (simplified) custom module:

`

# library/output.py

#!/usr/bin/python

from ansible.utils.display import Display
display = Display() # also tried Display(x) here - where x = 0..5
display.display(u"hello world")
display.vv(u"hello world")
display.banner(u"hello world")

print "{}"

`

This would be the playbook using the custom module (emphasis on the local connection):

`

# output.yml

- hosts: localhost
  connection: local
  tasks:
  - output:

`

And these are the various ways I tried to run this:

`

ansible-playbook output.yml
ansible-playbook output.yml -vvv
ansible-playbook output.yml -f 1
PYTHONUNBUFFERED=1 ansible-playbook output.yml

`

I also tried combinations of these (one fork & verbose etc.)

I was expecting at least one the “hello world” messages to show up in the output, however none of the commands did print anything:

`

PLAY ***************************************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [output] ******************************************************************
ok: [localhost]

PLAY RECAP *********************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0 

`

That won’t work, modules are run in their own fork and their stdout/stderr are captured into the task results.

So using Display or direct print will not send the output directly to the user’s screen. Currently modules have no way of doing this, I am working on adding an update_json function that cand do this but it is still not ready.

Thanks Brian! I’m looking forward to update_json

You can use “action” for that.