Multiple sites on one Vagrant box with bedrock-ansible

Hi,

I’d like to run several sites on one VM provisioned with bedrock-ansible. I’ve read this thread:

where @emaildano was trying to achieve the same set-up. I can see there is a number of hints on how this can be achieved, but I thought maybe this process is now simplified in the latest version of bedrock-ansible ?

I noticed that the latest Vagrantfile is now designed so that it iterates through wordpress.sites and syncronizes folders for each site. It made me think that the previous thread might be now outdated.

I also found @swalkinshaw’s gist on github:

https://gist.github.com/swalkinshaw/12ab5364a41eacfee208

that is titled “Multiple sites on one box with bedrock-ansible”.

However, I’m not sure where similar configuration file (sites.yml) might be added to bedrock-ansible to enable running multiple sites on one VM.

I will appreciate your help.

1 Like

You’re correct that the other thread is outdated. And also that it’s now way easier to do this since Vagrantfile just reads wordpress_sites.

That Gist of sites.yml has a confusing filename I guess. That’s just what you put in group_vars/development.

Basically wordpress_sites is an array. So if you want multiple sites just add on another element. That should be all that’s needed.

1 Like

It’s great that the process has been simplified so much.

However, I’ve added another site to group_vars/development, reloaded Vagrant with the --provision flag, saw tasks passing successfully until it came to

TASK: [wordpress-sites | WP installed?] and
TASK: [wordpress-sites | Install WP]

where I received an error message saying:

stderr: Error: Can’t select database

I inspected .env in the current folder on my Vagrant machine and it contains correct database information. However, .env on my local drive is totally empty. This might be the problem because these files are supposed to be shared, aren’t they?

Also, just to clarify, I assume that do not need to manually create a database for my second site.

What might be the problem?

I think you might also need to run vagrant reload. Vagrant needs to set up the other synced folder and I don’t think that happens just running vagrant provision.

That’s correct, if you’re adding another synced folder the box needs to be reloaded first.

I actually run vagrant reload --provision at my first attempt. Anyway, I managed to get the database working be reloading vagrant yet again.

Unfortunately, another obstacle has come up. I have two sites added to my group_vars/development file. It appears that the second defined database overrides the first one so, in consequence, I end up with two wordpress installs, but only with one database - the one that was defined last.

To be clearer, my configuration of databases look like this:

First site:

env:
  wp_home: http://tarasiewicz.local
  wp_siteurl: http://tarasiewicz.local/wp
  wp_env: development
  db_name: tarasiewicz_local
  db_user: luqo33
  db_password: bonzo86

Second site:

env:
  wp_home: http://cpkg.local
  wp_siteurl: http://cpkg.local/wp
  wp_env: development
  db_name: cpkg_local
  db_user: luqo33
  db_password: bonzo86    

After provisioning the VM, only cpkg_local is present when I log in to mysql from the VM. tarasiewicz.local install does not have its database.

Why do I experience this issue? I would once again welcome your advice.

I don’t know what version of bedrock-ansible you’re using, but here’s the current code that creates the databases:

You can see that it loops over wordpress_sites and creates a database for each one. There may have been a bug at some point so maybe you can post your code?

It will either be in roles/wordpress-sites/tasks/database.yml or roles/wordpress-setup/tasks/database.yml

My code seem to have only cosmetic differences:

  • name: Create database of sites
    mysql_db: name=“{{ item.env.db_name | default(item.site_name) }}”
    state=present
    login_host=“{{ item.env.db_host | default(‘localhost’) }}”
    login_user=“{{ mysql_user }}”
    login_password=“{{ mysql_root_password }}”
    with_items: wordpress_sites
    when: item.db_create | default(True)

I’m totally new to ansible, but it does not seem to be overwriting each created database with the next database it is creating. Might the reason be that I’m using the same user for both databases, or is it irrelevant?

I can’t think of any reason why that wouldn’t be working. db_user wouldn’t matter. Only thing that would is the db_name and you have yours set correctly.

You can manually verify which databases exist from the command line:

mysql -u luqo33 p -e "show databases"

When I execute this command:

mysql -u luqo33 p -e "show databases"

I can see only the last database exists (in my case this is cpkg_local).

When I look up the databases with root credentials and not luqo33, I can see that both databases exist, i.e. tarasiewicz_local and cpkg_local.

Would this information help to solve the problem?

I managed to solve the issue. Once I used a different user for each stie’s database, all is working as expected. Both sites can communicate with their databases. I only need to log in to these mysql databases with different credentials.

I’m still wondering where in the configuration files it is determined that one db_user should have only one database assigned to him…

If you are working locally and want to use one user for all databases, you may as well use the root user. If it’s for a staging/production site, then you’ll be safer using one user per database/site anyways.

1 Like

A fair point indeed.

However, I’d still like to see when in the ansible playbooks it is determined that there should be one db user for one database. I was not able to find such fragment of configuration in the playbooks. Just pure curiosity :wink:

If this is indeed happening it’s a bug and not by design. And a weird bug too since I’m not sure why the user would matter. I did take a look earlier and didn’t see any issues (without trying it).

I just wanted to add that I was seeing the “Can’t select database” error when trying to use the same user for 2 development databases.