when does ansible-playbook write to stdout?

3,236 views
Skip to first unread message

Jake Clarkson

unread,
Sep 25, 2013, 9:12:56 AM9/25/13
to ansible...@googlegroups.com
Hey guys,

Since using Ansible >= 1.3.0 I've noticed that when provisioning a Vagrant VM the output of `ansible-playbook` is not shown until it has finished (whether it be an error or a successful provision).

I've had a look in the Vagrant source code and in https://github.com/mitchellh/vagrant/blob/master/plugins/provisioners/ansible/provisioner.rb the following can be seen:

result = Vagrant::Util::Subprocess.execute(*command) do |type, data|
if type == :stdout || type == :stderr
   @machine.env.ui.info(data, :new_line => false, :prefix => false)
  end
end

This looks to me like it's just redirecting stdout/err from `ansible-playbook` to the host machine's terminal. I have raised a ticket with Vagrant (https://github.com/mitchellh/vagrant/issues/2194).

If I run `ansible-playbook` by hand in the terminal I get 'realtime' feedback of task execution rather than solely at the end of the run, e.g.

ansible-playbook \
  ~/Development/playbook.yml \
  -i ~/Development/inventory \
  -u vagrant \
  --private-key=~/.vagrant.d/insecure_private_key

My question is, when does Ansible write to stdout? Did this change at all between 1.2.x -> 1.3?

Many thanks :-)

Everyone Advertises

http://www.digitalanimal.com

Digital Animal Limited is registered in England and Wales under company number: 07757607. Registered office 47 Castle Street Reading RG1 7SR. VAT No: GB 121 1672 57

This electronic message contains information from Digital Animal Ltd which may be privileged or confidential. The information is intended to be for the use of the individuals or entity named above. If you are not the intended recipient be aware that any disclosure, copying, distribution or use of the contents of this information is prohibited. If you have received this electronic message in error, please notify us by telephone or email (to the numbers or address above) immediately.

Jake Clarkson

unread,
Sep 25, 2013, 9:19:57 AM9/25/13
to ansible...@googlegroups.com
In order to replicate I have created a simple node.js script:

#!/usr/bin/env node
var spawn = require('child_process').spawn;
var args = [
  '/Users/jake/Development/playbook.yml',
  '-i', '/Users/jake/Development/inventory',
  '-u', 'vagrant',
  '--private-key', '/Users/jake/.vagrant.d/insecure_private_key'
];

var proc = spawn('ansible-playbook', args, {
  cwd: process.cwd()
});

proc.stdout.on('data', function(data) {
  console.log(data.toString());
});

proc.stderr.on('data', function(data) {
  console.error(data.toString());
});

Michael DeHaan

unread,
Sep 25, 2013, 9:37:36 AM9/25/13
to ansible...@googlegroups.com
Yes, ansible obviously writes to stdout.

I'm pretty sure you have a problem with vagrant here, and this should be reported on the vagrant bug tracker.




--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Michael DeHaan <mic...@ansibleworks.com>
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/

Jake Clarkson

unread,
Sep 25, 2013, 9:47:57 AM9/25/13
to ansible...@googlegroups.com
Thanks for the reply.

I agree it looks like a Vagrant issue. It does seem strange that if I use Ansible 1.2.x with the same version of Vagrant (1.3.3) I get the output on a task-by-task basis.

With the node.js example would you expect that just to trigger the 'data' event of the stdout at the end or after each task?

Dmitry Horbach

unread,
Sep 25, 2013, 4:28:35 PM9/25/13
to ansible...@googlegroups.com
Yes I noticed the same after upgrade to 1.3 Ansible.
 export PYTHONUNBUFFERED=1
This fixes the issue if ansible execution is wrapped with some other process

Michael DeHaan

unread,
Sep 25, 2013, 7:52:19 PM9/25/13
to ansible...@googlegroups.com
So weird that I've never heard of that environment variable until now.




--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Jake Clarkson

unread,
Sep 26, 2013, 3:59:01 AM9/26/13
to ansible...@googlegroups.com
Thanks Dmitry, that solved the problem :-)

Jake Clarkson

unread,
Sep 26, 2013, 5:19:39 AM9/26/13
to ansible...@googlegroups.com
Have submitted a PR to Vagrant for this:


Thanks all for your help :-)

Michael DeHaan

unread,
Sep 26, 2013, 8:27:26 AM9/26/13
to ansible...@googlegroups.com
Thanks very much!

We have lots of vagrant users so this will definitely continue to keep them happy.




--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Taylor Brown

unread,
Dec 9, 2013, 4:01:59 PM12/9/13
to ansible...@googlegroups.com
Thanks for this fix! I ran into this issue when running ansible-playbook as part of a shell provisioning script in Vagrant. In other words, Vagrant is just running a shell script, and the shell script is running "sudo ansible-playbook" in local mode. However, I just wanted to point out that in this case, the following doesn't work as I expected:

export PYTHONUNBUFFERED=1
sudo ansible-playbook /vagrant/provisioning/playbook.yml --connection=local -i "[database]127.0.0.1,"

Of course, I'm a Linux noobie, and it took me a while to realize I needed to do this instead:
sudo PYTHONUNBUFFERED=1 ansible-playbook ...

Or this:
sudo python -u `which ansible-playbook` ...

The "PYTHONUNBUFFERED" env variable didn't survive the call to sudo, which makes sense in hindsight. Anyway, in case anyone else is running into this instead of using Vagrant's built-in Ansible provisioner, I wanted to post the solution here since this thread is a likely place to stumble.

Taylor

Michael DeHaan

unread,
Dec 10, 2013, 12:33:23 PM12/10/13
to ansible...@googlegroups.com
Did this ever get addressed in Vagrant?


Jake Clarkson

unread,
Dec 10, 2013, 12:35:23 PM12/10/13
to ansible...@googlegroups.com

Yep, it was resolved in Vagrant 1.3.4.

You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/aXutTmXAbR0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
Reply all
Reply to author
Forward
0 new messages