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-09-07 12:32:36 +00:00
|
|
|
# Connect with Vagrant user (unless --user or --private-key are overidden by 'raw_arguments')
|
2013-03-20 14:41:21 +00:00
|
|
|
options = %W[--private-key=#{ssh[:private_key_path]} --user=#{ssh[:username]}]
|
2013-05-06 20:28:20 +00:00
|
|
|
|
|
|
|
# Joker! Not (yet) supported arguments can be passed this way.
|
|
|
|
options << "#{config.raw_arguments}" if config.raw_arguments
|
|
|
|
|
2013-09-07 12:32:36 +00:00
|
|
|
# Append Provisioner options (higher precedence):
|
2013-08-09 18:51:36 +00:00
|
|
|
if config.extra_vars
|
|
|
|
extra_vars = config.extra_vars.map do |k,v|
|
|
|
|
v = v.gsub('"', '\\"')
|
|
|
|
if v.include?(' ')
|
|
|
|
v = v.gsub("'", "\\'")
|
|
|
|
v = "'#{v}'"
|
|
|
|
end
|
|
|
|
|
|
|
|
"#{k}=#{v}"
|
|
|
|
end
|
|
|
|
options << "--extra-vars=\"#{extra_vars.join(" ")}\""
|
|
|
|
end
|
2013-09-07 12:32:36 +00:00
|
|
|
options << "--inventory-file=#{self.setup_inventory_file}"
|
2013-03-20 14:41:21 +00:00
|
|
|
options << "--sudo" if config.sudo
|
|
|
|
options << "--sudo-user=#{config.sudo_user}" if config.sudo_user
|
2013-09-07 13:17:43 +00:00
|
|
|
options << "#{self.get_verbosity_argument}" if config.verbose
|
2013-09-07 12:32:36 +00:00
|
|
|
options << "--ask-sudo-pass" if config.ask_sudo_pass
|
2013-09-02 22:30:49 +00:00
|
|
|
options << "--tags=#{as_list_argument(config.tags)}" if config.tags
|
|
|
|
options << "--skip-tags=#{as_list_argument(config.skip_tags)}" if config.skip_tags
|
|
|
|
options << "--limit=#{as_list_argument(config.limit)}" if config.limit
|
|
|
|
options << "--start-at-task=#{config.start_at_task}" if config.start_at_task
|
|
|
|
|
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 << {
|
2013-09-14 02:48:12 +00:00
|
|
|
:env => { "ANSIBLE_FORCE_COLOR" => "true" , "ANSIBLE_HOST_KEY_CHECKING" => "#{config.host_key_checking}" },
|
2013-08-29 18:55:58 +00:00
|
|
|
:notify => [:stdout, :stderr],
|
|
|
|
:workdir => @machine.env.root_path.to_s
|
2013-04-04 18:31:27 +00:00
|
|
|
}
|
2013-04-04 20:48:58 +00:00
|
|
|
|
2013-04-04 20:58:33 +00:00
|
|
|
begin
|
2013-07-30 12:25:15 +00:00
|
|
|
result = 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
|
|
|
|
2013-07-30 12:25:15 +00:00
|
|
|
raise Vagrant::Errors::AnsibleFailed if result.exit_code != 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
|
2013-08-28 23:52:13 +00:00
|
|
|
|
2013-09-07 12:32:36 +00:00
|
|
|
protected
|
|
|
|
|
|
|
|
# Auto-generate "safe" inventory file based on Vagrantfile,
|
|
|
|
# unless inventory_path is explicitly provided
|
2013-08-28 23:52:13 +00:00
|
|
|
def setup_inventory_file
|
2013-09-01 04:48:41 +00:00
|
|
|
return config.inventory_path if config.inventory_path
|
2013-08-28 23:52:13 +00:00
|
|
|
|
2013-07-07 06:47:37 +00:00
|
|
|
ssh = @machine.ssh_info
|
2013-08-28 23:54:44 +00:00
|
|
|
|
|
|
|
generated_inventory_file =
|
|
|
|
@machine.env.root_path.join("vagrant_ansible_inventory_#{machine.name}")
|
|
|
|
|
|
|
|
generated_inventory_file.open('w') do |file|
|
2013-07-07 06:47:37 +00:00
|
|
|
file.write("# Generated by Vagrant\n\n")
|
|
|
|
file.write("#{machine.name} ansible_ssh_host=#{ssh[:host]} ansible_ssh_port=#{ssh[:port]}\n")
|
|
|
|
end
|
2013-08-28 23:52:13 +00:00
|
|
|
|
2013-08-28 23:54:44 +00:00
|
|
|
return generated_inventory_file.to_s
|
2013-07-07 06:47:37 +00:00
|
|
|
end
|
2013-09-02 22:30:49 +00:00
|
|
|
|
2013-09-07 13:17:43 +00:00
|
|
|
def get_verbosity_argument
|
|
|
|
if config.verbose.to_s =~ /^v+$/
|
|
|
|
# Hopefully ansible-playbook accepts "silly" arguments like '-vvvvv', as '-vvv'
|
|
|
|
"-#{config.verbose}"
|
|
|
|
elsif config.verbose.to_s == 'extra'
|
|
|
|
'-vvv'
|
|
|
|
else
|
|
|
|
# fall back to default verbosity
|
|
|
|
'--verbose'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-06 19:17:45 +00:00
|
|
|
def as_list_argument(v)
|
|
|
|
v.kind_of?(Array) ? v.join(',') : v
|
|
|
|
end
|
2013-03-20 14:41:21 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|