# Multiple sites on one Vagrant box with bedrock-ansible

**URL:** https://discourse.roots.io/t/multiple-sites-on-one-vagrant-box-with-bedrock-ansible/3385
**Category:** bedrock
**Created:** 2015-04-01T19:27:49Z
**Posts:** 15

## Post 1 by @luqo33 — 2015-04-01T19:27:50Z

Hi,

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

> [@Bedrock-Ansible Workflow](https://discourse.roots.io/t/bedrock-ansible-workflow/2636):
>
> I’m trying to fully understand the best / what’s required setup for working with bedrock-ansible. I work on a few sites at once but I don’t have varying environments so multiple VM’s aren’t needed. I also use Bedrock for all my sites so here are my questions: The setup instructions always refer to the bedrock folder. Do I have to keep a separate empty bedrock project in addition to my other projects? Do I have to configure settings for each project in the vagrant file? For example, these foll…

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](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.

---

## Post 2 by @swalkinshaw — 2015-04-02T16:25:48Z

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.

---

## Post 3 by @luqo33 — 2015-04-03T21:26:35Z

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?

---

## Post 4 by @swalkinshaw — 2015-04-03T22:20:55Z

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`.

---

## Post 5 by @kalenjohnson — 2015-04-03T23:48:37Z

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

---

## Post 6 by @luqo33 — 2015-04-04T09:33:21Z

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.

---

## Post 7 by @swalkinshaw — 2015-04-04T19:11:29Z

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

> <https://github.com/roots/trellis/blob/6b403203f9fac49f8c2264c7bf17e0011e70ead4/roles/wordpress-setup/tasks/database.yml>

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`

---

## Post 8 by @luqo33 — 2015-04-04T20:40:24Z

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?

---

## Post 9 by @swalkinshaw — 2015-04-04T20:59:42Z

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"`

---

## Post 10 by @luqo33 — 2015-04-07T19:42:55Z

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?

---

## Post 11 by @luqo33 — 2015-04-07T20:07:51Z

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…

---

## Post 12 by @kalenjohnson — 2015-04-07T20:23:20Z

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.

---

## Post 13 by @luqo33 — 2015-04-07T21:21:52Z

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:

---

## Post 14 by @swalkinshaw — 2015-04-08T01:12:15Z

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

---

## Post 15 by @alex_ciarlillo — 2015-04-27T19:15:48Z

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.
