home
Hero image for: Executing Drupal Console commands on a project with DrupalVM using vagrant-exec plugin

Executing Drupal Console commands on a project with DrupalVM using vagrant-exec plugin

By Jesus Manuel Olivas ● Head of Products | March 13th, 2017

I am currently working on a project where we are using DrupalVM as composer dependency.

Since ssh into the VM machine to execute commands could be a little cumbersome, and providing a site configuration file to take advantage of the --target option to execute commands remotely was not an option, because the generate commands can not be executed remotely if you want to use the interactive mode.

My options got narrowed to execute vagrant ssh and run commands within the VM (which I already mentioned I am not a big fan) or take advantage of the vagrant-exec plugin.

I decided to give a try to the vagrant-exec plugin and this what you need to do to use it in your project when DrupalVM is required via composer.

Install plugin

Execute plugin install command.

vagrant plugin install vagrant-exec
view raw install.sh hosted with ❤ by GitHub

Plugin configuration

Create or update your config/Vagrantfile.local file with the following configuration:

if Vagrant.has_plugin?('vagrant-exec')
config.exec.commands '*', directory: '/var/www/drupal'
end
view raw Vagrantfile.local hosted with ❤ by GitHub

Execute command using plugin

vagrant exec bin/drupal generate:module
view raw generate-module.sh hosted with ❤ by GitHub

After this easy steps you can now execute DrupalConsole or any other shell commands against the VM without the need to shh into the machine.