diff --git a/plugins/provisioners/ansible/config.rb b/plugins/provisioners/ansible/config.rb index 818e11c7a..6232d8ece 100644 --- a/plugins/provisioners/ansible/config.rb +++ b/plugins/provisioners/ansible/config.rb @@ -12,6 +12,7 @@ module VagrantPlugins attr_accessor :tags attr_accessor :skip_tags attr_accessor :start_at_task + attr_accessor :groups attr_accessor :host_key_checking # Joker attribute, used to pass unsupported arguments to ansible anyway @@ -30,6 +31,7 @@ module VagrantPlugins @skip_tags = UNSET_VALUE @start_at_task = UNSET_VALUE @raw_arguments = UNSET_VALUE + @groups = UNSET_VALUE @host_key_checking = "true" end @@ -46,6 +48,7 @@ module VagrantPlugins @skip_tags = nil if @skip_tags == UNSET_VALUE @start_at_task = nil if @start_at_task == UNSET_VALUE @raw_arguments = nil if @raw_arguments == UNSET_VALUE + @groups = {} if @groups == UNSET_VALUE @host_key_checking = nil if @host_key_checking == UNSET_VALUE end diff --git a/plugins/provisioners/ansible/provisioner.rb b/plugins/provisioners/ansible/provisioner.rb index b1d6a866b..ce8cdf741 100644 --- a/plugins/provisioners/ansible/provisioner.rb +++ b/plugins/provisioners/ansible/provisioner.rb @@ -69,6 +69,31 @@ module VagrantPlugins generated_inventory_file.open('w') do |file| file.write("# Generated by Vagrant\n\n") file.write("#{machine.name} ansible_ssh_host=#{ssh[:host]} ansible_ssh_port=#{ssh[:port]}\n") + + # Write out groups information. Only include current + # machine and its groups to avoid Ansible errors on + # provisioning. + groups_of_groups = {} + included_groups = [] + + config.groups.each_pair do |gname, gmembers| + if gname.end_with?(":children") + groups_of_groups[gname] = gmembers + elsif gmembers.include?("#{machine.name}") + included_groups << gname + file.write("\n[#{gname}]\n") + file.write("#{machine.name}\n") + end + end + + groups_of_groups.each_pair do |gname, gmembers| + unless (included_groups & gmembers).empty? + file.write("\n[#{gname}]\n") + gmembers.each do |gm| + file.write("#{gm}\n") if included_groups.include?(gm) + end + end + end end return generated_inventory_file.to_s