Roots Discourse

Wrong .env file gets copied to wrong web root on vagrant up --provision

For some reason the wrong .env file gets copied to the wrong website folder.

What I’m trying to achieve:

  1. Use a monorepo with three folders, /frontend, /backend and /server
  2. Have multiple sites in wordpress_sites.yml
  3. Use this setup to run two WordPress websites (noten.nl and nussmarkt.de) which will be there own unique businesses and have different languages (Dutch and German), but are built on the same codebase

Reproducing the bug:

  1. Run vagrant destroy to remove all local configurations and files
  2. Configure wordpress_sites.yml and vault.yml in the development folder
  3. Run vagrant up --prosivionand keep an eye on the two Ansible .env tasks: Create .env file and Copy .env file into web root
  4. SSH into the server and navigate to /tmp where the temporary .env files are stored. These values are correct and match with the data in wordpress_sites.yml
  5. Navigate to the webroots and check the .env file in the /current folder
  6. :warning:Here’s the bug :warning:: The .env file data of the lowest website in wordpress_sites.yml (in my case that’s nussmarkt.de) is copied to both of the webroots.

My wordpress_sites.yml:

wordpress_sites:
  noten.nl:
    site_hosts:
      - canonical: api-development.noten.nl
    local_path: ../backend
    admin_email: ******@noten.nl
    multisite:
      enabled: false
    ssl:
      enabled: false
    cache:
      enabled: false
    env:
      cookie_domain: noten.nl
      frontend_url: http://development.noten.nl

  nussekaufen.de:
    site_hosts:
      - canonical: api-development.nussekaufen.de
    local_path: ../backend
    admin_email: ******@nussekaufen.de
    multisite:
      enabled: false
    ssl:
      enabled: false
    cache:
      enabled: false
    env:
      cookie_domain: nussekaufen.de
      frontend_url: http://development.nussekaufen.de

.env file of noten.nl (/srv/www/noten.nl/current) - (contains data of NusseKaufen.de, which it shouldn’t):

ACF_PRO_KEY=‘******’
AUTH_KEY='******'
AUTH_SALT='******'
COOKIE_DOMAIN='nussekaufen.de'
DB_HOST='localhost'
DB_NAME='nussekaufen_de_development'
DB_PASSWORD='******'
DB_USER='nussekaufen_de'
DB_USER_HOST='localhost'
DISABLE_WP_CRON='True'
DOMAIN_CURRENT_SITE='api-development.nussekaufen.de'
FRONTEND_URL='http://development.nussekaufen.de'
GRAPHQL_JWT_AUTH_SECRET_KEY='******'
LOGGED_IN_KEY='******'
LOGGED_IN_SALT='******'
NONCE_KEY='******'
NONCE_SALT='******'
SECURE_AUTH_KEY='******'
SECURE_AUTH_SALT='******'
WP_DEBUG_LOG='/srv/www/nussekaufen.de/logs/debug.log'
WP_ENV='development'
WP_HOME='http://api-development.nussekaufen.de'
WP_SITEURL='http://api-development.nussekaufen.de/wp'

.env file of nussekaufen.de (/srv/www/nussekaufen.de/current):

ACF_PRO_KEY=‘******’
AUTH_KEY='******'
AUTH_SALT='******'
COOKIE_DOMAIN='nussekaufen.de'
DB_HOST='localhost'
DB_NAME='nussekaufen_de_development'
DB_PASSWORD='******'
DB_USER='nussekaufen_de'
DB_USER_HOST='localhost'
DISABLE_WP_CRON='True'
DOMAIN_CURRENT_SITE='api-development.nussekaufen.de'
FRONTEND_URL='http://development.nussekaufen.de'
GRAPHQL_JWT_AUTH_SECRET_KEY='******'
LOGGED_IN_KEY='******'
LOGGED_IN_SALT='******'
NONCE_KEY='******'
NONCE_SALT='******'
SECURE_AUTH_KEY='******'
SECURE_AUTH_SALT='******'
WP_DEBUG_LOG='/srv/www/nussekaufen.de/logs/debug.log'
WP_ENV='development'
WP_HOME='http://api-development.nussekaufen.de'
WP_SITEURL='http://api-development.nussekaufen.de/wp'

