diff --git a/plugins/provisioners/ansible/provisioner/base.rb b/plugins/provisioners/ansible/provisioner/base.rb index 6671f0218..b61426037 100644 --- a/plugins/provisioners/ansible/provisioner/base.rb +++ b/plugins/provisioners/ansible/provisioner/base.rb @@ -112,6 +112,13 @@ module VagrantPlugins group_vars = {} inventory_groups = "" + # Verify if host range patterns exist and warn + if config.groups.any? do |gm| + gm.to_s[/(?:\[[a-z]:[a-z]\]|\[[0-9]+?:[0-9]+?\])/] + end + @machine.ui.warn(I18n.t("vagrant.provisioners.ansible.ansible_host_pattern_detected")) + end + config.groups.each_pair do |gname, gmembers| if gname.is_a?(Symbol) gname = gname.to_s @@ -134,6 +141,12 @@ module VagrantPlugins defined_groups << gname inventory_groups += "\n[#{gname}]\n" gmembers.each do |gm| + # TODO : Expand and validate host range patterns + # against @inventory_machines list before adding them + # otherwise abort with an error message + if gm[/(?:\[[a-z]:[a-z]\]|\[[0-9]+?:[0-9]+?\])/] + inventory_groups += "#{gm}\n" + end inventory_groups += "#{gm}\n" if @inventory_machines.include?(gm.to_sym) end end diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 2720e9cf4..6fbd4ee79 100755 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -2065,6 +2065,12 @@ en: interactive_not_elevated: "To be interactive, it must also be privileged." ansible: + ansible_host_pattern_detected: |- + Vagrant has detected a host range pattern in the ansible.groups option. + Vagrant doesn't fully check the validity of these parameters! + + Please check http://docs.ansible.com/ansible/intro_inventory.html#hosts-and-groups + for more information. cannot_detect: |- Vagrant does not support detecting whether Ansible is installed for the guest OS running in the machine. Vagrant will assume it is diff --git a/website/docs/source/v2/provisioning/ansible_intro.html.md b/website/docs/source/v2/provisioning/ansible_intro.html.md index daa21aeb4..6871030d3 100644 --- a/website/docs/source/v2/provisioning/ansible_intro.html.md +++ b/website/docs/source/v2/provisioning/ansible_intro.html.md @@ -156,6 +156,8 @@ Vagrant.configure(2) do |config| ansible.groups = { "group1" => ["machine1"], "group2" => ["machine2"], + "group3" => ["machine[01:50]"], + "group4" => ["machine-[a:d]"], "all_groups:children" => ["group1", "group2"], "group1:vars" => {"variable1" => 9, "variable2" => "example"} @@ -171,6 +173,14 @@ Vagrant would generate an inventory file that might look like: machine1 ansible_ssh_host=127.0.0.1 ansible_ssh_port=2200 ansible_ssh_user='vagrant' ansible_ssh_private_key_file='/home/.../.vagrant/machines/machine1/virtualbox/private_key' machine2 ansible_ssh_host=127.0.0.1 ansible_ssh_port=2222 ansible_ssh_user='vagrant' ansible_ssh_private_key_file='/home/.../.vagrant/machines/machine2/virtualbox/private_key' +machine01 ansible_ssh_host=127.0.0.1 ansible_ssh_port=2223 ansible_ssh_user='vagrant' ansible_ssh_private_key_file='/home/.../.vagrant/machines/machine01/virtualbox/private_key' +machine02 ansible_ssh_host=127.0.0.1 ansible_ssh_port=2224 ansible_ssh_user='vagrant' ansible_ssh_private_key_file='/home/.../.vagrant/machines/machine02/virtualbox/private_key' +machine03 ansible_ssh_host=127.0.0.1 ansible_ssh_port=2225 ansible_ssh_user='vagrant' ansible_ssh_private_key_file='/home/.../.vagrant/machines/machine03/virtualbox/private_key' +machine04 ansible_ssh_host=127.0.0.1 ansible_ssh_port=2226 ansible_ssh_user='vagrant' ansible_ssh_private_key_file='/home/.../.vagrant/machines/machine04/virtualbox/private_key' +machine-a ansible_ssh_host=127.0.0.1 ansible_ssh_port=2227 ansible_ssh_user='vagrant' ansible_ssh_private_key_file='/home/.../.vagrant/machines/machine-a/virtualbox/private_key' +machine-b ansible_ssh_host=127.0.0.1 ansible_ssh_port=2228 ansible_ssh_user='vagrant' ansible_ssh_private_key_file='/home/.../.vagrant/machines/machine-b/virtualbox/private_key' +machine-c ansible_ssh_host=127.0.0.1 ansible_ssh_port=2229 ansible_ssh_user='vagrant' ansible_ssh_private_key_file='/home/.../.vagrant/machines/machine-c/virtualbox/private_key' +machine-d ansible_ssh_host=127.0.0.1 ansible_ssh_port=2230 ansible_ssh_user='vagrant' ansible_ssh_private_key_file='/home/.../.vagrant/machines/machine-d/virtualbox/private_key' [group1] machine1 @@ -178,6 +188,12 @@ machine1 [group2] machine2 +[group3] +machine[01:50] + +[group4] +machine-[a:d] + [all_groups:children] group1 group2 @@ -192,14 +208,17 @@ variable2=example - Prior to Vagrant 1.7.3, the `ansible_ssh_private_key_file` variable was not set in generated inventory, but passed as command line argument to `ansible-playbook` command. - The generation of group variables blocks (e.g. `[group1:vars]`) is only possible since Vagrant 1.8.0. Note however that setting variables directly in the inventory is not the [preferred practice in Ansible](http://docs.ansible.com/intro_inventory.html#splitting-out-host-and-group-specific-data). If possible, group (or host) variables should be set in `YAML` files stored in the `group_vars/` or `host_vars/` directories in the playbook (or inventory) directory instead. - Unmanaged machines and undefined groups are not added to the inventory, to avoid useless Ansible errors (e.g. *unreachable host* or *undefined child group*) + - [Host range patterns (numeric and alphabetic ranges)](http://docs.ansible.com/ansible/intro_inventory.html#hosts-and-groups) will not be validated by vagrant. Hosts will be added as group members to the inventory anyway, this might lead to errors in Ansible (e.q *unreachable host*). -For example, `machine3` and `group3` in the example below would not be added to the generated inventory file: +For example, `machine3`, `group3` and `group1:vars` in the example below would not be added to the generated inventory file, +`machine-[a:d]` and `machine[01:50]` would be (even thought there are no hosts named machine50): ``` ansible.groups = { - "group1" => ["machine1"], - "group2" => ["machine2", "machine3"], - "all_groups:children" => ["group1", "group2", "group3"] + "group1" => ["machine1","machine[01:50]"], + "group2" => ["machine2", "machine3", "machine-[a:d]"], + "all_groups:children" => ["group1", "group2", "group3"], + "group1:vars" => { "variable1" => 9, "variable2" => "example" } } ```