2013-03-20 14:41:21 +00:00
|
|
|
module VagrantPlugins
|
|
|
|
module Ansible
|
|
|
|
class Provisioner < Vagrant.plugin("2", :provisioner)
|
|
|
|
def provision
|
|
|
|
ssh = @machine.ssh_info
|
2013-04-04 20:48:58 +00:00
|
|
|
|
2013-03-20 14:41:21 +00:00
|
|
|
options = %W[--private-key=#{ssh[:private_key_path]} --user=#{ssh[:username]}]
|
2013-04-05 15:43:45 +00:00
|
|
|
options << "--extra-vars=" + config.extra_vars.map{|k,v| "#{k}=#{v}"}.join(' ') if config.extra_vars
|
2013-03-20 14:41:21 +00:00
|
|
|
options << "--inventory-file=#{config.inventory_file}" if config.inventory_file
|
|
|
|
options << "--ask-sudo-pass" if config.ask_sudo_pass
|
2013-04-04 20:48:58 +00:00
|
|
|
|
2013-03-20 14:41:21 +00:00
|
|
|
if config.limit
|
|
|
|
if not config.limit.kind_of?(Array)
|
|
|
|
config.limit = [config.limit]
|
|
|
|
end
|
|
|
|
config.limit = config.limit.join(",")
|
|
|
|
options << "--limit=#{config.limit}"
|
|
|
|
end
|
2013-04-04 20:48:58 +00:00
|
|
|
|
2013-03-20 14:41:21 +00:00
|
|
|
options << "--sudo" if config.sudo
|
|
|
|
options << "--sudo-user=#{config.sudo_user}" if config.sudo_user
|
|
|
|
options << "--verbose" if config.verbose
|
2013-04-04 20:48:58 +00:00
|
|
|
|
2013-04-04 07:07:59 +00:00
|
|
|
# Assemble the full ansible-playbook command
|
2013-03-20 14:41:21 +00:00
|
|
|
command = (%w(ansible-playbook) << options << config.playbook).flatten
|
2013-04-04 20:48:58 +00:00
|
|
|
|
2013-04-04 07:07:59 +00:00
|
|
|
# Write stdout and stderr data, since it's the regular Ansible output
|
2013-04-04 18:31:27 +00:00
|
|
|
command << {
|
|
|
|
:env => { "ANSIBLE_FORCE_COLOR" => "true" },
|
|
|
|
:notify => [:stdout, :stderr]
|
|
|
|
}
|
2013-04-04 20:48:58 +00:00
|
|
|
|
2013-04-04 20:58:33 +00:00
|
|
|
begin
|
2013-07-20 04:07:09 +00:00
|
|
|
exit_status = Vagrant::Util::Subprocess.execute(*command) do |type, data|
|
2013-04-04 20:58:33 +00:00
|
|
|
if type == :stdout || type == :stderr
|
2013-07-20 03:38:25 +00:00
|
|
|
@machine.env.ui.info(data, :new_line => false, :prefix => false)
|
2013-04-04 20:58:33 +00:00
|
|
|
end
|
2013-04-04 20:48:58 +00:00
|
|
|
end
|
2013-07-20 04:07:09 +00:00
|
|
|
|
|
|
|
raise Vagrant::Errors::AnsibleFailed if exit_status != 0
|
2013-04-04 20:58:33 +00:00
|
|
|
rescue Vagrant::Util::Subprocess::LaunchError
|
|
|
|
raise Vagrant::Errors::AnsiblePlaybookAppNotFound
|
2013-04-04 07:07:59 +00:00
|
|
|
end
|
2013-03-20 14:41:21 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|