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 diff --git a/website/docs/source/v2/provisioning/ansible.html.md b/website/docs/source/v2/provisioning/ansible.html.md index 619b8e71a..20f48fa0e 100644 --- a/website/docs/source/v2/provisioning/ansible.html.md +++ b/website/docs/source/v2/provisioning/ansible.html.md @@ -146,4 +146,13 @@ by the sudo command. * `ansible.raw_arguments` is an *unsafe wildcard* string that can be used to take advantage of `ansible-playbook` arguments that are not (yet) supported by this Vagrant provisioner plugin. This can be very useful when integrating with bleeding edge Ansible versions. Following precedence rules apply: * Any supported options (described above) will override conflicting `raw_arguments` value (e.g. `--tags` or `--start-at-task`) * Vagrant default user authentication can be overridden via `raw_arguments` (with custom values for `--user` and `--private-key`) +* `ansible.groups` can be used to pass a hash of group names and group members to be included in the generated inventory file. For example: +``` +ansible.groups = { + "group1" => ["machine1"], + "group2" => ["machine2", "machine3"], + "all_groups:children" => ["group1", "group2"] +} +``` +Note that only the current machine and its related groups will be added to the inventory file. * `ansible.host_key_checking` can be set to `false` which will disable host key checking and prevent `"FAILED: (25, 'Inappropriate ioctl for device')"` errors from being reported during provisioner runs. The default value is `true`, which matches the default behavior of Ansible 1.2.1 and later.