Skip to content

Commit 94e3a8e

Browse files
committed
Add a multi_ansible provisioner
A box may be configured with `multi_ansible`, which specifies an ordered list of ansible provisioner names, at least one of which must be the standard name 'ansible'. Forklift will expect to find a key corresponding to each provisioner name within the top level box configuration. Each provisioner may be configured with any of the same options supported by the base ansible provisioner; you may also pass `reuse_vars: true` as a shorthand to simply copy all variables from the base 'ansible' named provisioner. For example, example-box: multi_ansible: - 'bootstrap' - 'ansible' - 'post' boostrap: reuse_vars: true config_file: ansible_bootstrap.cfg playbook: - 'playbooks/bootstrap.yml' ansible: variables: var1: "value1" playbook: - 'playbooks/main.yml' post: variables: var1: "value2" playbook: - 'user_playbooks/editor.yml' - 'user_playbooks/dotfiles.yml' Forklift will run the Ansible provisioners in stages, in the order defined by `multi_ansible`. Files named ansible_*.cfg are added to .gitignore so that the user may define additional ansible config files for their custom provisioner stages.
1 parent 2a9f80c commit 94e3a8e

2 files changed

Lines changed: 22 additions & 4 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ playbooks/galaxy_collections
1717
inventories/local_*
1818
user_devel_env_files/
1919
.cache
20+
ansible_*.cfg
2021

2122
# Vagrant
2223
.vagrant

vagrant/lib/forklift/box_distributor.rb

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def define_vm(config, box = {})
9292

9393
networks = configure_networks(box.fetch('networks', []))
9494
configure_shell(machine, box)
95-
configure_ansible(machine, box['ansible'], box['name'])
95+
configure_multi_ansible(machine, box)
9696
configure_providers(machine, box, networks)
9797
configure_synced_folders(machine, box)
9898
configure_private_network(machine, box)
@@ -174,7 +174,7 @@ def configure_networks(networks)
174174
end
175175
end
176176

177-
def configure_ansible(machine, ansible, box_name)
177+
def configure_ansible(machine, ansible, box_name, extra_vars, provisioner_name)
178178
return unless ansible
179179

180180
if ansible.key?('group') && !ansible['group'].nil?
@@ -189,9 +189,9 @@ def configure_ansible(machine, ansible, box_name)
189189
return unless (playbooks = ansible['playbook'])
190190

191191
[playbooks].flatten.each_with_index do |playbook, index|
192-
machine.vm.provision("main#{index}", type: 'ansible') do |ansible_provisioner|
192+
machine.vm.provision("#{provisioner_name}-#{index}", type: 'ansible', preserve_order: true) do |ansible_provisioner|
193193
ansible_provisioner.playbook = playbook
194-
ansible_provisioner.extra_vars = ansible['variables']
194+
ansible_provisioner.extra_vars = extra_vars
195195
ansible_provisioner.groups = @ansible_groups
196196
ansible_provisioner.verbose = ansible['verbose'] || false
197197
%w[config_file galaxy_role_file inventory_path].each do |key|
@@ -203,6 +203,23 @@ def configure_ansible(machine, ansible, box_name)
203203
end
204204
end
205205

206+
def configure_multi_ansible(machine, box)
207+
ansible_provisioner_names = box.fetch('multi_ansible', ['ansible'])
208+
return unless ansible_provisioner_names.include?('ansible')
209+
210+
primary_ansible = box['ansible']
211+
return unless primary_ansible
212+
213+
primary_ansible_vars = primary_ansible['variables']
214+
box_name = box['name']
215+
216+
ansible_provisioner_names.each do |provisioner_name|
217+
ansible = box[provisioner_name]
218+
extra_vars = ansible.fetch('reuse_vars', false) ? primary_ansible_vars : ansible['variables']
219+
configure_ansible(machine, ansible, box_name, extra_vars, provisioner_name)
220+
end
221+
end
222+
206223
def configure_shell(machine, box)
207224
return unless box.key?('shell') && !box['shell'].nil?
208225

0 commit comments

Comments
 (0)