From 3ca2d19cb7fa7da68b6ff22a045b7924da40d12a Mon Sep 17 00:00:00 2001 From: Scott Walkinshaw Date: Sat, 8 Apr 2017 17:40:10 -0400 Subject: [PATCH] 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. --- CHANGELOG.md | 1 + Vagrantfile | 6 +++--- lib/trellis/vagrant.rb | 17 +++++++++++++++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6aec9aec7b..6bf3595e76 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,5 @@ ### HEAD +* Add ansible_local support for non-Windows ([#824](https://github.com/roots/trellis/pull/824)) * Option to install WP-CLI packages ([#837](https://github.com/roots/trellis/pull/837)) * Update WP-CLI to 1.2.1 ([#838](https://github.com/roots/trellis/pull/838)) * Auto-install Vagrant plugins ([#829](https://github.com/roots/trellis/pull/829)) diff --git a/Vagrantfile b/Vagrantfile index 9e8fc6c7ed..c8cbda1e70 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -91,11 +91,11 @@ Vagrant.configure('2') do |config| end end - provisioner = Vagrant::Util::Platform.windows? ? :ansible_local : :ansible - provisioning_path = Vagrant::Util::Platform.windows? ? ANSIBLE_PATH_ON_VM : ANSIBLE_PATH + provisioner = local_provisioning? ? :ansible_local : :ansible + provisioning_path = local_provisioning? ? ANSIBLE_PATH_ON_VM : ANSIBLE_PATH config.vm.provision provisioner do |ansible| - if Vagrant::Util::Platform.windows? + if local_provisioning? ansible.install_mode = 'pip' ansible.provisioning_path = provisioning_path ansible.version = vconfig.fetch('vagrant_ansible_version') diff --git a/lib/trellis/vagrant.rb b/lib/trellis/vagrant.rb index d25b44fe59..bc036b8afd 100644 --- a/lib/trellis/vagrant.rb +++ b/lib/trellis/vagrant.rb @@ -62,6 +62,10 @@ def load_wordpress_sites wordpress_sites end +def local_provisioning? + @local_provisioning ||= Vagrant::Util::Platform.windows? || !which('ansible-playbook') || ENV['FORCE_ANSIBLE_LOCAL'] +end + def local_site_path(site) File.expand_path(site['local_path'], ANSIBLE_PATH) end @@ -90,3 +94,16 @@ def post_up_message def remote_site_path(site_name, site) "/srv/www/#{site_name}/#{site['current_path'] || 'current'}" end + +def which(cmd) + exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : [''] + + paths = ENV['PATH'].split(File::PATH_SEPARATOR).flat_map do |path| + exts.map { |ext| File.join(path, "#{cmd}#{ext}") } + end + + paths.any? do |path| + next unless File.executable?(path) && !File.directory?(path) + system("#{path} --help", %i(out err) => File::NULL) + end +end