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 Apr 22, 2017
1 parent 1f165ea commit a282136
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 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))
* 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
41 changes: 28 additions & 13 deletions Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ Vagrant.configure('2') do |config|
wordpress_sites.each_pair do |name, site|
config.vm.synced_folder local_site_path(site), remote_site_path(name, site), owner: 'vagrant', group: 'www-data', mount_options: ['dmode=776', 'fmode=775']
end

config.vm.synced_folder ANSIBLE_PATH, ANSIBLE_PATH_ON_VM, mount_options: ['dmode=755', 'fmode=644']
config.vm.synced_folder File.join(ANSIBLE_PATH, 'bin'), bin_path, mount_options: ['dmode=755', 'fmode=755']
else
Expand All @@ -88,35 +89,33 @@ Vagrant.configure('2') do |config|
config.vm.synced_folder local_site_path(site), nfs_path(name), type: 'nfs'
config.bindfs.bind_folder nfs_path(name), remote_site_path(name, site), u: 'vagrant', g: 'www-data', o: 'nonempty'
end

config.vm.synced_folder ANSIBLE_PATH, '/ansible-nfs', type: 'nfs'
config.bindfs.bind_folder '/ansible-nfs', ANSIBLE_PATH_ON_VM, o: 'nonempty', p: '0644,a+D'
config.bindfs.bind_folder bin_path, bin_path, perms: '0755'
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 = '2.2.0'
ansible.version = '2.2.2'
end

ansible.playbook = File.join(provisioning_path, 'dev.yml')
unless ENV['SKIP_GALAXY']
ansible.galaxy_role_file = File.join(provisioning_path, 'requirements.yml')
end
ansible.galaxy_role_file = File.join(provisioning_path, 'requirements.yml') unless ENV['SKIP_GALAXY']
ansible.galaxy_roles_path = File.join(provisioning_path, 'vendor/roles')

ansible.groups = {
'web' => ['default'],
'development' => ['default']
}

if tags = ENV['ANSIBLE_TAGS']
ansible.tags = tags
end
ansible.tags = ENV['ANSIBLE_TAGS']

ansible.extra_vars = {'vagrant_version' => Vagrant::VERSION}
if vars = ENV['ANSIBLE_VARS']
Expand Down Expand Up @@ -155,6 +154,10 @@ Vagrant.configure('2') do |config|

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 All @@ -171,12 +174,24 @@ def post_up_message
msg = 'Your Trellis Vagrant box is ready to use!'
msg << "\n* Composer and WP-CLI commands need to be run on the virtual machine."
msg << "\n* You can SSH into the machine with `vagrant ssh`."
msg << "\n* Then navigate to your WordPress sites at `/srv/www`"
msg << "\n or to your Trellis files at `#{ANSIBLE_PATH_ON_VM}`."

msg << "\n* Your WordPress sites are found in `/srv/www`."
msg << "\n* Windows users: Trellis files are in `#{ANSIBLE_PATH_ON_VM}`." if Vagrant::Util::Platform.windows?
msg
end

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)
end
end

0 comments on commit a282136

Please sign in to comment.