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

2,783 views
Skip to first unread message

Florin Lipan

unread,
Mar 22, 2016, 1:12:04 PM3/22/16
to Ansible Development
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

---

Does anyone have any idea how the Display() class is supposed to work or what I might be doing wrong here? Ideally I'd like these custom messages to be displayed in real time, even before the module execution finishes. I've read through this issue but I can imagine achieving this over a local connection might be easier (no SSH pipe). But then again, I couldn't get Display() to print anything, real time or after the module executed...

Any help would be highly appreciated :)

Brian Coca

unread,
Mar 22, 2016, 2:46:25 PM3/22/16
to Florin Lipan, Ansible Development
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.



--
----------
Brian Coca

Florin Lipan

unread,
Mar 23, 2016, 4:33:35 AM3/23/16
to Ansible Development, florin...@blacklane.com
Thanks Brian! I'm looking forward to update_json

Thomas Decaux

unread,
Nov 9, 2020, 4:04:26 AM11/9/20
to Ansible Development
You can use "action" for that.
Reply all
Reply to author
Forward
0 new messages