Trouble getting wp-cli to work with Vagrant bedrock-ansible setup

G’day folks,

I’m giving the Bedrock stack a spin on my current project, and have almost got it working flawlessly! It’s great stuff!

My small problem relates to using wp-cli with the bedrock-ansible Vagrant stack. Specifically, I can’t run wp-cli from my host OS.

Here’s how it is:

  • the host OS is running OSX

  • the guest VM is running bedrock-ansible

  • the project repo is on the host OS, i.e. in /my/dir, and shared to the VM

  • if I try to run wp-cli from the host OS, it cannot find the correct env vars it needs:

    /my/dir$ wp db export

    Fatal error: Uncaught exception ‘RuntimeException’ with message ‘Required ENV vars missing: ‘DB_NAME’, ‘DB_USER’, ‘DB_PASSWORD’, ‘WP_HOME’, ‘WP_SITEURL’’ in /my/dirs/vendor/vlucas/phpdotenv/src/Dotenv.php:63

Since bedrock-ansible hosts these env vars outside the project, wp-cli can’t find them.

My question is: has anybody else found this problem, and have you found an elegant solution? Or, perhaps, am I missing something obvious?

The solution is to just run the WP-CLI commands on the VM box. You can run vagrant ssh, then cd into the project dir and run the commands.

Out of curiosity, why is this the case? Isn’t .env just a symlink to the guest machine’s shared .env? Seems like it should be available.

Also, doesn’t this mean we can’t use the Capistrano WP-CLI wrapper to sync DBs?

I’ve been using WP Migrate DB Pro to sync data, but it might be nice to eventually make the DB sync part of the cap deploy.