From a1ad1207bd80fc7267d1eb56a2bdedfdfcd427cf Mon Sep 17 00:00:00 2001 From: Collin Allen Date: Wed, 20 Mar 2013 07:41:21 -0700 Subject: [PATCH] Add ansible provisioner --- plugins/provisioners/ansible/config.rb | 46 +++++++++++++++++++++ plugins/provisioners/ansible/plugin.rb | 23 +++++++++++ plugins/provisioners/ansible/provisioner.rb | 28 +++++++++++++ templates/locales/en.yml | 3 ++ 4 files changed, 100 insertions(+) create mode 100644 plugins/provisioners/ansible/config.rb create mode 100644 plugins/provisioners/ansible/plugin.rb create mode 100644 plugins/provisioners/ansible/provisioner.rb diff --git a/plugins/provisioners/ansible/config.rb b/plugins/provisioners/ansible/config.rb new file mode 100644 index 000000000..de01835cd --- /dev/null +++ b/plugins/provisioners/ansible/config.rb @@ -0,0 +1,46 @@ +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 = [] + + if !playbook + errors << I18n.t("vagrant.provisioners.ansible.no_playbook") + end + + { "ansible provisioner" => errors } + end + end + end +end diff --git a/plugins/provisioners/ansible/plugin.rb b/plugins/provisioners/ansible/plugin.rb new file mode 100644 index 000000000..e18628ac6 --- /dev/null +++ b/plugins/provisioners/ansible/plugin.rb @@ -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_relative "config" + Config + end + + provisioner(:ansible) do + require_relative "provisioner" + Provisioner + end + end + end +end diff --git a/plugins/provisioners/ansible/provisioner.rb b/plugins/provisioners/ansible/provisioner.rb new file mode 100644 index 000000000..4a670cd6e --- /dev/null +++ b/plugins/provisioners/ansible/provisioner.rb @@ -0,0 +1,28 @@ +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 + + command = (%w(ansible-playbook) << options << config.playbook).flatten + + Vagrant::Util::SafeExec.exec(*command) + end + end + end +end diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 832fb2701..307da6ce8 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -1015,6 +1015,9 @@ 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." guest: base: