provisioners/ansible: Fix Group Management
- Support arbitrary depth of "groups of groups of ... groups" - Skip ':vars' suffix, but allow group names with ':' (yes, Ansible accepts this character) - Like for groups of machines, groups of groups can result "empty", but it is not an issue for Ansible. Recursive filter on the group tree is a bit hard to implement, and don't brind real added value at Vagrant level.
This commit is contained in:
parent
b723f0d43d
commit
c2663f5d30
|
@ -73,7 +73,7 @@ module VagrantPlugins
|
||||||
ssh = @machine.ssh_info
|
ssh = @machine.ssh_info
|
||||||
|
|
||||||
# Managed machines
|
# Managed machines
|
||||||
inventory_machines = []
|
inventory_machines = {}
|
||||||
|
|
||||||
generated_inventory_file =
|
generated_inventory_file =
|
||||||
@machine.env.root_path.join("vagrant_ansible_inventory")
|
@machine.env.root_path.join("vagrant_ansible_inventory")
|
||||||
|
@ -86,7 +86,7 @@ module VagrantPlugins
|
||||||
m = @machine.env.machine(*am)
|
m = @machine.env.machine(*am)
|
||||||
if !m.ssh_info.nil?
|
if !m.ssh_info.nil?
|
||||||
file.write("#{m.name} ansible_ssh_host=#{m.ssh_info[:host]} ansible_ssh_port=#{m.ssh_info[:port]}\n")
|
file.write("#{m.name} ansible_ssh_host=#{m.ssh_info[:host]} ansible_ssh_port=#{m.ssh_info[:port]}\n")
|
||||||
inventory_machines << m
|
inventory_machines[m.name] = m
|
||||||
else
|
else
|
||||||
@logger.error("Auto-generated inventory: Impossible to get SSH information for machine '#{m.name} (#{m.provider_name})'. This machine should be recreated.")
|
@logger.error("Auto-generated inventory: Impossible to get SSH information for machine '#{m.name} (#{m.provider_name})'. This machine should be recreated.")
|
||||||
# Let a note about this missing machine
|
# Let a note about this missing machine
|
||||||
|
@ -97,11 +97,12 @@ module VagrantPlugins
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Write out groups information. Only includes groups and
|
# Write out groups information.
|
||||||
# machines which have been defined, otherwise Ansible will
|
# All defined groups will be included, but only supported
|
||||||
# complain.
|
# machines and defined child groups will be included.
|
||||||
|
# Group variables are intentionally skipped.
|
||||||
groups_of_groups = {}
|
groups_of_groups = {}
|
||||||
included_groups = []
|
defined_groups = []
|
||||||
|
|
||||||
config.groups.each_pair do |gname, gmembers|
|
config.groups.each_pair do |gname, gmembers|
|
||||||
# Require that gmembers be an array
|
# Require that gmembers be an array
|
||||||
|
@ -110,22 +111,21 @@ module VagrantPlugins
|
||||||
|
|
||||||
if gname.end_with?(":children")
|
if gname.end_with?(":children")
|
||||||
groups_of_groups[gname] = gmembers
|
groups_of_groups[gname] = gmembers
|
||||||
elsif !gname.include?(':')
|
defined_groups << gname.sub(/:children$/, '')
|
||||||
# skip group variables [:vars] and any other ":" suffixes
|
elsif !gname.include?(':vars')
|
||||||
included_groups << gname
|
defined_groups << gname
|
||||||
file.write("\n[#{gname}]\n")
|
file.write("\n[#{gname}]\n")
|
||||||
gmembers.each do |gm|
|
gmembers.each do |gm|
|
||||||
file.write("#{gm}\n") if inventory_machines.map { |m| m.name }.include?(gm.to_sym)
|
file.write("#{gm}\n") if inventory_machines.include?(gm.to_sym)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defined_groups.uniq!
|
||||||
groups_of_groups.each_pair do |gname, gmembers|
|
groups_of_groups.each_pair do |gname, gmembers|
|
||||||
unless (included_groups & gmembers).empty?
|
file.write("\n[#{gname}]\n")
|
||||||
file.write("\n[#{gname}]\n")
|
gmembers.each do |gm|
|
||||||
gmembers.each do |gm|
|
file.write("#{gm}\n") if defined_groups.include?(gm)
|
||||||
file.write("#{gm}\n") if included_groups.include?(gm)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -37,7 +37,9 @@ machines. The generated inventory file is created adjacent to your Vagrantfile,
|
||||||
`vagrant_ansible_inventory`.
|
`vagrant_ansible_inventory`.
|
||||||
|
|
||||||
The `ansible.groups` option can be used to pass a hash of group
|
The `ansible.groups` option can be used to pass a hash of group
|
||||||
names and group members to be included in the generated inventory file. For example:
|
names and group members to be included in the generated inventory file. Group variables
|
||||||
|
are intentionally not supported, as this practice is not recommended.
|
||||||
|
For example:
|
||||||
|
|
||||||
```
|
```
|
||||||
ansible.groups = {
|
ansible.groups = {
|
||||||
|
@ -47,7 +49,7 @@ ansible.groups = {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Note that undefined machines and groups are not added to the inventory.
|
Note that unmanaged machines and undefined groups are not added to the inventory.
|
||||||
For example, `group3` in the above example would not be added to the inventory file.
|
For example, `group3` in the above example would not be added to the inventory file.
|
||||||
|
|
||||||
A generated inventory might look like:
|
A generated inventory might look like:
|
||||||
|
|
Loading…
Reference in New Issue