From 77b11a989ca1aaa02e60068c1754c052bc4e8031 Mon Sep 17 00:00:00 2001 From: Christian Henz Date: Tue, 1 Dec 2015 18:15:40 +0100 Subject: [PATCH] Implemented host_vars option. --- plugins/provisioners/ansible/config/base.rb | 3 +++ plugins/provisioners/ansible/provisioner/base.rb | 13 +++++++++++++ plugins/provisioners/ansible/provisioner/guest.rb | 2 ++ plugins/provisioners/ansible/provisioner/host.rb | 3 +++ 4 files changed, 21 insertions(+) diff --git a/plugins/provisioners/ansible/config/base.rb b/plugins/provisioners/ansible/config/base.rb index 2cefe4a89..14ae1a3e6 100644 --- a/plugins/provisioners/ansible/config/base.rb +++ b/plugins/provisioners/ansible/config/base.rb @@ -9,6 +9,7 @@ module VagrantPlugins attr_accessor :galaxy_role_file attr_accessor :galaxy_roles_path attr_accessor :galaxy_command + attr_accessor :host_vars attr_accessor :groups attr_accessor :inventory_path attr_accessor :limit @@ -27,6 +28,7 @@ module VagrantPlugins @galaxy_role_file = UNSET_VALUE @galaxy_roles_path = UNSET_VALUE @galaxy_command = UNSET_VALUE + @host_vars = UNSET_VALUE @groups = UNSET_VALUE @inventory_path = UNSET_VALUE @limit = UNSET_VALUE @@ -46,6 +48,7 @@ module VagrantPlugins @galaxy_role_file = nil if @galaxy_role_file == UNSET_VALUE @galaxy_roles_path = nil if @galaxy_roles_path == UNSET_VALUE @galaxy_command = GALAXY_COMMAND_DEFAULT if @galaxy_command == UNSET_VALUE + @host_vars = {} if @host_vars == UNSET_VALUE @groups = {} if @groups == UNSET_VALUE @inventory_path = nil if @inventory_path == UNSET_VALUE @limit = nil if @limit == UNSET_VALUE diff --git a/plugins/provisioners/ansible/provisioner/base.rb b/plugins/provisioners/ansible/provisioner/base.rb index bf577ab6e..51377a61f 100644 --- a/plugins/provisioners/ansible/provisioner/base.rb +++ b/plugins/provisioners/ansible/provisioner/base.rb @@ -70,6 +70,19 @@ module VagrantPlugins end end + def get_inventory_host_vars_string(machine_name) + vars = config.host_vars[machine_name] + s = nil + if vars.is_a?(Hash) + s = vars.each.collect{ |k, v| "#{k}=#{v}" }.join(" ") + elsif vars.is_a?(Array) + s = vars.join(" ") + elsif vars.is_a?(String) + s = vars + end + if s and !s.empty? then s else nil end + end + def generate_inventory inventory = "# Generated by Vagrant\n\n" diff --git a/plugins/provisioners/ansible/provisioner/guest.rb b/plugins/provisioners/ansible/provisioner/guest.rb index a82bf7562..6f0b43f95 100644 --- a/plugins/provisioners/ansible/provisioner/guest.rb +++ b/plugins/provisioners/ansible/provisioner/guest.rb @@ -128,6 +128,8 @@ module VagrantPlugins else machines += "#{machine_name}\n" end + host_vars = get_inventory_host_vars_string(machine_name) + machines.sub!(/\n$/, " #{host_vars}\n") if host_vars end end diff --git a/plugins/provisioners/ansible/provisioner/host.rb b/plugins/provisioners/ansible/provisioner/host.rb index 54928bdfc..dcce98fb8 100644 --- a/plugins/provisioners/ansible/provisioner/host.rb +++ b/plugins/provisioners/ansible/provisioner/host.rb @@ -151,12 +151,15 @@ module VagrantPlugins # Call only once the SSH and WinRM info computation # Note that machines configured with WinRM communicator, also have a "partial" ssh_info. m_ssh_info = m.ssh_info + host_vars = get_inventory_host_vars_string(m.name) if m.config.vm.communicator == :winrm m_winrm_net_info = CommunicatorWinRM::Helper.winrm_info(m) # can raise a WinRMNotReady exception... machines += get_inventory_winrm_machine(m, m_winrm_net_info) + machines.sub!(/\n$/, " #{host_vars}\n") if host_vars @inventory_machines[m.name] = m elsif !m_ssh_info.nil? machines += get_inventory_ssh_machine(m, m_ssh_info) + machines.sub!(/\n$/, " #{host_vars}\n") if host_vars @inventory_machines[m.name] = m else @logger.error("Auto-generated inventory: Impossible to get SSH information for machine '#{m.name} (#{m.provider_name})'. This machine should be recreated.")