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 23, 2017
1 parent 5ad51ab commit 3ca2d19
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))
* 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))
Expand Down
6 changes: 3 additions & 3 deletions Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
17 changes: 17 additions & 0 deletions lib/trellis/vagrant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 3ca2d19

Please sign in to comment.