Merge ansible provisioner [GH-1465]
This commit is contained in:
commit
1c2a46ba72
|
@ -0,0 +1,65 @@
|
|||
module VagrantPlugins
|
||||
module Ansible
|
||||
class Config < Vagrant.plugin("2", :config)
|
||||
attr_accessor :playbook
|
||||
attr_accessor :extra_vars
|
||||
attr_accessor :inventory_file
|
||||
attr_accessor :ask_sudo_pass
|
||||
attr_accessor :limit
|
||||
attr_accessor :sudo
|
||||
attr_accessor :sudo_user
|
||||
attr_accessor :verbose
|
||||
|
||||
def initialize
|
||||
@playbook = UNSET_VALUE
|
||||
@extra_vars = UNSET_VALUE
|
||||
@inventory_file = UNSET_VALUE
|
||||
@ask_sudo_pass = UNSET_VALUE
|
||||
@limit = UNSET_VALUE
|
||||
@sudo = UNSET_VALUE
|
||||
@sudo_user = UNSET_VALUE
|
||||
@verbose = UNSET_VALUE
|
||||
end
|
||||
|
||||
def finalize!
|
||||
@playbook = nil if @playbook == UNSET_VALUE
|
||||
@extra_vars = nil if @extra_vars == UNSET_VALUE
|
||||
@inventory_file = nil if @inventory_file == UNSET_VALUE
|
||||
@ask_sudo_pass = nil if @ask_sudo_pass == UNSET_VALUE
|
||||
@limit = nil if @limit == UNSET_VALUE
|
||||
@sudo = nil if @sudo == UNSET_VALUE
|
||||
@sudo_user = nil if @sudo_user == UNSET_VALUE
|
||||
@verbose = nil if @verbose == UNSET_VALUE
|
||||
end
|
||||
|
||||
def validate(machine)
|
||||
errors = []
|
||||
|
||||
# Validate that a playbook path was provided
|
||||
if !playbook
|
||||
errors << I18n.t("vagrant.provisioners.ansible.no_playbook")
|
||||
end
|
||||
|
||||
# Validate the existence of said playbook on the host
|
||||
if playbook
|
||||
expanded_path = Pathname.new(playbook).expand_path(machine.env.root_path)
|
||||
if !expanded_path.file?
|
||||
errors << I18n.t("vagrant.provisioners.ansible.playbook_path_invalid",
|
||||
:path => expanded_path)
|
||||
end
|
||||
end
|
||||
|
||||
# Validate the existence of the inventory_file, if specified
|
||||
if inventory_file
|
||||
expanded_path = Pathname.new(inventory_file).expand_path(machine.env.root_path)
|
||||
if !expanded_path.file?
|
||||
errors << I18n.t("vagrant.provisioners.ansible.inventory_file_path_invalid",
|
||||
:path => expanded_path)
|
||||
end
|
||||
end
|
||||
|
||||
{ "ansible provisioner" => errors }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,23 @@
|
|||
require "vagrant"
|
||||
|
||||
module VagrantPlugins
|
||||
module Ansible
|
||||
class Plugin < Vagrant.plugin("2")
|
||||
name "ansible"
|
||||
description <<-DESC
|
||||
Provides support for provisioning your virtual machines with
|
||||
Ansible playbooks.
|
||||
DESC
|
||||
|
||||
config(:ansible, :provisioner) do
|
||||
require File.expand_path("../config", __FILE__)
|
||||
Config
|
||||
end
|
||||
|
||||
provisioner(:ansible) do
|
||||
require File.expand_path("../provisioner", __FILE__)
|
||||
Provisioner
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,37 @@
|
|||
module VagrantPlugins
|
||||
module Ansible
|
||||
class Provisioner < Vagrant.plugin("2", :provisioner)
|
||||
def provision
|
||||
ssh = @machine.ssh_info
|
||||
|
||||
options = %W[--private-key=#{ssh[:private_key_path]} --user=#{ssh[:username]}]
|
||||
options << "--extra-vars=\"#{config.extra_vars}\"" if config.extra_vars
|
||||
options << "--inventory-file=#{config.inventory_file}" if config.inventory_file
|
||||
options << "--ask-sudo-pass" if config.ask_sudo_pass
|
||||
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
|
||||
options << "--sudo" if config.sudo
|
||||
options << "--sudo-user=#{config.sudo_user}" if config.sudo_user
|
||||
options << "--verbose" if config.verbose
|
||||
|
||||
# Assemble the full ansible-playbook command
|
||||
command = (%w(ansible-playbook) << options << config.playbook).flatten
|
||||
|
||||
# Write stdout and stderr data, since it's the regular Ansible output
|
||||
command << {
|
||||
:env => { "ANSIBLE_FORCE_COLOR" => "true" },
|
||||
:notify => [:stdout, :stderr]
|
||||
}
|
||||
Vagrant::Util::Subprocess.execute(*command) do |type, data|
|
||||
puts "#{data}" if type == :stdout || type == :stderr
|
||||
yield type, data if block_given?
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1038,3 +1038,8 @@ en:
|
|||
no_path_or_inline: "One of `path` or `inline` must be set."
|
||||
path_invalid: "`path` for shell provisioner does not exist on the host system: %{path}"
|
||||
upload_path_not_set: "`upload_path` must be set for the shell provisioner."
|
||||
|
||||
ansible:
|
||||
no_playbook: "`playbook` must be set for the Ansible provisioner."
|
||||
playbook_path_invalid: "`playbook` for the Ansible provisioner does not exist on the host system: %{path}"
|
||||
inventory_file_path_invalid: "`inventory_file` for the Ansible provisioner does not exist on the host system: %{path}"
|
||||
|
|
Loading…
Reference in New Issue