Skip to content

Commit af14792

Browse files
committed
Add ansible_local support for non-Windows
Previously only Windows would use Vagrant's `ansible_local` provisioner. But it's a much easier experience to get started in development since it skips the Ansible requirement. This automatically uses the `ansible_local` provisioner if `ansible-playbook` does not exist on the host machine.
1 parent c7ad193 commit af14792

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

Vagrantfile

+23-4
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@ Vagrant.configure('2') do |config|
7878
wordpress_sites.each_pair do |name, site|
7979
config.vm.synced_folder local_site_path(site), remote_site_path(name, site), owner: 'vagrant', group: 'www-data', mount_options: ['dmode=776', 'fmode=775']
8080
end
81-
config.vm.synced_folder ANSIBLE_PATH, ANSIBLE_PATH_ON_VM, mount_options: ['dmode=755', 'fmode=644']
82-
config.vm.synced_folder File.join(ANSIBLE_PATH, 'bin'), bin_path, mount_options: ['dmode=755', 'fmode=755']
8381
else
8482
if !Vagrant.has_plugin? 'vagrant-bindfs'
8583
fail_with_message "vagrant-bindfs missing, please install the plugin with this command:\nvagrant plugin install vagrant-bindfs"
@@ -94,8 +92,12 @@ Vagrant.configure('2') do |config|
9492
end
9593
end
9694

97-
provisioner = Vagrant::Util::Platform.windows? ? :ansible_local : :ansible
98-
provisioning_path = Vagrant::Util::Platform.windows? ? ANSIBLE_PATH_ON_VM : ANSIBLE_PATH
95+
config.vm.synced_folder ANSIBLE_PATH, ANSIBLE_PATH_ON_VM, mount_options: ['dmode=755', 'fmode=644']
96+
config.vm.synced_folder File.join(ANSIBLE_PATH, 'bin'), bin_path, mount_options: ['dmode=755', 'fmode=755']
97+
98+
provisioner = local_provisioning? ? :ansible_local : :ansible
99+
provisioning_path = local_provisioning? ? ANSIBLE_PATH_ON_VM : ANSIBLE_PATH
100+
99101
config.vm.provision provisioner do |ansible|
100102
if Vagrant::Util::Platform.windows?
101103
ansible.install_mode = 'pip'
@@ -155,6 +157,10 @@ Vagrant.configure('2') do |config|
155157

156158
end
157159

160+
def local_provisioning?
161+
Vagrant::Util::Platform.windows? || !which('ansible-playbook') || ENV['FORCE_ANSIBLE_LOCAL']
162+
end
163+
158164
def local_site_path(site)
159165
File.expand_path(site['local_path'], ANSIBLE_PATH)
160166
end
@@ -180,3 +186,16 @@ end
180186
def remote_site_path(site_name, site)
181187
"/srv/www/#{site_name}/#{site['current_path'] || 'current'}"
182188
end
189+
190+
def which(cmd)
191+
exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
192+
193+
ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
194+
exts.each do |ext|
195+
exe = File.join(path, "#{cmd}#{ext}")
196+
return exe if File.executable?(exe) && !File.directory?(exe)
197+
end
198+
end
199+
200+
nil
201+
end

0 commit comments

Comments
 (0)