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.
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());
});
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.
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?
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
So weird that I’ve never heard of that environment variable until now.
Thanks Dmitry, that solved the problem
Have submitted a PR to Vagrant for this:
https://github.com/mitchellh/vagrant/pull/2275
Thanks all for your help
Thanks very much!
We have lots of vagrant users so this will definitely continue to keep them happy.
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
Did this ever get addressed in Vagrant?
Yep, it was resolved in Vagrant 1.3.4.