Skip to content

Commit

Permalink
Add ansible_local support for non-Windows
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
swalkinshaw committed Jun 4, 2017
1 parent 560b523 commit 15b98f1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
### HEAD
* Add ansible_local support for non-Windows ([#824](https://github.com/roots/trellis/pull/824))
* Add Vagrant config ([#828](https://github.com/roots/trellis/pull/828))
* Ansible 2.3 compatibility ([#813](https://github.com/roots/trellis/pull/813))
* Remove potentially dangerous `db_import` option ([#825](https://github.com/roots/trellis/pull/825))
Expand Down
6 changes: 3 additions & 3 deletions Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,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')
Expand Down
17 changes: 17 additions & 0 deletions lib/trellis/vagrant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,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
Expand Down Expand Up @@ -62,3 +66,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}", %i(out err) => File::NULL).nil?
end
end

0 comments on commit 15b98f1

Please sign in to comment.