Setup

  • Trellis 1.7.0
  • macOS Big Sur 11.1 (20C69)
  • Ansible 2.10.3
  • Vagrant 2.2.14

This problem only occurs on local development.

Any idea where to start debugging?

Limited vagrant up --provision to handle just the wordpress tag and enabled verbose output for Ansible.

The verbose output of TASK [wordpress-install : Copy .env file into web root]:

TASK [wordpress-install : Copy .env file into web root] ************************
changed: [default -> 127.0.0.1] => (item={'key': 'noten.nl', 'value': {'site_hosts': [{'canonical': 'api-development.noten.nl'}], 'local_path': '../backend', 'admin_email': '******@noten.nl', 'multisite': {'enabled': False}, 'ssl': {'enabled': False}, 'cache': {'enabled': False}, 'env': {'cookie_domain': 'noten.nl', 'frontend_url': 'http://development.noten.nl'}}}) => {"ansible_loop_var": "item", "changed": true, "cmd": "/usr/bin/rsync --delay-updates -F --compress --checksum --archive --out-format=<<CHANGED>>%i %n%L /tmp/noten.nl.env /srv/www/noten.nl/current/.env", "item": {"key": "noten.nl", "value": {"admin_email": "******@noten.nl", "cache": {"enabled": false}, "env": {"cookie_domain": "noten.nl", "frontend_url": "http://development.noten.nl"}, "local_path": "../backend", "multisite": {"enabled": false}, "site_hosts": [{"canonical": "api-development.noten.nl"}], "ssl": {"enabled": false}}}, "msg": ">fcstp..... noten.nl.env\n", "rc": 0, "stdout_lines": [">fcstp..... noten.nl.env"]}
changed: [default -> 127.0.0.1] => (item={'key': 'nussekaufen.de', 'value': {'site_hosts': [{'canonical': 'api-development.nussekaufen.de'}], 'local_path': '../backend', 'admin_email': '******@nussekaufen.de', 'multisite': {'enabled': False}, 'ssl': {'enabled': False}, 'cache': {'enabled': False}, 'env': {'cookie_domain': 'nussekaufen.de', 'frontend_url': 'http://development.nussekaufen.de'}}}) => {"ansible_loop_var": "item", "changed": true, "cmd": "/usr/bin/rsync --delay-updates -F --compress --checksum --archive --out-format=<<CHANGED>>%i %n%L /tmp/nussekaufen.de.env /srv/www/nussekaufen.de/current/.env", "item": {"key": "nussekaufen.de", "value": {"admin_email": "******@nussekaufen.de", "cache": {"enabled": false}, "env": {"cookie_domain": "nussekaufen.de", "frontend_url": "http://development.nussekaufen.de"}, "local_path": "../backend", "multisite": {"enabled": false}, "site_hosts": [{"canonical": "api-development.nussekaufen.de"}], "ssl": {"enabled": false}}}, "msg": ">fcstp..... nussekaufen.de.env\n", "rc": 0, "stdout_lines": [">fcstp..... nussekaufen.de.env"]}

So it seems like the right .env file actually gets copied to the right folder.

Allright. I’ve missed something obvious. The reason I’m seeing the wrong .env data in my development environment is because I’m using the same local_path for two different wordpress_sites.

The .env file in my local /backend folder gets mapped to the Vagrant server, which overwrites the .env file it created before.

I’ve tried hacking / fixing it by creating a local folder “/backend_nussekaufen” with symlinks to the contents of the original “/backend” folder and setting “/backend_nussekaufen” as my “local_path” for the nussekaufen.de site in wordpress_sites, but unfortunately it still created the .env file in “/backend”.

Sleeping on it now. Might think of something tomorrow.

To confirm, are you using the same bedrock repo (../backend) for both noten.nl and nussekaufen.de?

Yes, that’s correct.

I’ve found a solution:
I modified both Trellis and Bedrock to use {{ site_name }}.env (e.g. nussekaufen.de.env or noten.nl.env) instead of the default .env file.

1 Like